class PaletteTown::Color

Attributes

hue[RW]
lum[RW]
sat[RW]

Public Class Methods

new(color) click to toggle source
# File lib/palettetown/color.rb, line 4
def initialize color
  if color.is_a? String
    color = self.class.from_hex(color)
  elsif color.is_a? Fixnum
    color = self.class.from_hex("%06x" % color)
  elsif color.is_a? PaletteTown::Color
    color = color.to_h
  elsif color.is_a? Hash
    if color[:hue].nil? or color[:sat].nil? or color[:lum].nil?
      raise ArgumentError
    end
  end
  @hue = color[:hue].to_f
  @sat = color[:sat].to_f
  @lum = color[:lum].to_f
end

Private Class Methods

from_hex(color) click to toggle source
# File lib/palettetown/color.rb, line 56
def self.from_hex color
  color.shift if color[0] == '#'
  r = color[0..1].to_i 16
  g = color[2..3].to_i 16
  b = color[4..5].to_i 16

  from_rgb(r, g, b)
end
from_rgb(red, green, blue) click to toggle source
# File lib/palettetown/color.rb, line 64
def self.from_rgb red, green, blue
  red /= 255.0
  green /= 255.0
  blue /= 255.0

  min = [red, green, blue].min
  max = [red, green, blue].max

  lum = (max + min) / 2

  if max == min
    hue = 0
    sat = 0
  else
    delta = max - min

    if lum > 0.5
      sat = delta / (2 - max - min)
    else
      sat = delta / (max + min)
    end

    case max
    when red
      hue  = (green - blue) / delta
      hue += 6 if green < blue
    when green
      hue  = (blue - red) / delta
      hue += 2
    when blue
      hue  = (red - green) / delta
      hue += 4
    end
    hue /= 6
    hue *= Math::PI * 2  # Convert 0..1 into radians
  end
  return {hue: hue, sat: sat, lum: lum}
end
hue_to_rgb(x, y, z) click to toggle source
# File lib/palettetown/color.rb, line 102
def self.hue_to_rgb x, y, z
  # Frankly I don't know what this all does
  # I just ported it from the W3C CSS spec
  z += 1 if z < 0
  z -= 1 if z > 1
  return x + (y - x) * z * 6.0 if z < 1.0 / 6.0
  return y if z < 1.0 / 2.0
  return x + (y - x) * (2.0 / 3.0 - z) * 6.0 if z < 2.0 / 3.0
  return x
end

Public Instance Methods

to_h() click to toggle source
# File lib/palettetown/color.rb, line 44
def to_h
  return {
    :hue => @hue,
    :sat => @sat,
    :lum => @lum
  }
end
to_hex() click to toggle source
# File lib/palettetown/color.rb, line 41
def to_hex
  "%<r>02x%<g>02x%<b>02x" % to_rgb
end
to_rgb() click to toggle source
# File lib/palettetown/color.rb, line 20
def to_rgb
  if @sat == 0
    r = g = b = @lum
  else
    y = if @lum < 0.5
      @lum * (@sat + 1.0)
    else
      @lum + @sat - (@lum * @sat)
    end
    x = @lum * 2.0 - y
    # Why are we using 2pi here?  Because we're using Radians, bitch.
    r = self.class.hue_to_rgb(x, y, @hue * Math::PI * 2 + 1.0/3.0)
    g = self.class.hue_to_rgb(x, y, @hue * Math::PI * 2)
    b = self.class.hue_to_rgb(x, y, @hue * Math::PI * 2 - 1.0/3.0)
  end
  return {
    :r => (r * 255).to_i,
    :g => (g * 255).to_i,
    :b => (b * 255).to_i
  }
end
to_s() click to toggle source
# File lib/palettetown/color.rb, line 51
def to_s
  "##{to_hex}"
end