color_conversion 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. data/.gitignore +20 -0
  2. data/Gemfile +4 -0
  3. data/Guardfile +9 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +67 -0
  6. data/Rakefile +22 -0
  7. data/color_conversion.gemspec +31 -0
  8. data/lib/color_conversion.rb +20 -0
  9. data/lib/color_conversion/color.rb +10 -0
  10. data/lib/color_conversion/color_converter.rb +124 -0
  11. data/lib/color_conversion/converters/cmyk_converter.rb +26 -0
  12. data/lib/color_conversion/converters/hex_converter.rb +26 -0
  13. data/lib/color_conversion/converters/hsl_converter.rb +51 -0
  14. data/lib/color_conversion/converters/hsl_string_converter.rb +24 -0
  15. data/lib/color_conversion/converters/hsv_converter.rb +35 -0
  16. data/lib/color_conversion/converters/name_converter.rb +167 -0
  17. data/lib/color_conversion/converters/null_converter.rb +14 -0
  18. data/lib/color_conversion/converters/rgb_converter.rb +21 -0
  19. data/lib/color_conversion/converters/rgb_string_converter.rb +25 -0
  20. data/lib/color_conversion/errors.rb +4 -0
  21. data/lib/color_conversion/version.rb +3 -0
  22. data/spec/color_converter_spec.rb +63 -0
  23. data/spec/color_spec.rb +94 -0
  24. data/spec/converters/cmyk_converter_spec.rb +31 -0
  25. data/spec/converters/hex_converter_spec.rb +33 -0
  26. data/spec/converters/hsl_converter_spec.rb +41 -0
  27. data/spec/converters/hsl_string_converter_spec.rb +31 -0
  28. data/spec/converters/hsv_converter_spec.rb +36 -0
  29. data/spec/converters/name_converter_spec.rb +26 -0
  30. data/spec/converters/null_converter_spec.rb +19 -0
  31. data/spec/converters/rgb_converter_spec.rb +41 -0
  32. data/spec/converters/rgb_string_converter_spec.rb +45 -0
  33. data/spec/spec_helper.rb +9 -0
  34. data/spec/version_spec.rb +9 -0
  35. metadata +174 -0
@@ -0,0 +1,24 @@
1
+ module ColorConversion
2
+ class HslStringConverter < ColorConverter
3
+
4
+ def self.matches?(color)
5
+ return false unless color.kind_of?(String)
6
+
7
+ color.include?("hsl(") || color.include?("hsla(")
8
+ end
9
+
10
+ private
11
+
12
+ def to_rgba(hsl)
13
+ matches = hsl.match(/hsla?\(([0-9\.,%\s]+)\)/)
14
+ raise InvalidColorError unless matches
15
+
16
+ h, s, l, a = matches[1].split(",").map do |color|
17
+ color.gsub("%", "").strip
18
+ end
19
+ raise InvalidColorError unless h && s && l
20
+
21
+ HslConverter.new(h: h, s: s, l: l, a: a).rgba
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,35 @@
1
+ module ColorConversion
2
+ class HsvConverter < ColorConverter
3
+
4
+ def self.matches?(color)
5
+ return false unless color.kind_of?(Hash)
6
+
7
+ color.include?(:h) && color.include?(:s) &&
8
+ ( color.include?(:v) || color.include?(:b) )
9
+ end
10
+
11
+ private
12
+
13
+ def to_rgba(hsv)
14
+ h = hsv[:h] / 60.0
15
+ s = hsv[:s] / 100.0
16
+ v = (hsv[:v] || hsv[:b]) / 100.0
17
+ hi = h.floor % 6.0
18
+
19
+ f = h - h.floor
20
+ p = (255 * v * (1 - s)).round
21
+ q = (255 * v * (1 - (s * f))).round
22
+ t = (255 * v * (1 - (s * (1 - f)))).round
23
+ v = (255 * v).round
24
+
25
+ case hi
26
+ when 0 then {r: v, g: t, b: p, a: 1.0}
27
+ when 1 then {r: q, g: v, b: p, a: 1.0}
28
+ when 2 then {r: p, g: v, b: t, a: 1.0}
29
+ when 3 then {r: p, g: q, b: v, a: 1.0}
30
+ when 4 then {r: t, g: p, b: v, a: 1.0}
31
+ when 5 then {r: v, g: p, b: q, a: 1.0}
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,167 @@
1
+ module ColorConversion
2
+ class NameConverter < ColorConverter
3
+
4
+ def self.matches?(color)
5
+ return false unless color.kind_of?(String)
6
+
7
+ color_names.include?(color.downcase.to_sym)
8
+ end
9
+
10
+ private
11
+
12
+ def to_rgba(name)
13
+ rgb = self.class.color_names[name.downcase.to_sym]
14
+ {r: rgb[0], g: rgb[1], b: rgb[2], a: 1.0}
15
+ end
16
+
17
+ def self.color_names
18
+ {aliceblue: [240,248,255],
19
+ antiquewhite: [250,235,215],
20
+ aqua: [0,255,255],
21
+ aquamarine: [127,255,212],
22
+ azure: [240,255,255],
23
+ beige: [245,245,220],
24
+ bisque: [255,228,196],
25
+ black: [0,0,0],
26
+ blanchedalmond: [255,235,205],
27
+ blue: [0,0,255],
28
+ blueviolet: [138,43,226],
29
+ brown: [165,42,42],
30
+ burlywood: [222,184,135],
31
+ cadetblue: [95,158,160],
32
+ chartreuse: [127,255,0],
33
+ chocolate: [210,105,30],
34
+ coral: [255,127,80],
35
+ cornflowerblue: [100,149,237],
36
+ cornsilk: [255,248,220],
37
+ crimson: [220,20,60],
38
+ cyan: [0,255,255],
39
+ darkblue: [0,0,139],
40
+ darkcyan: [0,139,139],
41
+ darkgoldenrod: [184,134,11],
42
+ darkgray: [169,169,169],
43
+ darkgreen: [0,100,0],
44
+ darkgrey: [169,169,169],
45
+ darkkhaki: [189,183,107],
46
+ darkmagenta: [139,0,139],
47
+ darkolivegreen: [85,107,47],
48
+ darkorange: [255,140,0],
49
+ darkorchid: [153,50,204],
50
+ darkred: [139,0,0],
51
+ darksalmon: [233,150,122],
52
+ darkseagreen: [143,188,143],
53
+ darkslateblue: [72,61,139],
54
+ darkslategray: [47,79,79],
55
+ darkslategrey: [47,79,79],
56
+ darkturquoise: [0,206,209],
57
+ darkviolet: [148,0,211],
58
+ deeppink: [255,20,147],
59
+ deepskyblue: [0,191,255],
60
+ dimgray: [105,105,105],
61
+ dimgrey: [105,105,105],
62
+ dodgerblue: [30,144,255],
63
+ firebrick: [178,34,34],
64
+ floralwhite: [255,250,240],
65
+ forestgreen: [34,139,34],
66
+ fuchsia: [255,0,255],
67
+ gainsboro: [220,220,220],
68
+ ghostwhite: [248,248,255],
69
+ gold: [255,215,0],
70
+ goldenrod: [218,165,32],
71
+ gray: [128,128,128],
72
+ green: [0,128,0],
73
+ greenyellow: [173,255,47],
74
+ grey: [128,128,128],
75
+ honeydew: [240,255,240],
76
+ hotpink: [255,105,180],
77
+ indianred: [205,92,92],
78
+ indigo: [75,0,130],
79
+ ivory: [255,255,240],
80
+ khaki: [240,230,140],
81
+ lavender: [230,230,250],
82
+ lavenderblush: [255,240,245],
83
+ lawngreen: [124,252,0],
84
+ lemonchiffon: [255,250,205],
85
+ lightblue: [173,216,230],
86
+ lightcoral: [240,128,128],
87
+ lightcyan: [224,255,255],
88
+ lightgoldenrodyellow: [250,250,210],
89
+ lightgray: [211,211,211],
90
+ lightgreen: [144,238,144],
91
+ lightgrey: [211,211,211],
92
+ lightpink: [255,182,193],
93
+ lightsalmon: [255,160,122],
94
+ lightseagreen: [32,178,170],
95
+ lightskyblue: [135,206,250],
96
+ lightslategray: [119,136,153],
97
+ lightslategrey: [119,136,153],
98
+ lightsteelblue: [176,196,222],
99
+ lightyellow: [255,255,224],
100
+ lime: [0,255,0],
101
+ limegreen: [50,205,50],
102
+ linen: [250,240,230],
103
+ magenta: [255,0,255],
104
+ maroon: [128,0,0],
105
+ mediumaquamarine: [102,205,170],
106
+ mediumblue: [0,0,205],
107
+ mediumorchid: [186,85,211],
108
+ mediumpurple: [147,112,219],
109
+ mediumseagreen: [60,179,113],
110
+ mediumslateblue: [123,104,238],
111
+ mediumspringgreen: [0,250,154],
112
+ mediumturquoise: [72,209,204],
113
+ mediumvioletred: [199,21,133],
114
+ midnightblue: [25,25,112],
115
+ mintcream: [245,255,250],
116
+ mistyrose: [255,228,225],
117
+ moccasin: [255,228,181],
118
+ navajowhite: [255,222,173],
119
+ navy: [0,0,128],
120
+ oldlace: [253,245,230],
121
+ olive: [128,128,0],
122
+ olivedrab: [107,142,35],
123
+ orange: [255,165,0],
124
+ orangered: [255,69,0],
125
+ orchid: [218,112,214],
126
+ palegoldenrod: [238,232,170],
127
+ palegreen: [152,251,152],
128
+ paleturquoise: [175,238,238],
129
+ palevioletred: [219,112,147],
130
+ papayawhip: [255,239,213],
131
+ peachpuff: [255,218,185],
132
+ peru: [205,133,63],
133
+ pink: [255,192,203],
134
+ plum: [221,160,221],
135
+ powderblue: [176,224,230],
136
+ purple: [128,0,128],
137
+ red: [255,0,0],
138
+ rosybrown: [188,143,143],
139
+ royalblue: [65,105,225],
140
+ saddlebrown: [139,69,19],
141
+ salmon: [250,128,114],
142
+ sandybrown: [244,164,96],
143
+ seagreen: [46,139,87],
144
+ seashell: [255,245,238],
145
+ sienna: [160,82,45],
146
+ silver: [192,192,192],
147
+ skyblue: [135,206,235],
148
+ slateblue: [106,90,205],
149
+ slategray: [112,128,144],
150
+ slategrey: [112,128,144],
151
+ snow: [255,250,250],
152
+ springgreen: [0,255,127],
153
+ steelblue: [70,130,180],
154
+ tan: [210,180,140],
155
+ teal: [0,128,128],
156
+ thistle: [216,191,216],
157
+ tomato: [255,99,71],
158
+ turquoise: [64,224,208],
159
+ violet: [238,130,238],
160
+ wheat: [245,222,179],
161
+ white: [255,255,255],
162
+ whitesmoke: [245,245,245],
163
+ yellow: [255,255,0],
164
+ yellowgreen: [154,205,50]}
165
+ end
166
+ end
167
+ end
@@ -0,0 +1,14 @@
1
+ module ColorConversion
2
+ class NullConverter < ColorConverter
3
+
4
+ def self.matches?(color)
5
+ true
6
+ end
7
+
8
+ private
9
+
10
+ def to_rgba(color)
11
+ raise InvalidColorError
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,21 @@
1
+ module ColorConversion
2
+ class RgbConverter < ColorConverter
3
+
4
+ def self.matches?(color)
5
+ return false unless color.kind_of?(Hash)
6
+
7
+ color.include?(:r) && color.include?(:g) && color.include?(:b)
8
+ end
9
+
10
+ private
11
+
12
+ def to_rgba(rgb)
13
+ r = rgb[:r].to_f
14
+ g = rgb[:g].to_f
15
+ b = rgb[:b].to_f
16
+ a = (rgb[:a] || 1.0).to_f
17
+
18
+ {r: r, g: g, b: b, a: a}
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,25 @@
1
+ module ColorConversion
2
+ class RgbStringConverter < ColorConverter
3
+
4
+ def self.matches?(color)
5
+ return false unless color.kind_of?(String)
6
+
7
+ color.include?("rgb(") || color.include?("rgba(")
8
+ end
9
+
10
+ private
11
+
12
+ def to_rgba(rgb)
13
+ matches = rgb.match(/rgba?\(([0-9\.,\s]+)\)/)
14
+ raise InvalidColorError unless matches
15
+
16
+ r, g, b, a = matches[1].split(",").map {|color| color.strip }
17
+ raise InvalidColorError unless r && g && b
18
+
19
+ {r: r.to_i,
20
+ g: g.to_i,
21
+ b: b.to_i,
22
+ a: (a || 1.0).to_f}
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,4 @@
1
+ module ColorConversion
2
+ class Error < StandardError; end
3
+ class InvalidColorError < Error; end
4
+ end
@@ -0,0 +1,3 @@
1
+ module ColorConversion
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,63 @@
1
+ require 'spec_helper'
2
+
3
+ describe ColorConverter do
4
+
5
+ describe ".rgb" do
6
+ it "converts color to rgb" do
7
+ conv = RgbConverter.new(r: 51, g: 102, b: 204)
8
+
9
+ rgb = {r: 51, g: 102, b: 204}
10
+ expect(conv.rgb).to eq rgb
11
+ end
12
+ end
13
+
14
+ describe ".hex" do
15
+ it "converts color to hex" do
16
+ conv = RgbConverter.new(r: 51, g: 102, b: 204)
17
+ expect(conv.hex).to eq "#3366cc"
18
+ end
19
+ end
20
+
21
+ describe ".hsl" do
22
+ it "converts color to hsl" do
23
+ conv = RgbConverter.new(r: 51, g: 102, b: 204)
24
+
25
+ hsl = {h: 220, s: 60, l: 50}
26
+ expect(conv.hsl).to eq hsl
27
+ end
28
+ end
29
+
30
+ describe ".hsv" do
31
+ it "converts color to hsv" do
32
+ conv = RgbConverter.new(r: 51, g: 102, b: 204)
33
+
34
+ hsv = {h: 220, s: 75, v: 80}
35
+ expect(conv.hsv).to eq hsv
36
+ end
37
+ end
38
+
39
+ describe ".hsb" do
40
+ it "converts color to hsb" do
41
+ conv = RgbConverter.new(r: 51, g: 102, b: 204)
42
+
43
+ hsb = {h: 220, s: 75, b: 80}
44
+ expect(conv.hsb).to eq hsb
45
+ end
46
+ end
47
+
48
+ describe ".cymk" do
49
+ it "converts color to cymk" do
50
+ conv = RgbConverter.new(r: 64, g: 104, b: 193)
51
+
52
+ cmyk = {c: 67, m: 46, y: 0, k: 24}
53
+ expect(conv.cmyk).to eq cmyk
54
+ end
55
+ end
56
+
57
+ describe ".alpha" do
58
+ it "finds alpha for color" do
59
+ conv = RgbConverter.new(r: 51, g: 102, b: 204, a: 0.5)
60
+ expect(conv.alpha).to eq 0.5
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,94 @@
1
+ require 'spec_helper'
2
+
3
+ describe Color do
4
+
5
+ describe ".new with color string" do
6
+
7
+ it "should initialize color by lower hex" do
8
+ color = Color.new("#3366cc")
9
+ expect(color.rgb).to eq(r: 51, g: 102, b: 204)
10
+ end
11
+
12
+ it "should initialize color by hex with hash" do
13
+ color = Color.new("#3366CC")
14
+ expect(color.rgb).to eq(r: 51, g: 102, b: 204)
15
+ end
16
+
17
+ it "should initialize color by short hex" do
18
+ color = Color.new("#36C")
19
+ expect(color.rgb).to eq(r: 51, g: 102, b: 204)
20
+ end
21
+
22
+ it "should initialize color by rgb string" do
23
+ color = Color.new("rgb(51, 102, 204)")
24
+ expect(color.rgb).to eq(r: 51, g: 102, b: 204)
25
+ end
26
+
27
+ it "should initialize color by rgba string" do
28
+ color = Color.new("rgba(51, 102, 204, 0.2)")
29
+ expect(color.rgb).to eq(r: 51, g: 102, b: 204)
30
+ expect(color.alpha).to eq 0.2
31
+ end
32
+
33
+ it "should initialize color by hsl string" do
34
+ color = Color.new("hsl(225, 73%, 57%)")
35
+ expect(color.rgb).to eq(r: 65, g: 105, b: 225)
36
+ end
37
+
38
+ it "should initialize color by hsla string" do
39
+ color = Color.new("hsla(225, 73%, 57%, 0.5)")
40
+ expect(color.rgb).to eq(r: 65, g: 105, b: 225)
41
+ expect(color.alpha).to eq 0.5
42
+ end
43
+
44
+ it "should initialize color by textual string" do
45
+ color = Color.new("royalblue")
46
+ expect(color.rgb).to eq(r: 65, g: 105, b: 225)
47
+ end
48
+
49
+ it "should initialize color by textual string case" do
50
+ color = Color.new("RoyalBlue")
51
+ expect(color.rgb).to eq(r: 65, g: 105, b: 225)
52
+ end
53
+ end
54
+
55
+
56
+ describe ".new" do
57
+ it "should initialize color by rgb" do
58
+ color = Color.new(r: 51, g: 102, b: 204)
59
+ expect(color.rgb).to eq(r: 51, g: 102, b: 204)
60
+ end
61
+
62
+ it "should initialize color by rgba" do
63
+ color = Color.new(r: 51, g: 102, b: 204, a: 0.5)
64
+ expect(color.rgb).to eq(r: 51, g: 102, b: 204)
65
+ expect(color.alpha).to eq 0.5
66
+ end
67
+
68
+ it "should initialize color by hsl" do
69
+ color = Color.new(h: 225, s: 73, l: 57)
70
+ expect(color.rgb).to eq(r: 65, g: 105, b: 225)
71
+ end
72
+
73
+ it "should initialize color by hsla" do
74
+ color = Color.new(h: 225, s: 73, l: 57, a: 0.5)
75
+ expect(color.rgb).to eq(r: 65, g: 105, b: 225)
76
+ expect(color.alpha).to eq 0.5
77
+ end
78
+
79
+ it "should initialize color by hsv" do
80
+ color = Color.new(h: 220, s: 75, v: 80)
81
+ expect(color.rgb).to eq(r: 51, g: 102, b: 204)
82
+ end
83
+
84
+ it "should initialize color by hsb" do
85
+ color = Color.new(h: 220, s: 75, b: 80)
86
+ expect(color.rgb).to eq(r: 51, g: 102, b: 204)
87
+ end
88
+
89
+ it "should initialize color by cmyk" do
90
+ color = Color.new(c: 74, m: 58, y: 22, k: 3)
91
+ expect(color.rgb).to eq(r: 64, g: 104, b: 193)
92
+ end
93
+ end
94
+ end