Class: Borealis::Color

Inherits:
Object
  • Object
show all
Includes:
Math
Defined in:
lib/borealis/color.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(red, green, blue) ⇒ Color

Returns a new instance of Color.



7
8
9
10
11
# File 'lib/borealis/color.rb', line 7

def initialize(red, green, blue)
  @red = red
  @green = green
  @blue = blue
end

Instance Attribute Details

#blueObject (readonly)

Returns the value of attribute blue.



5
6
7
# File 'lib/borealis/color.rb', line 5

def blue
  @blue
end

#greenObject (readonly)

Returns the value of attribute green.



5
6
7
# File 'lib/borealis/color.rb', line 5

def green
  @green
end

#redObject (readonly)

Returns the value of attribute red.



5
6
7
# File 'lib/borealis/color.rb', line 5

def red
  @red
end

Instance Method Details

#==(other) ⇒ Object



31
32
33
# File 'lib/borealis/color.rb', line 31

def ==(other)
  red == other.red && green == other.green && blue == other.blue
end

#hexObject



27
28
29
# File 'lib/borealis/color.rb', line 27

def hex
  "##{hex_segment red}#{hex_segment green}#{hex_segment blue}"
end

#lab_distance_to(other) ⇒ Object



17
18
19
20
21
# File 'lib/borealis/color.rb', line 17

def lab_distance_to(other)
  l, a, b = to_lab
  other_l, other_a, other_b = other.to_lab
  Math.sqrt((l - other_l) ** 2 + (a - other_a) ** 2 + (b - other_b) ** 2)
end

#rgbObject



23
24
25
# File 'lib/borealis/color.rb', line 23

def rgb
  "(#{red}, #{green}, #{blue})"
end

#rgb_distance_to(other) ⇒ Object



13
14
15
# File 'lib/borealis/color.rb', line 13

def rgb_distance_to(other)
  Math.sqrt((red - other.red) ** 2 + (green - other.green) ** 2 + (blue - other.blue) ** 2)
end

#to_labObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/borealis/color.rb', line 36

def to_lab
  reference_white_X =  95.047
  reference_white_Y = 100.000
  reference_white_Z = 108.883

  x, y, z = to_xyz
  x = x / reference_white_X
  y = y / reference_white_Y
  z = z / reference_white_Z

  x, y, z = [x, y, z].map do |value|
    if value > 0.008856
      value ** (1.0 / 3.0)
    else
      7.787 * value + 16.0 / 116.0
    end
  end

  cie_L = 116 * y - 16
  cie_a = 500 * (x - y)
  cie_b = 200 * (y - z)

  [cie_L, cie_a, cie_b]
end