chroma 0.0.1.alpha.1 → 0.0.1.alpha.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +2 -0
- data/CHANGELOG.md +37 -0
- data/README.md +1 -1
- data/chroma.gemspec +1 -0
- data/lib/chroma.rb +6 -0
- data/lib/chroma/color.rb +13 -1
- data/lib/chroma/color/attributes.rb +6 -0
- data/lib/chroma/color/modifiers.rb +5 -5
- data/lib/chroma/color/serializers.rb +59 -58
- data/lib/chroma/converters/hsl_converter.rb +8 -0
- data/lib/chroma/converters/hsv_converter.rb +8 -0
- data/lib/chroma/converters/rgb_converter.rb +6 -2
- data/lib/chroma/harmonies.rb +3 -3
- data/lib/chroma/palette_builder.rb +58 -0
- data/lib/chroma/rgb_generator.rb +1 -1
- data/lib/chroma/rgb_generator/from_hex_string_values.rb +1 -1
- data/lib/chroma/rgb_generator/from_hsl.rb +2 -2
- data/lib/chroma/rgb_generator/from_hsl_values.rb +2 -2
- data/lib/chroma/rgb_generator/from_hsv.rb +2 -2
- data/lib/chroma/rgb_generator/from_hsv_values.rb +2 -2
- data/lib/chroma/rgb_generator/from_string.rb +1 -1
- data/lib/chroma/version.rb +1 -1
- data/spec/chroma/define_palette_spec.rb +33 -0
- data/spec/chroma/paint_spec.rb +55 -0
- data/spec/color/modifiers_spec.rb +9 -0
- data/spec/color/serializers_spec.rb +152 -0
- data/spec/color_spec.rb +30 -0
- data/spec/spec_helper.rb +99 -0
- metadata +32 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 83b41e45a991b08c177b51e08ad7147ff5f22d60
|
4
|
+
data.tar.gz: b55cf5c98e93305486a863a38cc1642fd695b57a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c6a283052b0210c1434cc8492063bf95a7653bf28c08111e840b9a0d77d936d29c6b761498bdcc940ad62e5f579df217311cf740c12d8ca7023780795571106c
|
7
|
+
data.tar.gz: 8e61b3b0060212abcab4ba2de3d1a3d700c91ecd6dc1b8dec3f154fa954e39149f6d7ee73627b085cd4efe245162954424e68632967f7df4317185ac33ebf807
|
data/.rspec
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
### v0.0.1.alpha.2
|
2
|
+
**Bug Fixes:**
|
3
|
+
* Fixed bug with number of arguments passed to generator classes in RgbGenerator. (#1)
|
4
|
+
* Make `FromHexStringValues.from_hex8` take alpha as second parameter instead of last.
|
5
|
+
Fixes incorrect color generation from hex8. (#6)
|
6
|
+
* Ensure that string serialization rounds the alpha value where applicable. (#7)
|
7
|
+
* Fix bug where `to_s` and `inspect` would return `<unknown>` instead of using
|
8
|
+
hex if the format was `:name` and the named color could not be found. (#2)
|
9
|
+
* Fix bug where `Color` equality wasn't implemented. (#12)
|
10
|
+
* Fix bug where passing in an instance of `Hsl` or `Hsv` to `Color.new` caused
|
11
|
+
their values to get changed. (#11)
|
12
|
+
* Fix bug with `Color` equality being off due to floating point math. (#13)
|
13
|
+
* Fix bug where `Color` instances generated from hsla and hsva strings had the
|
14
|
+
wrong alpha value. (#15)
|
15
|
+
|
16
|
+
**Method Changes:**
|
17
|
+
* Add optional `hex_for_unknown` parameter to `Color::Serializers#to_name`.
|
18
|
+
If true, it allows `to_name` to default to hex string if name is not found
|
19
|
+
instead of returning `'<unknown>'`. (#2)
|
20
|
+
* Add missing conversion methods to converters (a12244f0d81c9480490cfb8a472993f54dd9fbd2)
|
21
|
+
* Add equality (`eql?` and `==`) methods to `Color` class and `ColorModes`
|
22
|
+
classes. (#12, #13)
|
23
|
+
* Add `Chroma.define_palette` for defining custom palettes. (#9)
|
24
|
+
* Add `Color#paint` method for returning itself. (#14)
|
25
|
+
* Tweak `Color` serialization method names. Switched to this naming primarily
|
26
|
+
to drop the `*_s` on the string serialization methods.
|
27
|
+
* `to_hsv` -> `hsv`
|
28
|
+
* `to_hsv_s` -> `to_hsv`
|
29
|
+
* `to_hsl` -> `hsl`
|
30
|
+
* `to_hsl_s` -> `to_hsl`
|
31
|
+
* `to_hex` -> `to_basic_hex` (made private)
|
32
|
+
* `to_hex_s` -> `to_hex`
|
33
|
+
* `to_hex8` -> `to_basic_hex8` (made private)
|
34
|
+
* `to_hex8_s` -> `to_hex8`
|
35
|
+
* `to_rgb` -> `rgb` (moved attr_reader to serializers and made public)
|
36
|
+
* `to_rgb_s` -> `to_rgb`
|
37
|
+
* Removed `to_name_s` alias
|
data/README.md
CHANGED
data/chroma.gemspec
CHANGED
data/lib/chroma.rb
CHANGED
@@ -14,6 +14,7 @@ require 'chroma/color_modes'
|
|
14
14
|
|
15
15
|
# Palettes
|
16
16
|
require 'chroma/harmonies'
|
17
|
+
require 'chroma/palette_builder'
|
17
18
|
|
18
19
|
# RGB Generators
|
19
20
|
require 'chroma/rgb_generator'
|
@@ -50,6 +51,11 @@ module Chroma
|
|
50
51
|
hex_named_colors_map[hex]
|
51
52
|
end
|
52
53
|
|
54
|
+
def define_palette(name, &block)
|
55
|
+
raise "Palette `#{name}' already exists" if Harmonies.method_defined? name
|
56
|
+
PaletteBuilder.build(name, &block)
|
57
|
+
end
|
58
|
+
|
53
59
|
private
|
54
60
|
|
55
61
|
def hex_named_colors_map
|
data/lib/chroma/color.rb
CHANGED
@@ -11,8 +11,20 @@ module Chroma
|
|
11
11
|
@format = format || gen_format
|
12
12
|
end
|
13
13
|
|
14
|
+
def paint
|
15
|
+
self
|
16
|
+
end
|
17
|
+
|
18
|
+
def eql?(other)
|
19
|
+
self.class == other.class && self == other
|
20
|
+
end
|
21
|
+
|
22
|
+
def ==(other)
|
23
|
+
to_hex == other.to_hex
|
24
|
+
end
|
25
|
+
|
14
26
|
def complement
|
15
|
-
hsl =
|
27
|
+
hsl = self.hsl
|
16
28
|
hsl.h = (hsl.h + 180) % 360
|
17
29
|
Color.new(hsl)
|
18
30
|
end
|
@@ -2,7 +2,7 @@ module Chroma
|
|
2
2
|
class Color
|
3
3
|
module Modifiers
|
4
4
|
def lighten(amount = 10)
|
5
|
-
hsl =
|
5
|
+
hsl = self.hsl
|
6
6
|
hsl.l = clamp01(hsl.l + amount / 100.0)
|
7
7
|
self.class.new(hsl, @format)
|
8
8
|
end
|
@@ -20,19 +20,19 @@ module Chroma
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def darken(amount = 10)
|
23
|
-
hsl =
|
23
|
+
hsl = self.hsl
|
24
24
|
hsl.l = clamp01(hsl.l - amount / 100.0)
|
25
25
|
self.class.new(hsl, @format)
|
26
26
|
end
|
27
27
|
|
28
28
|
def desaturate(amount = 10)
|
29
|
-
hsl =
|
29
|
+
hsl = self.hsl
|
30
30
|
hsl.s = clamp01(hsl.s - amount / 100.0)
|
31
31
|
self.class.new(hsl, @format)
|
32
32
|
end
|
33
33
|
|
34
34
|
def saturate(amount = 10)
|
35
|
-
hsl =
|
35
|
+
hsl = self.hsl
|
36
36
|
hsl.s = clamp01(hsl.s + amount / 100.0)
|
37
37
|
self.class.new(hsl, @format)
|
38
38
|
end
|
@@ -42,7 +42,7 @@ module Chroma
|
|
42
42
|
end
|
43
43
|
|
44
44
|
def spin(amount)
|
45
|
-
hsl =
|
45
|
+
hsl = self.hsl
|
46
46
|
hue = (hsl.h.round + amount) % 360
|
47
47
|
hsl.h = hue < 0 ? 360 + hue : hue
|
48
48
|
self.class.new(hsl, @format)
|
@@ -2,95 +2,96 @@ module Chroma
|
|
2
2
|
class Color
|
3
3
|
module Serializers
|
4
4
|
def to_hsv
|
5
|
-
|
6
|
-
end
|
7
|
-
|
8
|
-
def to_hsv_s
|
9
|
-
to_hs_s(:v)
|
5
|
+
to_hs(:v)
|
10
6
|
end
|
11
7
|
|
12
8
|
def to_hsl
|
13
|
-
|
14
|
-
end
|
15
|
-
|
16
|
-
def to_hsl_s
|
17
|
-
to_hs_s(:l)
|
9
|
+
to_hs(:l)
|
18
10
|
end
|
19
11
|
|
20
12
|
def to_hex(allow_3 = false)
|
21
|
-
|
22
|
-
to_2char_hex(n)
|
23
|
-
end
|
24
|
-
|
25
|
-
if allow_3 && r[0] == r[1] && g[0] == g[1] && b[0] == b[1]
|
26
|
-
return "#{r[0]}#{g[0]}#{b[0]}"
|
27
|
-
end
|
28
|
-
|
29
|
-
[r, g, b].flatten * ''
|
30
|
-
end
|
31
|
-
|
32
|
-
def to_hex_s(allow_3 = false)
|
33
|
-
"##{to_hex(allow_3)}"
|
13
|
+
"##{to_basic_hex(allow_3)}"
|
34
14
|
end
|
35
15
|
|
36
16
|
def to_hex8
|
37
|
-
|
38
|
-
to_2char_hex(alpha * 255),
|
39
|
-
to_2char_hex(@rgb.r),
|
40
|
-
to_2char_hex(@rgb.g),
|
41
|
-
to_2char_hex(@rgb.b)
|
42
|
-
].join('')
|
43
|
-
end
|
44
|
-
|
45
|
-
def to_hex8_s
|
46
|
-
"##{to_hex8}"
|
17
|
+
"##{to_basic_hex8}"
|
47
18
|
end
|
48
19
|
|
49
20
|
def to_rgb
|
50
|
-
@rgb
|
51
|
-
end
|
52
|
-
|
53
|
-
def to_rgb_s
|
54
21
|
middle = @rgb.to_a[0..2].map(&:round).join(', ')
|
55
22
|
|
56
|
-
|
23
|
+
with_alpha(:rgb, middle)
|
57
24
|
end
|
58
25
|
|
59
|
-
def to_name
|
26
|
+
def to_name(hex_for_unknown = false)
|
60
27
|
return 'transparent' if alpha.zero?
|
61
28
|
|
62
|
-
if alpha < 1 || (name = Chroma.name_from_hex(
|
63
|
-
|
29
|
+
if alpha < 1 || (name = Chroma.name_from_hex(to_basic_hex(true))).nil?
|
30
|
+
if hex_for_unknown
|
31
|
+
to_hex
|
32
|
+
else
|
33
|
+
'<unknown>'
|
34
|
+
end
|
64
35
|
else
|
65
36
|
name
|
66
37
|
end
|
67
38
|
end
|
68
39
|
|
69
|
-
alias_method :to_name_s, :to_name
|
70
|
-
|
71
40
|
def to_s(format = @format)
|
72
|
-
use_alpha = alpha < 1 && alpha >= 0 && /^hex(3|6)
|
41
|
+
use_alpha = alpha < 1 && alpha >= 0 && /^hex(3|6)?$/ =~ format
|
73
42
|
|
74
|
-
return
|
43
|
+
return to_rgb if use_alpha
|
75
44
|
|
76
45
|
case format.to_s
|
77
|
-
when 'rgb' then
|
78
|
-
when 'hex', 'hex6' then
|
79
|
-
when 'hex3' then
|
80
|
-
when 'hex8' then
|
81
|
-
when 'hsl' then
|
82
|
-
when 'hsv' then
|
83
|
-
when 'name' then to_name
|
84
|
-
else
|
46
|
+
when 'rgb' then to_rgb
|
47
|
+
when 'hex', 'hex6' then to_hex
|
48
|
+
when 'hex3' then to_hex(true)
|
49
|
+
when 'hex8' then to_hex8
|
50
|
+
when 'hsl' then to_hsl
|
51
|
+
when 'hsv' then to_hsv
|
52
|
+
when 'name' then to_name(true)
|
53
|
+
else to_hex
|
85
54
|
end
|
86
55
|
end
|
87
56
|
|
88
57
|
alias_method :inspect, :to_s
|
89
58
|
|
59
|
+
def hsv
|
60
|
+
Converters::HsvConverter.convert_rgb(@rgb)
|
61
|
+
end
|
62
|
+
|
63
|
+
def hsl
|
64
|
+
Converters::HslConverter.convert_rgb(@rgb)
|
65
|
+
end
|
66
|
+
|
67
|
+
attr_reader :rgb
|
68
|
+
|
90
69
|
private
|
91
70
|
|
92
|
-
def
|
93
|
-
|
71
|
+
def to_basic_hex(allow_3 = false)
|
72
|
+
r, g, b = [@rgb.r, @rgb.g, @rgb.b].map do |n|
|
73
|
+
to_2char_hex(n)
|
74
|
+
end
|
75
|
+
|
76
|
+
if allow_3 && r[0] == r[1] && g[0] == g[1] && b[0] == b[1]
|
77
|
+
return "#{r[0]}#{g[0]}#{b[0]}"
|
78
|
+
end
|
79
|
+
|
80
|
+
"#{[r, g, b].flatten * ''}"
|
81
|
+
end
|
82
|
+
|
83
|
+
def to_basic_hex8
|
84
|
+
[
|
85
|
+
to_2char_hex(alpha * 255),
|
86
|
+
to_2char_hex(@rgb.r),
|
87
|
+
to_2char_hex(@rgb.g),
|
88
|
+
to_2char_hex(@rgb.b)
|
89
|
+
].join('')
|
90
|
+
end
|
91
|
+
|
92
|
+
def to_hs(third)
|
93
|
+
name = "hs#{third}"
|
94
|
+
color = send(name)
|
94
95
|
|
95
96
|
h = color.h.round
|
96
97
|
s = (color.s * 100).round
|
@@ -98,12 +99,12 @@ module Chroma
|
|
98
99
|
|
99
100
|
middle = "#{h}, #{s}%, #{lv}%"
|
100
101
|
|
101
|
-
|
102
|
+
with_alpha(name, middle)
|
102
103
|
end
|
103
104
|
|
104
|
-
def
|
105
|
+
def with_alpha(mode, middle)
|
105
106
|
if alpha < 1
|
106
|
-
"#{mode}a(#{middle}, #{
|
107
|
+
"#{mode}a(#{middle}, #{rounded_alpha})"
|
107
108
|
else
|
108
109
|
"#{mode}(#{middle})"
|
109
110
|
end
|
@@ -1,6 +1,10 @@
|
|
1
1
|
module Chroma
|
2
2
|
module Converters
|
3
3
|
class RgbConverter < Base
|
4
|
+
def convert_rgb
|
5
|
+
@input
|
6
|
+
end
|
7
|
+
|
4
8
|
def convert_hsl
|
5
9
|
h, s, l = @input
|
6
10
|
|
@@ -18,7 +22,7 @@ module Chroma
|
|
18
22
|
b = hue_to_rgb(p, q, h - 1/3.0) * 255
|
19
23
|
end
|
20
24
|
|
21
|
-
ColorModes::Rgb.new(r, g, b)
|
25
|
+
ColorModes::Rgb.new(r, g, b, bound_alpha(@input.a))
|
22
26
|
end
|
23
27
|
|
24
28
|
def convert_hsv
|
@@ -39,7 +43,7 @@ module Chroma
|
|
39
43
|
g = [t, v, v, q, p, p][mod] * 255
|
40
44
|
b = [p, p, t, v, v, q][mod] * 255
|
41
45
|
|
42
|
-
ColorModes::Rgb.new(r, g, b)
|
46
|
+
ColorModes::Rgb.new(r, g, b, bound_alpha(@input.a))
|
43
47
|
end
|
44
48
|
|
45
49
|
private
|
data/lib/chroma/harmonies.rb
CHANGED
@@ -21,7 +21,7 @@ module Chroma
|
|
21
21
|
end
|
22
22
|
|
23
23
|
def analogous(results = 6, slices = 30)
|
24
|
-
hsl = @color.
|
24
|
+
hsl = @color.hsl
|
25
25
|
part = 360 / slices
|
26
26
|
hsl.h = ((hsl.h - (part * results >> 1)) + 720) % 360
|
27
27
|
|
@@ -32,7 +32,7 @@ module Chroma
|
|
32
32
|
end
|
33
33
|
|
34
34
|
def monochromatic(results = 6)
|
35
|
-
h, s, v = @color.
|
35
|
+
h, s, v = @color.hsv
|
36
36
|
modification = 1.0 / results
|
37
37
|
|
38
38
|
results.times.map do
|
@@ -45,7 +45,7 @@ module Chroma
|
|
45
45
|
private
|
46
46
|
|
47
47
|
def hsl_map(degrees)
|
48
|
-
h, s, l = @color.
|
48
|
+
h, s, l = @color.hsl
|
49
49
|
|
50
50
|
degrees.map do |deg|
|
51
51
|
Color.new(ColorModes::Hsl.new((h + deg) % 360, s, l))
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module Chroma
|
2
|
+
class PaletteBuilder
|
3
|
+
def self.build(name, &block)
|
4
|
+
new(name, &block).build
|
5
|
+
end
|
6
|
+
|
7
|
+
def initialize(name, &block)
|
8
|
+
@name = name
|
9
|
+
@block = block
|
10
|
+
end
|
11
|
+
|
12
|
+
def build
|
13
|
+
dsl = PaletteBuilderDsl.new
|
14
|
+
dsl.instance_eval(&@block)
|
15
|
+
conversions = dsl.conversions
|
16
|
+
|
17
|
+
Harmonies.send(:define_method, @name) do
|
18
|
+
conversions.map do |color_calls|
|
19
|
+
color_calls.evaluate(@color)
|
20
|
+
end.unshift(@color)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
class PaletteBuilderDsl
|
27
|
+
attr_reader :conversions
|
28
|
+
|
29
|
+
def initialize
|
30
|
+
@conversions = []
|
31
|
+
end
|
32
|
+
|
33
|
+
def method_missing(name, *args)
|
34
|
+
ColorCalls.new(name, args).tap do |color_calls|
|
35
|
+
@conversions << color_calls
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
class ColorCalls
|
40
|
+
attr_reader :name, :args
|
41
|
+
|
42
|
+
def initialize(name, args)
|
43
|
+
@calls = [[name, args]]
|
44
|
+
end
|
45
|
+
|
46
|
+
def evaluate(color)
|
47
|
+
@calls.reduce(color) do |c, (name, args)|
|
48
|
+
c.send(name, *args)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def method_missing(name, *args)
|
53
|
+
@calls << [name, args]
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
data/lib/chroma/rgb_generator.rb
CHANGED
@@ -2,12 +2,12 @@ module Chroma
|
|
2
2
|
module RgbGenerator
|
3
3
|
class FromHsl < Base
|
4
4
|
def initialize(format, hsl)
|
5
|
-
@format = format
|
5
|
+
@format = format
|
6
6
|
@hsl = hsl
|
7
7
|
end
|
8
8
|
|
9
9
|
def generate
|
10
|
-
|
10
|
+
FromHslValues.new(@format, *@hsl.to_a).generate
|
11
11
|
end
|
12
12
|
end
|
13
13
|
end
|
@@ -5,12 +5,12 @@ module Chroma
|
|
5
5
|
s = to_percentage(s)
|
6
6
|
l = to_percentage(l)
|
7
7
|
|
8
|
-
@format = format
|
8
|
+
@format = format || :hsl
|
9
9
|
@hsl = ColorModes::Hsl.new(h, s, l, a)
|
10
10
|
end
|
11
11
|
|
12
12
|
def generate
|
13
|
-
|
13
|
+
[Converters::RgbConverter.convert_hsl(@hsl), @format]
|
14
14
|
end
|
15
15
|
end
|
16
16
|
end
|
@@ -2,12 +2,12 @@ module Chroma
|
|
2
2
|
module RgbGenerator
|
3
3
|
class FromHsv < Base
|
4
4
|
def initialize(format, hsv)
|
5
|
-
@format = format
|
5
|
+
@format = format
|
6
6
|
@hsv = hsv
|
7
7
|
end
|
8
8
|
|
9
9
|
def generate
|
10
|
-
|
10
|
+
FromHsvValues.new(@format, *@hsv.to_a).generate
|
11
11
|
end
|
12
12
|
end
|
13
13
|
end
|
@@ -5,12 +5,12 @@ module Chroma
|
|
5
5
|
s = to_percentage(s)
|
6
6
|
v = to_percentage(v)
|
7
7
|
|
8
|
-
@format = format
|
8
|
+
@format = format || :hsv
|
9
9
|
@hsv = ColorModes::Hsv.new(h, s, v, a)
|
10
10
|
end
|
11
11
|
|
12
12
|
def generate
|
13
|
-
|
13
|
+
[Converters::RgbConverter.convert_hsv(@hsv), @format]
|
14
14
|
end
|
15
15
|
end
|
16
16
|
end
|
data/lib/chroma/version.rb
CHANGED
@@ -0,0 +1,33 @@
|
|
1
|
+
describe Chroma, '.define_palette' do
|
2
|
+
def add_palette
|
3
|
+
Chroma.define_palette :foo do
|
4
|
+
spin 60
|
5
|
+
spin 180
|
6
|
+
spin(60).brighten(20)
|
7
|
+
greyscale
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def remove_palette
|
12
|
+
if Chroma::Harmonies.method_defined? :foo
|
13
|
+
Chroma::Harmonies.send(:remove_method, :foo)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
after(:example) { remove_palette }
|
18
|
+
|
19
|
+
let(:red) { '#ff0000'.paint }
|
20
|
+
|
21
|
+
it 'adds the new palette method' do
|
22
|
+
expect(red.palette).to_not respond_to(:foo)
|
23
|
+
add_palette
|
24
|
+
expect(red.palette).to respond_to(:foo)
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'generates the correct colors' do
|
28
|
+
add_palette
|
29
|
+
|
30
|
+
expect(red.palette.foo).
|
31
|
+
to generate_palette %w(#ff0000 #ffff00 #00ffff #ffff33 #808080)
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Chroma do
|
4
|
+
describe '.paint' do
|
5
|
+
context 'with named color' do
|
6
|
+
it 'creates a color' do
|
7
|
+
expect(Chroma.paint('red')).to be_a(Chroma::Color)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
context 'with 6 character hexadecimal' do
|
12
|
+
it 'creates a color' do
|
13
|
+
expect(Chroma.paint('#ff0000')).to be_a(Chroma::Color)
|
14
|
+
expect(Chroma.paint('ff0000')).to be_a(Chroma::Color)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'with 3 character hexadecimal' do
|
19
|
+
it 'creates a color' do
|
20
|
+
expect(Chroma.paint('#f00')).to be_a(Chroma::Color)
|
21
|
+
expect(Chroma.paint('f00')).to be_a(Chroma::Color)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'with 8 character hexadecimal' do
|
26
|
+
let(:hex) { '#80ff0000' }
|
27
|
+
|
28
|
+
it 'creates a color' do
|
29
|
+
expect(Chroma.paint(hex)).to be_a(Chroma::Color)
|
30
|
+
expect(Chroma.paint(hex[1..-1])).to be_a(Chroma::Color)
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'sets alpha' do
|
34
|
+
expect(Chroma.paint(hex).alpha).to be_within(0.1).of(0.5)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context 'with rgb' do
|
39
|
+
it 'creates a color' do
|
40
|
+
expect(Chroma.paint('rgb(255, 0, 0)')).to be_a(Chroma::Color)
|
41
|
+
expect(Chroma.paint('rgba(255, 0, 0, 0.5)')).to be_a(Chroma::Color)
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'sets alpha' do
|
45
|
+
expect(Chroma.paint('rgba(255, 0, 0, 0.5)').alpha).to eq(0.5)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context 'with hsl' do
|
50
|
+
it 'creates a color' do
|
51
|
+
expect(Chroma.paint('hsl(120, 100%, 50%)')).to be_a(Chroma::Color)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,152 @@
|
|
1
|
+
describe Chroma::Color do
|
2
|
+
context 'with serializers' do
|
3
|
+
let(:green) { 'green'.paint }
|
4
|
+
let(:blue) { 'rgba(0, 0, 255, 0.5)'.paint }
|
5
|
+
let(:transparent) { 'rgba(0, 0, 0, 0)'.paint }
|
6
|
+
|
7
|
+
describe '#to_hsv' do
|
8
|
+
it 'returns the hsv string' do
|
9
|
+
expect(green.to_hsv).to eq('hsv(120, 100%, 50%)')
|
10
|
+
expect(blue.to_hsv).to eq('hsva(240, 100%, 100%, 0.5)')
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe '#to_hsl' do
|
15
|
+
it 'returns the hsl string' do
|
16
|
+
expect(green.to_hsl).to eq('hsl(120, 100%, 25%)')
|
17
|
+
expect(blue.to_hsl).to eq('hsla(240, 100%, 50%, 0.5)')
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe '#to_hex' do
|
22
|
+
context 'with allow_3 set to false' do
|
23
|
+
it 'returns the hex string' do
|
24
|
+
expect(green.to_hex).to eq('#008000')
|
25
|
+
expect(blue.to_hex).to eq('#0000ff')
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'with allow_3 set to true' do
|
30
|
+
it 'returns the hex string' do
|
31
|
+
expect(green.to_hex(true)).to eq('#008000')
|
32
|
+
expect(blue.to_hex(true)).to eq('#00f')
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe '#to_hex8' do
|
38
|
+
it 'returns the hex8 string' do
|
39
|
+
expect(green.to_hex8).to eq('#ff008000')
|
40
|
+
expect(blue.to_hex8).to eq('#800000ff')
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe '#to_rgb' do
|
45
|
+
it 'returns the rgb string' do
|
46
|
+
expect(green.to_rgb).to eq('rgb(0, 128, 0)')
|
47
|
+
expect(blue.to_rgb).to eq('rgba(0, 0, 255, 0.5)')
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe '#to_name' do
|
52
|
+
context 'with hex_for_unknown set to false' do
|
53
|
+
context 'with known named color' do
|
54
|
+
context 'when alpha = 1' do
|
55
|
+
it 'returns the named color' do
|
56
|
+
expect(green.to_name).to eq('green')
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
context 'when alpha < 1' do
|
61
|
+
it 'returns "<unknown>"' do
|
62
|
+
expect(blue.to_name).to eq('<unknown>')
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
context 'when alpha = 0' do
|
68
|
+
it 'returns "transparent"' do
|
69
|
+
expect(transparent.to_name).to eq('transparent')
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
context 'with unknown named color' do
|
74
|
+
it 'returns "<unknown>"' do
|
75
|
+
expect('#123'.paint.to_name).to eq('<unknown>')
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
context 'with hex_for_unknown set to true' do
|
81
|
+
context 'with known named color' do
|
82
|
+
context 'when alpha = 1' do
|
83
|
+
it 'returns the named color' do
|
84
|
+
expect(green.to_name(true)).to eq('green')
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
context 'when alpha < 1' do
|
89
|
+
it 'returns the hex string' do
|
90
|
+
expect(blue.to_name(true)).to eq('#0000ff')
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
context 'when alpha = 0' do
|
96
|
+
it 'returns "transparent"' do
|
97
|
+
expect(transparent.to_name(true)).to eq('transparent')
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
context 'with unknown named color' do
|
102
|
+
it 'returns returns the hex string' do
|
103
|
+
expect('#123'.paint.to_name(true)).to eq('#112233')
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
describe '#to_s' do
|
110
|
+
it 'returns the appropriate string according to format' do
|
111
|
+
expect('#ff0000'.paint.to_s).to eq('#ff0000')
|
112
|
+
expect('#f00'.paint.to_s).to eq('#f00')
|
113
|
+
expect('#80ff0000'.paint.to_s).to eq('#80ff0000')
|
114
|
+
expect('hsl(120, 100%, 50%)'.paint.to_s).to eq('hsl(120, 100%, 50%)')
|
115
|
+
expect('hsla(120, 100%, 50%, 0.5)'.paint.to_s).to eq('hsla(120, 100%, 50%, 0.5)')
|
116
|
+
expect('hsv(120, 100%, 50%)'.paint.to_s).to eq('hsv(120, 100%, 50%)')
|
117
|
+
expect('hsva(120, 100%, 50%, 0.5)'.paint.to_s).to eq('hsva(120, 100%, 50%, 0.5)')
|
118
|
+
expect('red'.paint.to_s).to eq('red')
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
describe '#hsv' do
|
123
|
+
it 'returns an hsv instance' do
|
124
|
+
hsv = green.hsv
|
125
|
+
|
126
|
+
expect(hsv).to be_a(Chroma::ColorModes::Hsv)
|
127
|
+
expect(hsv.h).to be_within(0.01).of(120)
|
128
|
+
expect(hsv.s).to be_within(0.01).of(1)
|
129
|
+
expect(hsv.v).to be_within(0.01).of(0.5)
|
130
|
+
expect(hsv.a).to eq(1)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
describe '#hsl' do
|
135
|
+
it 'returns an hsl instance' do
|
136
|
+
hsl = green.hsl
|
137
|
+
|
138
|
+
expect(hsl).to be_a(Chroma::ColorModes::Hsl)
|
139
|
+
expect(hsl.h).to be_within(0.01).of(120)
|
140
|
+
expect(hsl.s).to be_within(0.01).of(1)
|
141
|
+
expect(hsl.l).to be_within(0.01).of(0.25)
|
142
|
+
expect(hsl.a).to eq(1)
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
describe '#rgb' do
|
147
|
+
it 'returns the underlying @rgb iv' do
|
148
|
+
expect(green.rgb).to equal(green.instance_variable_get(:@rgb))
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
data/spec/color_spec.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Chroma::Color do
|
4
|
+
let(:red) { 'red'.paint }
|
5
|
+
let(:other_red) { '#f00'.paint }
|
6
|
+
let(:blue) { 'blue'.paint }
|
7
|
+
|
8
|
+
context 'with equality' do
|
9
|
+
it 'equals itself' do
|
10
|
+
expect(red).to eql(red)
|
11
|
+
expect(red).to eq(red)
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'equals another instance of the same color' do
|
15
|
+
expect(red).to eql(other_red)
|
16
|
+
expect(red).to eq(other_red)
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'does not equal another instance of a different color' do
|
20
|
+
expect(red).to_not eql(blue)
|
21
|
+
expect(red).to_not eq(blue)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '#paint' do
|
26
|
+
it 'returns itself' do
|
27
|
+
expect(red.paint).to equal(red)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'chroma'
|
2
|
+
|
3
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
4
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
5
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause this
|
6
|
+
# file to always be loaded, without a need to explicitly require it in any files.
|
7
|
+
#
|
8
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
9
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
10
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
11
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
12
|
+
# a separate helper file that requires the additional dependencies and performs
|
13
|
+
# the additional setup, and require it from the spec files that actually need it.
|
14
|
+
#
|
15
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
16
|
+
# users commonly want.
|
17
|
+
#
|
18
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
19
|
+
RSpec.configure do |config|
|
20
|
+
# rspec-expectations config goes here. You can use an alternate
|
21
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
22
|
+
# assertions if you prefer.
|
23
|
+
config.expect_with :rspec do |expectations|
|
24
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
25
|
+
# and `failure_message` of custom matchers include text for helper methods
|
26
|
+
# defined using `chain`, e.g.:
|
27
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
28
|
+
# # => "be bigger than 2 and smaller than 4"
|
29
|
+
# ...rather than:
|
30
|
+
# # => "be bigger than 2"
|
31
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
32
|
+
end
|
33
|
+
|
34
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
35
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
36
|
+
config.mock_with :rspec do |mocks|
|
37
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
38
|
+
# a real object. This is generally recommended, and will default to
|
39
|
+
# `true` in RSpec 4.
|
40
|
+
mocks.verify_partial_doubles = true
|
41
|
+
end
|
42
|
+
|
43
|
+
# The settings below are suggested to provide a good initial experience
|
44
|
+
# with RSpec, but feel free to customize to your heart's content.
|
45
|
+
=begin
|
46
|
+
# These two settings work together to allow you to limit a spec run
|
47
|
+
# to individual examples or groups you care about by tagging them with
|
48
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
49
|
+
# get run.
|
50
|
+
config.filter_run :focus
|
51
|
+
config.run_all_when_everything_filtered = true
|
52
|
+
|
53
|
+
# Limits the available syntax to the non-monkey patched syntax that is recommended.
|
54
|
+
# For more details, see:
|
55
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
56
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
57
|
+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
58
|
+
config.disable_monkey_patching!
|
59
|
+
|
60
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
61
|
+
# be too noisy due to issues in dependencies.
|
62
|
+
config.warnings = true
|
63
|
+
|
64
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
65
|
+
# file, and it's useful to allow more verbose output when running an
|
66
|
+
# individual spec file.
|
67
|
+
if config.files_to_run.one?
|
68
|
+
# Use the documentation formatter for detailed output,
|
69
|
+
# unless a formatter has already been configured
|
70
|
+
# (e.g. via a command-line flag).
|
71
|
+
config.default_formatter = 'doc'
|
72
|
+
end
|
73
|
+
|
74
|
+
# Print the 10 slowest examples and example groups at the
|
75
|
+
# end of the spec run, to help surface which specs are running
|
76
|
+
# particularly slow.
|
77
|
+
config.profile_examples = 10
|
78
|
+
|
79
|
+
# Run specs in random order to surface order dependencies. If you find an
|
80
|
+
# order dependency and want to debug it, you can fix the order by providing
|
81
|
+
# the seed, which is printed after each run.
|
82
|
+
# --seed 1234
|
83
|
+
config.order = :random
|
84
|
+
|
85
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
86
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
87
|
+
# test failures related to randomization by passing the same `--seed` value
|
88
|
+
# as the one that triggered the failure.
|
89
|
+
Kernel.srand config.seed
|
90
|
+
=end
|
91
|
+
end
|
92
|
+
|
93
|
+
RSpec::Matchers.define :generate_palette do |expected|
|
94
|
+
expected.map!(&:paint)
|
95
|
+
|
96
|
+
match do |actual|
|
97
|
+
actual == expected
|
98
|
+
end
|
99
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chroma
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.1.alpha.
|
4
|
+
version: 0.0.1.alpha.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeremy Fairbank
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-01-
|
11
|
+
date: 2015-01-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 3.1.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.1.0
|
41
55
|
description: Chroma is a color manipulation and palette generation gem.
|
42
56
|
email:
|
43
57
|
- elpapapollo@gmail.com
|
@@ -46,6 +60,8 @@ extensions: []
|
|
46
60
|
extra_rdoc_files: []
|
47
61
|
files:
|
48
62
|
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- CHANGELOG.md
|
49
65
|
- Gemfile
|
50
66
|
- LICENSE
|
51
67
|
- README.md
|
@@ -64,6 +80,7 @@ files:
|
|
64
80
|
- lib/chroma/extensions/string.rb
|
65
81
|
- lib/chroma/harmonies.rb
|
66
82
|
- lib/chroma/helpers/bounders.rb
|
83
|
+
- lib/chroma/palette_builder.rb
|
67
84
|
- lib/chroma/rgb_generator.rb
|
68
85
|
- lib/chroma/rgb_generator/base.rb
|
69
86
|
- lib/chroma/rgb_generator/from_hex_string_values.rb
|
@@ -76,6 +93,12 @@ files:
|
|
76
93
|
- lib/chroma/rgb_generator/from_string.rb
|
77
94
|
- lib/chroma/version.rb
|
78
95
|
- lib/support/named_colors.yml
|
96
|
+
- spec/chroma/define_palette_spec.rb
|
97
|
+
- spec/chroma/paint_spec.rb
|
98
|
+
- spec/color/modifiers_spec.rb
|
99
|
+
- spec/color/serializers_spec.rb
|
100
|
+
- spec/color_spec.rb
|
101
|
+
- spec/spec_helper.rb
|
79
102
|
homepage: https://github.com/jfairbank/chroma
|
80
103
|
licenses:
|
81
104
|
- MIT
|
@@ -100,4 +123,10 @@ rubygems_version: 2.4.5
|
|
100
123
|
signing_key:
|
101
124
|
specification_version: 4
|
102
125
|
summary: Color manipulation and palette generation.
|
103
|
-
test_files:
|
126
|
+
test_files:
|
127
|
+
- spec/chroma/define_palette_spec.rb
|
128
|
+
- spec/chroma/paint_spec.rb
|
129
|
+
- spec/color/modifiers_spec.rb
|
130
|
+
- spec/color/serializers_spec.rb
|
131
|
+
- spec/color_spec.rb
|
132
|
+
- spec/spec_helper.rb
|