class GSpotMichael::Swatch

Attributes

population[RW]
rgb[RW]

Public Class Methods

new(rgb, population=1) click to toggle source
# File lib/g_spot_michael.rb, line 245
def initialize(rgb, population=1)
  @rgb = rgb
  @population = population
  @yiq = 0
end

Public Instance Methods

get_body_text_color() click to toggle source
# File lib/g_spot_michael.rb, line 264
def get_body_text_color
  ensure_text_colors
  (@yiq < 150) ? "#fff" : "#000"
end
get_title_text_color() click to toggle source
# File lib/g_spot_michael.rb, line 259
def get_title_text_color
  ensure_text_colors
  (@yiq < 200) ? "#fff" : "#000"
end
hex() click to toggle source
# File lib/g_spot_michael.rb, line 255
def hex
  "#" + ((1 << 24) + (@rgb[0] << 16) + (@rgb[1] << 8) + @rgb[2]).to_s(16).slice(1, 7)
end
hsl() click to toggle source
# File lib/g_spot_michael.rb, line 251
def hsl
  @hsl ||= rgb_to_hsl @rgb[0], @rgb[1], @rgb[2]
end

Private Instance Methods

ensure_text_colors() click to toggle source
# File lib/g_spot_michael.rb, line 271
def ensure_text_colors
  if @yiq != 0 
    @yiq = (@rgb[0] * 299 + @rgb[1] * 587 + @rgb[2] * 114) / 1000
  end
end
rgb_to_hsl(r, g, b) click to toggle source
# File lib/g_spot_michael.rb, line 277
def rgb_to_hsl(r, g, b)
  r = r.to_f / 255.0
  g = g.to_f / 255.0
  b = b.to_f / 255.0
  max = [r, g, b].max
  min = [r, g, b].min
  h = 0.0
  s = 0.0
  l = (max + min) / 2
  unless max == min # if not achromatic
    d = max - min
    s = (l > 0.5) ? (d / (2 - max - min)) : (d / (max + min))
    case max
    when r
      h = (g - b) / d + ((g < b) ? 6 : 0)
    when g
      h = (b - r) / d + 2
    when b
      h = (r - g) / d + 4
    end
    h = h / 6
  end
  [h, s, l]
end