color 1.7.1 → 2.0.0.pre.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. checksums.yaml +5 -13
  2. data/CHANGELOG.md +298 -0
  3. data/CODE_OF_CONDUCT.md +128 -0
  4. data/CONTRIBUTING.md +70 -0
  5. data/CONTRIBUTORS.md +10 -0
  6. data/LICENCE.md +27 -0
  7. data/Manifest.txt +11 -21
  8. data/README.md +54 -0
  9. data/Rakefile +74 -53
  10. data/SECURITY.md +34 -0
  11. data/lib/color/cielab.rb +348 -0
  12. data/lib/color/cmyk.rb +279 -213
  13. data/lib/color/grayscale.rb +128 -160
  14. data/lib/color/hsl.rb +205 -173
  15. data/lib/color/rgb/colors.rb +177 -163
  16. data/lib/color/rgb.rb +534 -537
  17. data/lib/color/version.rb +5 -0
  18. data/lib/color/xyz.rb +214 -0
  19. data/lib/color/yiq.rb +91 -46
  20. data/lib/color.rb +208 -141
  21. data/test/fixtures/cielab.json +444 -0
  22. data/test/minitest_helper.rb +20 -4
  23. data/test/test_cmyk.rb +49 -71
  24. data/test/test_color.rb +58 -106
  25. data/test/test_grayscale.rb +35 -56
  26. data/test/test_hsl.rb +72 -76
  27. data/test/test_rgb.rb +195 -267
  28. data/test/test_yiq.rb +12 -30
  29. metadata +165 -150
  30. checksums.yaml.gz.sig +0 -0
  31. data/.autotest +0 -5
  32. data/.gemtest +0 -0
  33. data/.hoerc +0 -2
  34. data/.minitest.rb +0 -2
  35. data/.travis.yml +0 -35
  36. data/Contributing.rdoc +0 -60
  37. data/Gemfile +0 -9
  38. data/History.rdoc +0 -172
  39. data/Licence.rdoc +0 -27
  40. data/README.rdoc +0 -50
  41. data/lib/color/css.rb +0 -7
  42. data/lib/color/palette/adobecolor.rb +0 -260
  43. data/lib/color/palette/gimp.rb +0 -104
  44. data/lib/color/palette/monocontrast.rb +0 -164
  45. data/lib/color/palette.rb +0 -4
  46. data/lib/color/rgb/contrast.rb +0 -57
  47. data/lib/color/rgb/metallic.rb +0 -28
  48. data/test/test_adobecolor.rb +0 -405
  49. data/test/test_css.rb +0 -19
  50. data/test/test_gimp.rb +0 -87
  51. data/test/test_monocontrast.rb +0 -130
  52. data.tar.gz.sig +0 -0
  53. metadata.gz.sig +0 -0
@@ -1,197 +1,165 @@
1
- # A colour object representing shades of grey. Used primarily in PDF
2
- # document creation.
3
- class Color::GrayScale
1
+ # frozen_string_literal: true
2
+
3
+ ##
4
+ # \Grayscale is a color object representing shades of gray as a ratio of black to white,
5
+ # where 0% (0.0) gray is black and 100% (1.0) gray is white.
6
+ #
7
+ # \Grayscale colors are immutable Data class instances. Array deconstruction is `[gray]`
8
+ # and hash deconstruction is `{g:, gray:}`. See #g, #gray.
9
+ class Color::Grayscale
4
10
  include Color
5
11
 
6
- # The format of a DeviceGrey colour for PDF. In color-tools 2.0 this will
7
- # be removed from this package and added back as a modification by the
8
- # PDF::Writer package.
9
- PDF_FORMAT_STR = "%.3f %s"
12
+ ##
13
+ # :attr_reader: brightness
14
+ # Returns the grayscale value as a proportion of white (0.0 .. 1.0).
10
15
 
11
- class << self
12
- # Creates a greyscale colour object from fractional values 0..1.
13
- #
14
- # Color::GreyScale.from_fraction(0.5)
15
- def from_fraction(g = 0, &block)
16
- new(g, 1.0, &block)
17
- end
16
+ ##
17
+ # :attr_reader: g
18
+ # Returns the grayscale value as a proportion of white (0.0 .. 1.0).
18
19
 
19
- # Creates a greyscale colour object from percentages 0..100.
20
- #
21
- # Color::GrayScale.from_percent(50)
22
- def from_percent(g = 0, &block)
23
- new(g, &block)
24
- end
25
- end
20
+ ##
21
+ # :attr_reader: gray
22
+ # Returns the grayscale value as a percentage of white (0.0 .. 100.0).
26
23
 
27
- # Creates a greyscale colour object from percentages 0..100.
24
+ ##
25
+ # Creates a grayscale color object from a percentage value (0.0 .. 100.0).
26
+ #
27
+ # ```ruby
28
+ # Color::Grayscale.from_percentage(50) # => Grayscale [0.50%]
29
+ # Color::Grayscale.from_values(50) # => Grayscale [0.50%]
30
+ # ```
28
31
  #
29
- # Color::GrayScale.new(50)
30
- def initialize(g = 0, radix = 100.0, &block) # :yields self:
31
- @g = Color.normalize(g / radix)
32
- block.call if block
32
+ # :call-seq:
33
+ # from_percentage(g)
34
+ # from_percentage(g:)
35
+ # from_values(g)
36
+ # from_values(g:)
37
+ def self.from_percentage(*args, **kwargs)
38
+ g =
39
+ case [args, kwargs]
40
+ in [[g], {}]
41
+ g
42
+ in [[], {g:}]
43
+ g
44
+ else
45
+ new(*args, **kwargs)
46
+ end
47
+
48
+ new(g: g / 100.0)
33
49
  end
34
50
 
35
- # Coerces the other Color object to grayscale.
36
- def coerce(other)
37
- other.to_grayscale
51
+ class << self
52
+ alias_method :from_values, :from_percentage
53
+ alias_method :from_fraction, :new
54
+ alias_method :from_internal, :from_fraction # :nodoc:
38
55
  end
39
56
 
40
- # Present the colour as a DeviceGrey fill colour string for PDF. This will
41
- # be removed from the default package in color-tools 2.0.
42
- def pdf_fill
43
- PDF_FORMAT_STR % [ @g, "g" ]
57
+ ##
58
+ # Creates a grayscale color object from a fractional value (0.0 .. 1.0).
59
+ #
60
+ # ```ruby
61
+ # Color::Grayscale.from_fraction(0.5)
62
+ # Color::Grayscale.new(0.5)
63
+ # Color::Grayscale[g: 0.5]
64
+ # ```
65
+ def initialize(g:)
66
+ super(g: normalize(g))
44
67
  end
45
68
 
46
- # Present the colour as a DeviceGrey stroke colour string for PDF. This
47
- # will be removed from the default package in color-tools 2.0.
48
- def pdf_stroke
49
- PDF_FORMAT_STR % [ @g, "G" ]
50
- end
69
+ ##
70
+ # Coerces the other Color object to grayscale.
71
+ def coerce(other) = other.to_grayscale
51
72
 
52
- def to_255
53
- [(@g * 255).round, 255].min
54
- end
55
- private :to_255
73
+ ##
74
+ # Convert \Grayscale to Color::CMYK.
75
+ def to_cmyk(...) = Color::CMYK.from_fraction(0, 0, 0, 1.0 - g.to_f)
56
76
 
57
- # Present the colour as an HTML/CSS colour string.
58
- def html
59
- gs = "%02x" % to_255
60
- "##{gs * 3}"
61
- end
77
+ ##
78
+ # Convert \Grayscale to Color::RGB.
79
+ def to_rgb(...) = Color::RGB.from_fraction(g, g, g)
62
80
 
63
- # Present the colour as an RGB HTML/CSS colour string (e.g., "rgb(0%, 50%,
64
- # 100%)").
65
- def css_rgb
66
- "rgb(%3.2f%%, %3.2f%%, %3.2f%%)" % [ gray, gray, gray ]
67
- end
81
+ ##
82
+ def to_grayscale(...) = self
68
83
 
69
- # Present the colour as an RGBA (with alpha) HTML/CSS colour string (e.g.,
70
- # "rgb(0%, 50%, 100%, 1)").
71
- def css_rgba
72
- "rgba(%3.2f%%, %3.2f%%, %3.2f%%, %1.2f)" % [ gray, gray, gray, 1 ]
84
+ ##
85
+ # Convert \Grayscale to Color::YIQ.
86
+ #
87
+ # This approximates the actual value, as I and Q are calculated by treating the
88
+ # grayscale value as a RGB value. The Y (intensity or brightness) value is the same as
89
+ # the grayscale value.
90
+ def to_yiq(...)
91
+ y = g
92
+ i = (g * 0.596) + (g * -0.275) + (g * -0.321)
93
+ q = (g * 0.212) + (g * -0.523) + (g * 0.311)
94
+ Color::YIQ.from_fraction(y, i, q)
73
95
  end
74
96
 
75
- # Present the colour as an HSL HTML/CSS colour string (e.g., "hsl(180,
76
- # 25%, 35%)"). Note that this will perform a #to_hsl operation.
77
- def css_hsl
78
- to_hsl.css_hsl
79
- end
97
+ ##
98
+ # Converts \Grayscale to Color::HSL.
99
+ def to_hsl(...) = Color::HSL.from_fraction(0, 0, g)
80
100
 
81
- # Present the colour as an HSLA (with alpha) HTML/CSS colour string (e.g.,
82
- # "hsla(180, 25%, 35%, 1)"). Note that this will perform a #to_hsl
83
- # operation.
84
- def css_hsla
85
- to_hsl.css_hsla
86
- end
101
+ ##
102
+ # Converts \Grayscale to Color::CIELAB via Color::RGB.
103
+ def to_lab(...) = to_rgb(...).to_lab(...)
87
104
 
88
- # Convert the greyscale colour to CMYK.
89
- def to_cmyk
90
- k = 1.0 - @g.to_f
91
- Color::CMYK.from_fraction(0, 0, 0, k)
105
+ ##
106
+ # Present the color as an HTML/CSS color string (e.g., `#dddddd`).
107
+ def html
108
+ "##{("%02x" % translate_range(g, to: 0.0..255.0)) * 3}"
92
109
  end
93
110
 
94
- # Convert the greyscale colour to RGB.
95
- def to_rgb(ignored = true)
96
- Color::RGB.from_fraction(g, g, g)
97
- end
111
+ ##
112
+ # Present the color as a CSS `rgb` color with optional `alpha`.
113
+ #
114
+ # ```ruby
115
+ # Color::Grayscale[0.5].css # => rgb(50.00% 50.00% 50.00%)
116
+ # Color::Grayscale[0.5].css(alpha: 0.75) # => rgb(50.00% 50.00% 50.00% / 0.75)
117
+ # ```
118
+ def css(alpha: nil)
119
+ params = ([css_value(gray, :percent)] * 3).join(" ")
120
+ params = "#{params} / #{css_value(alpha)}" if alpha
98
121
 
99
- # Reflexive conversion.
100
- def to_grayscale
101
- self
122
+ "rgb(#{params})"
102
123
  end
103
- alias to_greyscale to_grayscale
104
124
 
105
- # Lightens the greyscale colour by the stated percent.
106
- def lighten_by(percent)
107
- g = [@g + (@g * (percent / 100.0)), 1.0].min
108
- Color::GrayScale.from_fraction(g)
109
- end
125
+ ##
126
+ # Lightens the grayscale color by the stated percent.
127
+ def lighten_by(percent) = Color::Grayscale.from_fraction([g + (g * (percent / 100.0)), 1.0].min)
110
128
 
111
- # Darken the greyscale colour by the stated percent.
112
- def darken_by(percent)
113
- g = [@g - (@g * (percent / 100.0)), 0.0].max
114
- Color::GrayScale.from_fraction(g)
115
- end
129
+ ##
130
+ # Darken the grayscale color by the stated percent.
131
+ def darken_by(percent) = Color::Grayscale.from_fraction([g - (g * (percent / 100.0)), 0.0].max)
116
132
 
117
- # Returns the YIQ (NTSC) colour encoding of the greyscale value. This is
118
- # an approximation, as the values for I and Q are calculated by treating
119
- # the greyscale value as an RGB value. The Y (intensity or brightness)
120
- # value is the same as the greyscale value.
121
- def to_yiq
122
- y = @g
123
- i = (@g * 0.596) + (@g * -0.275) + (@g * -0.321)
124
- q = (@g * 0.212) + (@g * -0.523) + (@g * 0.311)
125
- Color::YIQ.from_fraction(y, i, q)
126
- end
133
+ ##
134
+ alias_method :brightness, :g
127
135
 
128
- # Returns the HSL colour encoding of the greyscale value.
129
- def to_hsl
130
- Color::HSL.from_fraction(0, 0, @g)
131
- end
136
+ ##
137
+ def gray = g * 100.0
132
138
 
133
- # Returns the brightness value for this greyscale value; this is the
134
- # greyscale value itself.
135
- def brightness
136
- @g
137
- end
139
+ ##
140
+ def inspect = "Grayscale [%.2f%%]" % [gray] # :nodoc:
138
141
 
139
- # Returns the grayscale value as a percentage of white (100% gray is
140
- # white).
141
- def gray
142
- @g * 100.0
143
- end
144
- alias grey gray
145
- # Returns the grayscale value as a fractional value of white in the range
146
- # 0.0 .. 1.0.
147
- def g
148
- @g
149
- end
150
- # Sets the grayscale value as a percentage of white.
151
- def gray=(gg)
152
- @g = Color.normalize(gg / 100.0)
153
- end
154
- alias grey= gray= ;
155
- # Returns the grayscale value as a fractional value of white in the range
156
- # 0.0 .. 1.0.
157
- def g=(gg)
158
- @g = Color.normalize(gg)
142
+ ##
143
+ def pretty_print(q) # :nodoc:
144
+ q.text "Grayscale"
145
+ q.breakable
146
+ q.group 2, "[", "]" do
147
+ q.text "%.2f%%" % gray
148
+ end
159
149
  end
160
150
 
161
- # Adds another colour to the current colour. The other colour will be
162
- # converted to grayscale before addition. This conversion depends upon a
163
- # #to_grayscale method on the other colour.
164
- #
165
- # The addition is done using the grayscale accessor methods to ensure a
166
- # valid colour in the result.
167
- def +(other)
168
- self.class.from_fraction(g + other.to_grayscale.g)
169
- end
151
+ ##
152
+ def to_a = [gray] # :nodoc:
170
153
 
171
- # Subtracts another colour to the current colour. The other colour will be
172
- # converted to grayscale before subtraction. This conversion depends upon
173
- # a #to_grayscale method on the other colour.
174
- #
175
- # The subtraction is done using the grayscale accessor methods to ensure a
176
- # valid colour in the result.
177
- def -(other)
178
- self + (-other)
179
- end
154
+ ##
155
+ alias_method :deconstruct, :to_a
180
156
 
181
- def inspect
182
- "Gray [%.2f%%]" % [ gray ]
183
- end
157
+ ##
158
+ def deconstruct_keys(_keys) = {g:, gray:}
184
159
 
185
- def to_a
186
- [ g ]
187
- end
160
+ ##
161
+ def to_internal = [g] # :nodoc:
188
162
 
189
- def -@
190
- gs = self.dup
191
- gs.instance_variable_set(:@g, -g)
192
- gs
193
- end
163
+ ##
164
+ def components = 1 # :nodoc:
194
165
  end
195
-
196
- # A synonym for Color::GrayScale.
197
- Color::GreyScale = Color::GrayScale