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
data/lib/color/cmyk.rb CHANGED
@@ -1,263 +1,329 @@
1
- # An CMYK colour object. CMYK (cyan, magenta, yellow, and black) colours are
2
- # based on additive percentages of ink. A CMYK colour of (0.3, 0, 0.8, 0.3)
3
- # would be mixed from 30% cyan, 0% magenta, 80% yellow, and 30% black.
4
- # Primarily used in four-colour printing processes.
1
+ # frozen_string_literal: true
2
+
3
+ ##
4
+ # The \CMYK color model is a subtractive color model based on additive percentages of
5
+ # colored inks: cyan, magenta, yellow, and key (most often black).
6
+ #
7
+ # \CMYK [30% 0% 80% 30%] would be mixed from 30% cyan, 0% magenta, 80% yellow, and 30%
8
+ # black.
9
+ #
10
+ # \CMYK colors are immutable Data class instances. Array deconstruction is `[cyan,
11
+ # magenta, yellow, key]` and hash deconstruction is `{c:, cyan:, m:, magenta:, y:, yellow:
12
+ # k:, key:}`. See #c, #cyan, #m, #magenta, #y, #yellow, #k, #key.
5
13
  class Color::CMYK
6
14
  include Color
7
15
 
8
- # The format of a DeviceCMYK colour for PDF. In color-tools 2.0 this will
9
- # be removed from this package and added back as a modification by the
10
- # PDF::Writer package.
11
- PDF_FORMAT_STR = "%.3f %.3f %.3f %.3f %s"
16
+ ##
17
+ # :attr_reader: c
18
+ # Returns the cyan (`C`) component as a value 0.0 .. 1.0.
12
19
 
13
- # Coerces the other Color object into CMYK.
14
- def coerce(other)
15
- other.to_cmyk
16
- end
20
+ ##
21
+ # :attr_reader: cyan
22
+ # Returns the cyan (`C`) component as a percentage value (0.0 .. 100.0).
17
23
 
18
- class << self
19
- # Creates a CMYK colour object from fractional values 0..1.
20
- #
21
- # Color::CMYK.from_fraction(0.3, 0, 0.8, 0.3)
22
- def from_fraction(c = 0, m = 0, y = 0, k = 0, &block)
23
- new(c, m, y, k, 1.0, &block)
24
- end
24
+ ##
25
+ # :attr_reader: m
26
+ # Returns the magenta (`M`) component as a value 0.0 .. 1.0.
25
27
 
26
- # Creates a CMYK colour object from percentages. Internally, the colour is
27
- # managed as fractional values 0..1.
28
- #
29
- # Color::CMYK.new(30, 0, 80, 30)
30
- def from_percent(c = 0, m = 0, y = 0, k = 0, &block)
31
- new(c, m, y, k, &block)
32
- end
33
- end
28
+ ##
29
+ # :attr_reader: magenta
30
+ # Returns the magenta (`M`) component as a percentage value (0.0 .. 100.0).
31
+
32
+ ##
33
+ # :attr_reader: y
34
+ # Returns the yellow (`Y`) component as a value 0.0 .. 1.0.
35
+
36
+ ##
37
+ # :attr_reader: yellow
38
+ # Returns the yellow (`Y`) component as a percentage value (0.0 .. 100.0).
39
+
40
+ ##
41
+ # :attr_reader: k
42
+ # Returns the key or black (`K`) component as a value 0.0 .. 1.0.
43
+
44
+ ##
45
+ # :attr_reader: b
46
+ # Returns the key or black (`K`) component as a value 0.0 .. 1.0.
34
47
 
35
- # Creates a CMYK colour object from percentages. Internally, the colour is
36
- # managed as fractional values 0..1.
48
+ ##
49
+ # :attr_reader: key
50
+ # Returns the key or black (`K`) component as a percentage value (0.0 .. 100.0).
51
+
52
+ ##
53
+ # :attr_reader: black
54
+ # Returns the key or black (`K`) component as a percentage value (0.0 .. 100.0).
55
+
56
+ ##
57
+ # Creates a CMYK color object from percentage values (0.0 .. 100.0).
37
58
  #
38
- # Color::CMYK.new(30, 0, 80, 30)
39
- def initialize(c = 0, m = 0, y = 0, k = 0, radix = 100.0, &block) # :yields self:
40
- @c, @m, @y, @k = [ c, m, y, k ].map { |v| Color.normalize(v / radix) }
41
- block.call(self) if block
42
- end
59
+ # ```ruby
60
+ # Color::CMYK.from_percentage(30, 0, 80, 30) # => CMYK [30.00% 0.00% 80.00% 30.00%]
61
+ # Color::CMYK.from_values(30, 0, 80, 30) # => CMYK [30.00% 0.00% 80.00% 30.00%]
62
+ # ```
63
+ #
64
+ # :call-seq:
65
+ # from_percentage(c, m, y, k)
66
+ # from_percentage(c:, m:, y:, k:)
67
+ # from_values(c, m, y, k)
68
+ # from_values(c:, m:, y:, k:)
69
+ def self.from_percentage(*args, **kwargs)
70
+ c, m, y, k =
71
+ case [args, kwargs]
72
+ in [[_, _, _, _], {}]
73
+ args
74
+ in [[], {c:, m:, y:, k:}]
75
+ [c, m, y, k]
76
+ else
77
+ new(*args, **kwargs)
78
+ end
43
79
 
44
- # Present the colour as a DeviceCMYK fill colour string for PDF. This will
45
- # be removed from the default package in color-tools 2.0.
46
- def pdf_fill
47
- PDF_FORMAT_STR % [ @c, @m, @y, @k, "k" ]
80
+ new(c: c / 100.0, m: m / 100.0, y: y / 100.0, k: k / 100.0)
48
81
  end
49
82
 
50
- # Present the colour as a DeviceCMYK stroke colour string for PDF. This
51
- # will be removed from the default package in color-tools 2.0.
52
- def pdf_stroke
53
- PDF_FORMAT_STR % [ @c, @m, @y, @k, "K" ]
54
- end
83
+ class << self
84
+ alias_method :from_values, :from_percentage
55
85
 
56
- # Present the colour as an RGB HTML/CSS colour string (e.g., "#aabbcc").
57
- # Note that this will perform a #to_rgb operation using the default
58
- # conversion formula.
59
- def html
60
- to_rgb.html
86
+ alias_method :from_fraction, :new
87
+ alias_method :from_internal, :new # :nodoc:
61
88
  end
62
89
 
63
- # Present the colour as an RGB HTML/CSS colour string (e.g., "rgb(0%, 50%,
64
- # 100%)"). Note that this will perform a #to_rgb operation using the
65
- # default conversion formula.
66
- def css_rgb
67
- to_rgb.css_rgb
90
+ ##
91
+ # Creates a CMYK color object from fractional values (0.0 .. 1.0).
92
+ #
93
+ # ```ruby
94
+ # Color::CMYK.from_fraction(0.3, 0, 0.8, 0.3) # => CMYK [30.00% 0.00% 80.00% 30.00%]
95
+ # Color::CMYK.new(0.3, 0, 0.8, 0.3) # => CMYK [30.00% 0.00% 80.00% 30.00%]
96
+ # Color::CMYK[c: 0.3, m: 0, y: 0.8, k: 0.3] # => CMYK [30.00% 0.00% 80.00% 30.00%]
97
+ # ```
98
+ def initialize(c:, m:, y:, k:)
99
+ super(c: normalize(c), m: normalize(m), y: normalize(y), k: normalize(k))
68
100
  end
69
101
 
70
- # Present the colour as an RGBA (with alpha) HTML/CSS colour string (e.g.,
71
- # "rgb(0%, 50%, 100%, 1)"). Note that this will perform a #to_rgb
72
- # operation using the default conversion formula.
73
- def css_rgba
74
- to_rgb.css_rgba
102
+ ##
103
+ # Output a CSS representation of the CMYK color using `device-cmyk()`.
104
+ #
105
+ # If an `alpha` value is provided, it will be included in the output.
106
+ #
107
+ # A `fallback` may be provided or included automatically depending on the value
108
+ # provided, which may be `true`, `false`, a Color object, or a Hash with `:color` and/or
109
+ # `:alpha` keys. The default value is `true`.
110
+ #
111
+ # When `fallback` is:
112
+ #
113
+ # - `true`: this CMYK color will be converted to RGB and this will be provided as the
114
+ # fallback color. If an `alpha` value is provided, it will be used for the fallback.
115
+ # - `false`: no fallback color will be included.
116
+ # - a Color object will be used to produce the `fallback` value.
117
+ # - a Hash will be checked for `:color` and/or `:alpha` keys:
118
+ # - if `:color` is present, it will be used for the fallback color; if not present,
119
+ # the CMYK color will be converted to RGB.
120
+ # - if `:alpha` is present, it will be used for the fallback color; if not present,
121
+ # the fallback color will be presented _without_ alpha.
122
+ #
123
+ # Examples:
124
+ #
125
+ # ```ruby
126
+ # cmyk = Color::CMYK.from_percentage(30, 0, 80, 30)
127
+ # cmyk.css
128
+ # # => device-cmyk(30.00% 0 80.00% 30.00%, rgb(49.00% 70.00% 14.00%))
129
+ #
130
+ # cmyk.css(alpha: 0.5)
131
+ # # => device-cmyk(30.00% 0 80.00% 30.00% / 0.50, rgb(49.00% 70.00% 14.00% / 0.50))
132
+ #
133
+ # cmyk.css(fallback: false)
134
+ # # => device-cmyk(30.00% 0 80.00% 30.00%)
135
+ #
136
+ # cmyk.css(alpha: 0.5, fallback: false)
137
+ # # => device-cmyk(30.00% 0 80.00% 30.00% / 0.50)
138
+ #
139
+ # cmyk.css(fallback: Color::RGB::Blue)
140
+ # # => device-cmyk(30.00% 0 80.00% 30.00%, rgb(0.00% 0.00% 100.00%))
141
+ #
142
+ # cmyk.css(alpha: 0.5, fallback: Color::RGB::Blue)
143
+ # # => device-cmyk(30.00% 0 80.00% 30.00% / 0.50, rgb(0.00% 0.00% 100.00% / 0.50))
144
+ #
145
+ # cmyk.css(alpha: 0.5, fallback: { color: Color::RGB::Blue })
146
+ # # => device-cmyk(30.00% 0 80.00% 30.00% / 0.50, rgb(0.00% 0.00% 100.00%))
147
+ #
148
+ # cmyk.css(alpha: 0.5, fallback: { color: Color::RGB::Blue, alpha: 0.3 })
149
+ # # => device-cmyk(30.00% 0 80.00% 30.00% / 0.50, rgb(0.00% 0.00% 100.00% / 0.30))
150
+ #
151
+ # cmyk.css(alpha: 0.5, fallback: { alpha: 0.3 })
152
+ # # => device-cmyk(30.00% 0 80.00% 30.00% / 0.50, rgb(49.00% 70.00% 14.00% / 0.30))
153
+ # ```
154
+ def css(alpha: nil, fallback: true)
155
+ if fallback.is_a?(Color)
156
+ device_cmyk(alpha, fallback, alpha)
157
+ elsif fallback.is_a?(Hash)
158
+ device_cmyk(alpha, fallback.fetch(:color) { to_rgb }, fallback[:alpha])
159
+ elsif fallback == true
160
+ device_cmyk(alpha, to_rgb, alpha)
161
+ else
162
+ device_cmyk(alpha, nil, nil)
163
+ end
75
164
  end
76
165
 
77
- # Present the colour as an HSL HTML/CSS colour string (e.g., "hsl(180,
78
- # 25%, 35%)"). Note that this will perform a #to_hsl operation using the
79
- # default conversion formula.
80
- def css_hsl
81
- to_hsl.css_hsl
82
- end
166
+ ##
167
+ # Coerces the other Color object into CMYK.
168
+ def coerce(other) = other.to_cmyk
169
+
170
+ ##
171
+ def to_cmyk(...) = self
83
172
 
84
- # Present the colour as an HSLA (with alpha) HTML/CSS colour string (e.g.,
85
- # "hsla(180, 25%, 35%, 1)"). Note that this will perform a #to_hsl
86
- # operation using the default conversion formula.
87
- def css_hsla
88
- to_hsl.css_hsla
173
+ ##
174
+ # Converts CMYK to Color::Grayscale.
175
+ #
176
+ # There are multiple methods for grayscale conversion, but this implements a variant of
177
+ # the Adobe PDF conversion method with higher precision:
178
+ #
179
+ # ```
180
+ # g = 1.0 - min(1.0, 0.299 * c + 0.587 * m + 0.114 * y + k)
181
+ # ```
182
+ #
183
+ # The default Adobe conversion uses lower precision conversion constants (0.3, 0.59, and
184
+ # 0.11) instead of the more precise NTSC/YIQ values.
185
+ def to_grayscale(...)
186
+ gc = 0.299 * c
187
+ gm = 0.587 * m
188
+ gy = 0.114 * y
189
+ g = 1.0 - [1.0, gc + gm + gy + k].min
190
+ Color::Grayscale.from_fraction(g)
89
191
  end
90
192
 
91
- # Converts the CMYK colour to RGB. Most colour experts strongly suggest
92
- # that this is not a good idea (some even suggesting that it's a very bad
93
- # idea). CMYK represents additive percentages of inks on white paper,
94
- # whereas RGB represents mixed colour intensities on a black screen.
193
+ ##
194
+ # Converts CMYK to Color::YIQ via Color::RGB.
195
+ def to_yiq(...) = to_rgb(...).to_yiq(...)
196
+
197
+ ##
198
+ # Converts CMYK to Color::RGB.
199
+ #
200
+ # Most color experts strongly suggest that this is not a good idea (some suggesting that
201
+ # it's a very bad idea). CMYK represents additive percentages of inks on white paper,
202
+ # whereas RGB represents mixed color intensities on an unlit (black) screen.
95
203
  #
96
- # However, the colour conversion can be done, and there are two different
97
- # methods for the conversion that provide slightly different results.
98
- # Adobe PDF conversions are done with the first form.
204
+ # The color conversion can be done and there are two different methods (standard and
205
+ # Adobe PDF) that provide slightly different results. Using CMYK [33% 66% 83% 25%], the
206
+ # standard method provides an approximate RGB color of (128, 65, 33) or #804121. The
207
+ # Adobe PDF method provides an approximate RGB color of (107, 23, 0) or #6b1700.
99
208
  #
100
- # # Adobe PDF Display Formula
209
+ # Which is correct? The colors may seem to be drastically different in the RGB color
210
+ # space, they differ mostly in intensity. The Adobe PDF conversion is a darker, slightly
211
+ # redder brown; the standard conversion is a lighter brown. Because of this subtlety,
212
+ # both methods are offered for conversion. The Adobe PDF method is not used by default;
213
+ # to use it, pass `rgb_method: :adobe` to #to_rgb.
214
+ #
215
+ # # Adobe PDF CMYK -> RGB Conversion
101
216
  # r = 1.0 - min(1.0, c + k)
102
217
  # g = 1.0 - min(1.0, m + k)
103
218
  # b = 1.0 - min(1.0, y + k)
104
219
  #
105
- # # Other
220
+ # # Standard CMYK -> RGB Conversion
106
221
  # r = 1.0 - (c * (1.0 - k) + k)
107
222
  # g = 1.0 - (m * (1.0 - k) + k)
108
223
  # b = 1.0 - (y * (1.0 - k) + k)
109
224
  #
110
- # If we have a CMYK colour of [33% 66% 83% 25%], the first method will
111
- # give an approximate RGB colour of (107, 23, 0) or #6b1700. The second
112
- # method will give an approximate RGB colour of (128, 65, 33) or #804121.
113
- # Which is correct? Although the colours may seem to be drastically
114
- # different in the RGB colour space, they are very similar colours,
115
- # differing mostly in intensity. The first is a darker, slightly redder
116
- # brown; the second is a lighter brown.
117
- #
118
- # Because of this subtlety, both methods are now offered for conversion.
119
- # The Adobe method is not used by default; to enable it, pass +true+ to
120
- # #to_rgb.
121
- #
122
- # Future versions of Color may offer other conversion mechanisms that
123
- # offer greater colour fidelity, including recognition of ICC colour
124
- # profiles.
125
- def to_rgb(use_adobe_method = false)
126
- if use_adobe_method
127
- Color::RGB.from_fraction(*adobe_cmyk_rgb)
128
- else
129
- Color::RGB.from_fraction(*standard_cmyk_rgb)
130
- end
131
- end
225
+ # :call-seq:
226
+ # to_rgb(rgb_method: :standard)
227
+ def to_rgb(*args, **kwargs)
228
+ values =
229
+ if kwargs[:rgb_method] == :adobe || args.first == :adobe
230
+ adobe_cmyk_rgb
231
+ else
232
+ standard_cmyk_rgb
233
+ end
132
234
 
133
- # Converts the CMYK colour to a single greyscale value. There are
134
- # undoubtedly multiple methods for this conversion, but only a minor
135
- # variant of the Adobe conversion method will be used:
136
- #
137
- # g = 1.0 - min(1.0, 0.299 * c + 0.587 * m + 0.114 * y + k)
138
- #
139
- # This treats the CMY values similarly to YIQ (NTSC) values and then adds
140
- # the level of black. This is a variant of the Adobe version because it
141
- # uses the more precise YIQ (NTSC) conversion values for Y (intensity)
142
- # rather than the approximates provided by Adobe (0.3, 0.59, and 0.11).
143
- def to_grayscale
144
- c = 0.299 * @c.to_f
145
- m = 0.587 * @m.to_f
146
- y = 0.114 * @y.to_f
147
- g = 1.0 - [1.0, c + m + y + @k].min
148
- Color::GrayScale.from_fraction(g)
235
+ Color::RGB.from_fraction(*values)
149
236
  end
150
- alias to_greyscale to_grayscale
151
237
 
152
- def to_cmyk
153
- self
154
- end
238
+ ##
239
+ # Converts CMYK to Color::HSL via Color::RGB.
240
+ def to_hsl(...) = to_rgb(...).to_hsl(...)
155
241
 
156
- def inspect
157
- "CMYK [%.2f%%, %.2f%%, %.2f%%, %.2f%%]" % [ cyan, magenta, yellow, black ]
158
- end
242
+ ##
243
+ # Converts CMYK to Color::CIELAB via Color::RGB.
244
+ def to_lab(...) = to_rgb(...).to_lab(...)
159
245
 
160
- # Converts to RGB then YIQ.
161
- def to_yiq
162
- to_rgb.to_yiq
163
- end
246
+ ##
247
+ # Converts CMYK to Color::XYZ via Color::RGB.
248
+ def to_xyz(...) = to_rgb(...).to_xyz(...)
164
249
 
165
- # Converts to RGB then HSL.
166
- def to_hsl
167
- to_rgb.to_hsl
168
- end
250
+ ##
251
+ def inspect = "CMYK [%.2f%% %.2f%% %.2f%% %.2f%%]" % [cyan, magenta, yellow, key] # :nodoc:
169
252
 
170
- # Returns the cyan (C) component of the CMYK colour as a percentage value.
171
- def cyan
172
- @c * 100.0
173
- end
174
- # Returns the cyan (C) component of the CMYK colour as a value in the
175
- # range 0.0 .. 1.0.
176
- def c
177
- @c
178
- end
179
- # Sets the cyan (C) component of the CMYK colour as a percentage value.
180
- def cyan=(cc)
181
- @c = Color.normalize(cc / 100.0)
182
- end
183
- # Sets the cyan (C) component of the CMYK colour as a value in the range
184
- # 0.0 .. 1.0.
185
- def c=(cc)
186
- @c = Color.normalize(cc)
253
+ ##
254
+ def pretty_print(q) # :nodoc:
255
+ q.text "CMYK"
256
+ q.breakable
257
+ q.group 2, "[", "]" do
258
+ q.text ".2f%%" % cyan
259
+ q.fill_breakable
260
+ q.text ".2f%%" % magenta
261
+ q.fill_breakable
262
+ q.text ".2f%%" % yellow
263
+ q.fill_breakable
264
+ q.text ".2f%%" % key
265
+ q.fill_breakable
266
+ end
187
267
  end
188
268
 
189
- # Returns the magenta (M) component of the CMYK colour as a percentage
190
- # value.
191
- def magenta
192
- @m * 100.0
193
- end
194
- # Returns the magenta (M) component of the CMYK colour as a value in the
195
- # range 0.0 .. 1.0.
196
- def m
197
- @m
198
- end
199
- # Sets the magenta (M) component of the CMYK colour as a percentage value.
200
- def magenta=(mm)
201
- @m = Color.normalize(mm / 100.0)
202
- end
203
- # Sets the magenta (M) component of the CMYK colour as a value in the
204
- # range 0.0 .. 1.0.
205
- def m=(mm)
206
- @m = Color.normalize(mm)
207
- end
269
+ ##
270
+ def cyan = c * 100.0 # :nodoc:
208
271
 
209
- # Returns the yellow (Y) component of the CMYK colour as a percentage
210
- # value.
211
- def yellow
212
- @y * 100.0
213
- end
214
- # Returns the yellow (Y) component of the CMYK colour as a value in the
215
- # range 0.0 .. 1.0.
216
- def y
217
- @y
218
- end
219
- # Sets the yellow (Y) component of the CMYK colour as a percentage value.
220
- def yellow=(yy)
221
- @y = Color.normalize(yy / 100.0)
222
- end
223
- # Sets the yellow (Y) component of the CMYK colour as a value in the range
224
- # 0.0 .. 1.0.
225
- def y=(kk)
226
- @y = Color.normalize(kk)
227
- end
272
+ ##
273
+ def magenta = m * 100.0 # :nodoc:
228
274
 
229
- # Returns the black (K) component of the CMYK colour as a percentage
230
- # value.
231
- def black
232
- @k * 100.0
233
- end
234
- # Returns the black (K) component of the CMYK colour as a value in the
235
- # range 0.0 .. 1.0.
236
- def k
237
- @k
238
- end
239
- # Sets the black (K) component of the CMYK colour as a percentage value.
240
- def black=(kk)
241
- @k = Color.normalize(kk / 100.0)
242
- end
243
- # Sets the black (K) component of the CMYK colour as a value in the range
244
- # 0.0 .. 1.0.
245
- def k=(kk)
246
- @k = Color.normalize(kk)
247
- end
275
+ ##
276
+ def yellow = y * 100.0 # :nodoc:
248
277
 
249
- def to_a
250
- [ c, m, y, k ]
251
- end
278
+ ##
279
+ alias_method :b, :k # :nodoc:
280
+
281
+ ##
282
+ def key = k * 100.0 # :nodoc:
283
+
284
+ ##
285
+ alias_method :black, :key # :nodoc:
286
+
287
+ ##
288
+ def to_a = [cyan, magenta, yellow, key] # :nodoc:
289
+
290
+ ##
291
+ alias_method :deconstruct, :to_a # :nodoc:
292
+
293
+ ##
294
+ def deconstruct_keys(_keys) = {c:, m:, y:, k:, cyan:, magenta:, yellow:, key:} # :nodoc:
295
+
296
+ ##
297
+ def to_internal = [c, m, y, k] # :nodoc:
298
+
299
+ ##
300
+ def components = 4 # :nodoc:
252
301
 
253
302
  private
303
+
304
+ ##
254
305
  # Implements the Adobe PDF conversion of CMYK to RGB.
255
- def adobe_cmyk_rgb
256
- [ @c, @m, @y ].map { |v| 1.0 - [ 1.0, v + @k ].min }
257
- end
306
+ def adobe_cmyk_rgb = [c, m, y].map { 1.0 - [1.0, _1 + k].min } # :nodoc:
258
307
 
308
+ ##
259
309
  # Implements the standard conversion of CMYK to RGB.
260
- def standard_cmyk_rgb
261
- [ @c, @m, @y ].map { |v| 1.0 - (v * (1.0 - k) + k) }
310
+ def standard_cmyk_rgb = [c, m, y].map { 1.0 - (_1 * (1.0 - k) + k) } # :nodoc:
311
+
312
+ ##
313
+ def device_cmyk(alpha, fallback, fallback_alpha) # :nodoc:
314
+ params = [
315
+ css_value(cyan, :percent),
316
+ css_value(magenta, :percent),
317
+ css_value(yellow, :percent),
318
+ css_value(key, :percent)
319
+ ].join(" ")
320
+ params = "#{params} / #{css_value(alpha)}" if alpha
321
+ fallback = fallback&.css(alpha: fallback_alpha)
322
+
323
+ if fallback
324
+ "device-cmyk(#{params}, #{fallback})"
325
+ else
326
+ "device-cmyk(#{params})"
327
+ end
262
328
  end
263
329
  end