Class: Camalian::Color

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*value) ⇒ Color

Returns a new instance of Color.


6
7
8
9
10
11
12
13
# File 'lib/camalian/color.rb', line 6

def initialize(*value)
  if value.size == 1
    rgb = extract_rgb(value.first)
    build_components(rgb[0], rgb[1], rgb[2])
  elsif value.size == 3
    build_components(value[0], value[1], value[2])
  end
end

Instance Attribute Details

#bObject (readonly)

Returns the value of attribute b


4
5
6
# File 'lib/camalian/color.rb', line 4

def b
  @b
end

#gObject (readonly)

Returns the value of attribute g


4
5
6
# File 'lib/camalian/color.rb', line 4

def g
  @g
end

#hObject (readonly)

Returns the value of attribute h


4
5
6
# File 'lib/camalian/color.rb', line 4

def h
  @h
end

#hsvObject (readonly)

Returns the value of attribute hsv


4
5
6
# File 'lib/camalian/color.rb', line 4

def hsv
  @hsv
end

#lObject (readonly)

Returns the value of attribute l


4
5
6
# File 'lib/camalian/color.rb', line 4

def l
  @l
end

#rObject (readonly)

Returns the value of attribute r


4
5
6
# File 'lib/camalian/color.rb', line 4

def r
  @r
end

#sObject (readonly)

Returns the value of attribute s


4
5
6
# File 'lib/camalian/color.rb', line 4

def s
  @s
end

Instance Method Details

#distance(color) ⇒ Object


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

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

#extract_rgb(color_hash) ⇒ Object


27
28
29
30
31
32
33
34
# File 'lib/camalian/color.rb', line 27

def extract_rgb(color_hash)
  color_hash = color_hash[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)
  [r, g, b]
end

#to_hexObject


19
20
21
# File 'lib/camalian/color.rb', line 19

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


15
16
17
# File 'lib/camalian/color.rb', line 15

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