Class: Camalian::Color

Inherits:
Object
  • Object
show all
Defined in:
lib/camalian/color.rb

Overview

Camalian color object

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(r, g, b) ⇒ Color

Returns a new instance of Color.



8
9
10
# File 'lib/camalian/color.rb', line 8

def initialize(r, g, b)
  build_components(r, g, b)
end

Instance Attribute Details

#bObject (readonly)

Returns the value of attribute b.



6
7
8
# File 'lib/camalian/color.rb', line 6

def b
  @b
end

#gObject (readonly)

Returns the value of attribute g.



6
7
8
# File 'lib/camalian/color.rb', line 6

def g
  @g
end

#hObject (readonly)

Returns the value of attribute h.



6
7
8
# File 'lib/camalian/color.rb', line 6

def h
  @h
end

#hsvObject (readonly)

Returns the value of attribute hsv.



6
7
8
# File 'lib/camalian/color.rb', line 6

def hsv
  @hsv
end

#lObject (readonly)

Returns the value of attribute l.



6
7
8
# File 'lib/camalian/color.rb', line 6

def l
  @l
end

#rObject (readonly)

Returns the value of attribute r.



6
7
8
# File 'lib/camalian/color.rb', line 6

def r
  @r
end

#sObject (readonly)

Returns the value of attribute s.



6
7
8
# File 'lib/camalian/color.rb', line 6

def s
  @s
end

Class Method Details

.from_hex(hex_value) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/camalian/color.rb', line 12

def self.from_hex(hex_value)
  color_hash = hex_value[0..6]
  color_hash = color_hash[1..6] if color_hash[0] == '#'
  r = color_hash[0..1].to_i(16)
  g = color_hash[2..3].to_i(16)
  b = color_hash[4..5].to_i(16)

  Color.new(r, g, b)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

Used for object comparison



36
37
38
# File 'lib/camalian/color.rb', line 36

def ==(other)
  r == other.r && g == other.g && b == other.b
end

#hashObject

Used for array uniqueness



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

def hash
  "#{r}#{g}#{b}".to_i
end

#hue_distance(color) ⇒ Object



41
42
43
# File 'lib/camalian/color.rb', line 41

def hue_distance(color)
  [(h - color.h) % 360, (color.h - h) % 360].min
end

#rgb_distance(color) ⇒ Object



45
46
47
# File 'lib/camalian/color.rb', line 45

def rgb_distance(color)
  Math.sqrt(((r - color.r)**2) + ((g - color.g)**2) + ((b - color.b)**2))
end

#to_hexObject



26
27
28
# File 'lib/camalian/color.rb', line 26

def to_hex
  "##{r.to_s(16).rjust(2, '0')}#{g.to_s(16).rjust(2, '0')}#{b.to_s(16).rjust(2, '0')}"
end

#to_sObject



22
23
24
# File 'lib/camalian/color.rb', line 22

def to_s
  "red=#{r} green=#{g} blue=#{b} hue=#{h} saturation=#{s} lightness=#{l}"
end