Class: ColorConversion::ColorConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/color_conversion/color_converter.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(color) ⇒ ColorConverter

Returns a new instance of ColorConverter.



23
24
25
# File 'lib/color_conversion/color_converter.rb', line 23

def initialize(color)
  @rgba = to_rgba(color)
end

Class Attribute Details

.convertersObject (readonly)

Returns the value of attribute converters.



7
8
9
# File 'lib/color_conversion/color_converter.rb', line 7

def converters
  @converters
end

Instance Attribute Details

#rgbaObject (readonly)

Returns the value of attribute rgba.



3
4
5
# File 'lib/color_conversion/color_converter.rb', line 3

def rgba
  @rgba
end

Class Method Details

.factory(color) ⇒ Object



16
17
18
19
20
21
# File 'lib/color_conversion/color_converter.rb', line 16

def self.factory(color)
  converter = ColorConverter.converters.find do |klass| 
    klass.matches?(color)
  end
  converter.new(color) if converter
end

.inherited(subclass) ⇒ Object



12
13
14
# File 'lib/color_conversion/color_converter.rb', line 12

def self.inherited(subclass)
  ColorConverter.converters << subclass
end

Instance Method Details

#alphaObject



67
68
69
# File 'lib/color_conversion/color_converter.rb', line 67

def alpha
  @rgba[:a]
end

#cmykObject



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/color_conversion/color_converter.rb', line 53

def cmyk
  @r, @g, @b = rgb_array_frac

  k = [1.0 - @r, 1.0 - @g, 1.0 - @b].min
  c = (1.0 - @r - k) / (1.0 - k)
  m = (1.0 - @g - k) / (1.0 - k)
  y = (1.0 - @b - k) / (1.0 - k)

  {c: (c * 100).round, 
   m: (m * 100).round, 
   y: (y * 100).round, 
   k: (k * 100).round}
end

#hexObject



31
32
33
# File 'lib/color_conversion/color_converter.rb', line 31

def hex      
  "#" + ("%02x" % @rgba[:r] + "%02x" % @rgba[:g] + "%02x" % @rgba[:b])
end

#hsbObject



47
48
49
50
51
# File 'lib/color_conversion/color_converter.rb', line 47

def hsb
  hsb = hsv
  hsb[:b] = hsb.delete(:v)
  hsb
end

#hslObject



35
36
37
38
39
# File 'lib/color_conversion/color_converter.rb', line 35

def hsl
  @r, @g, @b = rgb_array_frac

  {h: hue, s: hsl_saturation, l: hsl_lightness}
end

#hsvObject



41
42
43
44
45
# File 'lib/color_conversion/color_converter.rb', line 41

def hsv
  @r, @g, @b = rgb_array

  {h: hue, s: hsv_saturation, v: hsv_value}
end

#nameObject



71
72
73
# File 'lib/color_conversion/color_converter.rb', line 71

def name
  NameConverter.name_for_rgb(rgb)
end

#rgbObject



27
28
29
# File 'lib/color_conversion/color_converter.rb', line 27

def rgb
  {r: @rgba[:r], g: @rgba[:g], b: @rgba[:b]}
end