Class: ColorConversion::ColorConverter
- Inherits:
-
Object
- Object
- ColorConversion::ColorConverter
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
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
.converters ⇒ Object
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
#rgba ⇒ Object
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
#alpha ⇒ Object
119
120
121
|
# File 'lib/color_conversion/color_converter.rb', line 119
def alpha
@rgba[:a]
end
|
#cmyk ⇒ Object
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/color_conversion/color_converter.rb', line 103
def cmyk
r = @rgba[:r] / 255.0
g = @rgba[:g] / 255.0
b = @rgba[:b] / 255.0
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
|
#hex ⇒ Object
31
32
33
|
# File 'lib/color_conversion/color_converter.rb', line 31
def hex
"#" + ("%02x" % @rgba[:r] + "%02x" % @rgba[:g] + "%02x" % @rgba[:b])
end
|
#hsb ⇒ Object
97
98
99
100
101
|
# File 'lib/color_conversion/color_converter.rb', line 97
def hsb
hsb = hsv
hsb[:b] = hsb.delete(:v)
hsb
end
|
#hsl ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/color_conversion/color_converter.rb', line 35
def hsl
r = @rgba[:r] / 255.0
g = @rgba[:g] / 255.0
b = @rgba[:b] / 255.0
min = [r, g, b].min
max = [r, g, b].max
delta = max - min
h = if max == min
0
elsif r == max
(g - b) / delta
elsif g == max
2 + (b - r) / delta
elsif b == max
4 + (r - g) / delta
end
h = [h * 60, 360].min
h += 360 if h < 0
l = (min + max) / 2.0
s = if (max == min)
0
elsif (l <= 0.5)
delta / (max + min)
else
delta / (2.0 - max - min)
end
{h: h.round, s: (s * 100).round, l: (l * 100).round}
end
|
#hsv ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/color_conversion/color_converter.rb', line 68
def hsv
r = @rgba[:r].to_f
g = @rgba[:g].to_f
b = @rgba[:b].to_f
min = [r, g, b].min
max = [r, g, b].max
delta = (max - min).to_f
s = max == 0 ? 0 : (delta / max * 1000) / 10.0
h = if max == min
0
elsif r == max
(g - b) / delta
elsif g == max
2 + (b - r) / delta
elsif b == max
4 + (r - g) / delta
end
h = [h * 60, 360].min
h += 360 if h < 0
v = ((max / 255.0) * 1000) / 10.0
{h: h.round, s: s.round, v: v.round}
end
|
#rgb ⇒ Object
27
28
29
|
# File 'lib/color_conversion/color_converter.rb', line 27
def rgb
{r: @rgba[:r], g: @rgba[:g], b: @rgba[:b]}
end
|