module Colorname

Colorname Module

This module helps you to find the closest named color's name.

The closest color is finding by calculating percent error formula between your given color and named color list's every element's Red, Green and Blue values. Logically: many named colors = better pair

Color Name Finder method

Color Name Finder method

Find dominant colors in an image

Color Name Finder method

Constants

VALUES
VERSION

Public Class Methods

base(*cc) click to toggle source
# File lib/colorname/base.rb, line 4
def base(*cc)
  # Base Color Finder
  #
  # This function helps you to find the most dominant base color name
  # of the given color.
  base_color make_color *cc
end
find(*cc) click to toggle source
# File lib/colorname/find.rb, line 4
def find(*cc)
  # find (color_code or Color instance)
  #
  # This function helps you to find the name of the closest color's name
  # to given color or color code.
  decide make_color *cc
end
find_by_image(image) click to toggle source
# File lib/colorname/find_by_image.rb, line 4
def find_by_image(image)
  # find_by_image ( image path or url )
  #
  # This function help you to find the most dominant colors' names in
  # given image.
  #
  # You can give a path or url as arguement.
  colors = Miro::DominantColors.new(image)
  color_names = []

  colors.to_rgb.each do |color|
    color_names << find(*color)
  end

  color_names
end
Also aliased as: findbyimage
findbyimage(image)
Alias for: find_by_image

Private Class Methods

base_color(cc) click to toggle source
# File lib/colorname/base.rb, line 13
def base_color(cc)
  if cc.red == cc.green && cc.green == cc.blue
    # XXX Should add grey?
    cc.red < 127 ? :black : :white
  elsif cc.red == cc.green && cc.red > cc.blue
    [:red, :green]
  elsif cc.red == cc.blue && cc.red > cc.green
    [:red, :blue]
  elsif cc.green == cc.blue && cc.green > cc.red
    [:green, :blue]
  else
    case [cc.red, cc.green, cc.blue].each_with_index.max[1]
    when 0
      [:red]
    when 1
      [:green]
    when 2
      [:blue]
    end
  end
end
cdiff(real, guess) click to toggle source
# File lib/colorname/find.rb, line 31
def cdiff(real, guess)
  r_diff = ((real.red - guess[0])/real.red).abs/100.0
  g_diff = ((real.green - guess[1])/real.green).abs/100.0
  b_diff = ((real.blue - guess[2])/real.blue).abs/100.0
  r_diff + g_diff + b_diff
end
closest(cc) click to toggle source
# File lib/colorname/find.rb, line 18
def closest(cc)
  diff = 100
  similar = :undefined
  VALUES.each do |key, val|
    new_diff = cdiff(cc,val)
    if new_diff < diff
      diff = new_diff
      similar = key
    end
  end
  similar
end
decide(cc) click to toggle source
# File lib/colorname/find.rb, line 13
def decide(cc)
  result = VALUES.select{|_, val| val == [cc.red, cc.green, cc.blue]}.keys
  result.empty? ? (closest(cc)) : result
end
make_color(*cc) click to toggle source
# File lib/colorname/pre.rb, line 5
def make_color(*cc)
  case cc.size
  when 1
    if cc.first.is_a? Color
      cc.first
    elsif cc.first.is_a? String
      Color::RGB.by_hex(cc.first)
    else
      $stderr.puts 'Invalid color code input'
    end
  when 3
    Color::RGB.new(*cc)
  else
    $stderr.puts 'Wrong number of arguments' 
  end
end