Class: Spectrum::CMYK
- Inherits:
-
Object
- Object
- Spectrum::CMYK
- Defined in:
- lib/spectrum/cmyk.rb,
lib/spectrum.rb
Overview
An CMYK colour object. CMYK (cyan, magenta, yellow, and black) colours are based on additive percentages of ink. A CMYK colour of (0.3, 0, 0.8, 0.3) would be mixed from 30% cyan, 0% magenta, 80% yellow, and 30% black. Primarily used in four-colour printing processes.
Constant Summary collapse
- PDF_FORMAT_STR =
The format of a DeviceCMYK colour for PDF. In color-tools 2.0 this will be removed from this package and added back as a modification by the PDF::Writer package.
"%.3f %.3f %.3f %.3f %s"
Class Method Summary collapse
-
.from_fraction(c = 0, m = 0, y = 0, k = 0) ⇒ Object
Creates a CMYK colour object from fractional values 0..1.
-
.from_percent(c = 0, m = 0, y = 0, k = 0) ⇒ Object
Creates a CMYK colour object from percentages.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Compares the other colour to this one.
-
#black ⇒ Object
Returns the black (K) component of the CMYK colour as a percentage value.
-
#black=(kk) ⇒ Object
Sets the black (K) component of the CMYK colour as a percentage value.
-
#c ⇒ Object
Returns the cyan © component of the CMYK colour as a value in the range 0.0 ..
-
#c=(cc) ⇒ Object
Sets the cyan © component of the CMYK colour as a value in the range 0.0 ..
-
#css_hsl ⇒ Object
Present the colour as an HSL HTML/CSS colour string (e.g., “hsl(180, 25%, 35%)”).
-
#css_hsla ⇒ Object
Present the colour as an HSLA (with alpha) HTML/CSS colour string (e.g., “hsla(180, 25%, 35%, 1)”).
-
#css_rgb ⇒ Object
Present the colour as an RGB HTML/CSS colour string (e.g., “rgb(0%, 50%, 100%)”).
-
#css_rgba ⇒ Object
Present the colour as an RGBA (with alpha) HTML/CSS colour string (e.g., “rgb(0%, 50%, 100%, 1)”).
-
#cyan ⇒ Object
Returns the cyan © component of the CMYK colour as a percentage value.
-
#cyan=(cc) ⇒ Object
Sets the cyan © component of the CMYK colour as a percentage value.
-
#html ⇒ Object
Present the colour as an RGB HTML/CSS colour string (e.g., “#aabbcc”).
-
#initialize(c = 0, m = 0, y = 0, k = 0) ⇒ CMYK
constructor
Creates a CMYK colour object from percentages.
- #inspect ⇒ Object
-
#k ⇒ Object
Returns the black (K) component of the CMYK colour as a value in the range 0.0 ..
-
#k=(kk) ⇒ Object
Sets the black (K) component of the CMYK colour as a value in the range 0.0 ..
-
#m ⇒ Object
Returns the magenta (M) component of the CMYK colour as a value in the range 0.0 ..
-
#m=(mm) ⇒ Object
Sets the magenta (M) component of the CMYK colour as a value in the range 0.0 ..
-
#magenta ⇒ Object
Returns the magenta (M) component of the CMYK colour as a percentage value.
-
#magenta=(mm) ⇒ Object
Sets the magenta (M) component of the CMYK colour as a percentage value.
-
#pdf_fill ⇒ Object
Present the colour as a DeviceCMYK fill colour string for PDF.
-
#pdf_stroke ⇒ Object
Present the colour as a DeviceCMYK stroke colour string for PDF.
- #to_cmyk ⇒ Object
-
#to_grayscale ⇒ Object
(also: #to_greyscale)
Converts the CMYK colour to a single greyscale value.
-
#to_hsl ⇒ Object
Converts to RGB then HSL.
-
#to_rgb(use_adobe_method = false) ⇒ Object
Converts the CMYK colour to RGB.
-
#to_yiq ⇒ Object
Converts to RGB then YIQ.
-
#y ⇒ Object
Returns the yellow (Y) component of the CMYK colour as a value in the range 0.0 ..
-
#y=(kk) ⇒ Object
Sets the yellow (Y) component of the CMYK colour as a value in the range 0.0 ..
-
#yellow ⇒ Object
Returns the yellow (Y) component of the CMYK colour as a percentage value.
-
#yellow=(yy) ⇒ Object
Sets the yellow (Y) component of the CMYK colour as a percentage value.
Constructor Details
#initialize(c = 0, m = 0, y = 0, k = 0) ⇒ CMYK
65 66 67 68 69 70 |
# File 'lib/spectrum/cmyk.rb', line 65 def initialize(c = 0, m = 0, y = 0, k = 0) @c = c / 100.0 @m = m / 100.0 @y = y / 100.0 @k = k / 100.0 end |
Class Method Details
.from_fraction(c = 0, m = 0, y = 0, k = 0) ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/spectrum/cmyk.rb', line 44 def self.from_fraction(c = 0, m = 0, y = 0, k = 0) colour = Spectrum::CMYK.new colour.c = c colour.m = m colour.y = y colour.k = k colour end |
Instance Method Details
#==(other) ⇒ Object
Compares the other colour to this one. The other colour will be converted to CMYK before comparison, so the comparison between a CMYK colour and a non-CMYK colour will be approximate and based on the other colour’s #to_cmyk conversion. If there is no #to_cmyk conversion, this will raise an exception. This will report that two CMYK colours are equivalent if all component values are within COLOR_TOLERANCE of each other.
32 33 34 35 36 37 38 39 |
# File 'lib/spectrum/cmyk.rb', line 32 def ==(other) other = other.to_cmyk other.kind_of?(Spectrum::CMYK) and ((@c - other.c).abs <= Spectrum::COLOR_TOLERANCE) and ((@m - other.m).abs <= Spectrum::COLOR_TOLERANCE) and ((@y - other.y).abs <= Spectrum::COLOR_TOLERANCE) and ((@k - other.k).abs <= Spectrum::COLOR_TOLERANCE) end |
#black ⇒ Object
Returns the black (K) component of the CMYK colour as a percentage value.
264 265 266 |
# File 'lib/spectrum/cmyk.rb', line 264 def black @k * 100.0 end |
#black=(kk) ⇒ Object
Sets the black (K) component of the CMYK colour as a percentage value.
273 274 275 |
# File 'lib/spectrum/cmyk.rb', line 273 def black=(kk) @k = Spectrum.normalize(kk / 100.0) end |
#c ⇒ Object
Returns the cyan © component of the CMYK colour as a value in the range 0.0 .. 1.0.
209 210 211 |
# File 'lib/spectrum/cmyk.rb', line 209 def c @c end |
#c=(cc) ⇒ Object
Sets the cyan © component of the CMYK colour as a value in the range 0.0 .. 1.0.
218 219 220 |
# File 'lib/spectrum/cmyk.rb', line 218 def c=(cc) @c = Spectrum.normalize(cc) end |
#css_hsl ⇒ Object
Present the colour as an HSL HTML/CSS colour string (e.g., “hsl(180, 25%, 35%)”). Note that this will perform a #to_hsl operation using the default conversion formula.
108 109 110 |
# File 'lib/spectrum/cmyk.rb', line 108 def css_hsl to_hsl.css_hsl end |
#css_hsla ⇒ Object
Present the colour as an HSLA (with alpha) HTML/CSS colour string (e.g., “hsla(180, 25%, 35%, 1)”). Note that this will perform a #to_hsl operation using the default conversion formula.
115 116 117 |
# File 'lib/spectrum/cmyk.rb', line 115 def css_hsla to_hsl.css_hsla end |
#css_rgb ⇒ Object
Present the colour as an RGB HTML/CSS colour string (e.g., “rgb(0%, 50%, 100%)”). Note that this will perform a #to_rgb operation using the default conversion formula.
94 95 96 |
# File 'lib/spectrum/cmyk.rb', line 94 def css_rgb to_rgb.css_rgb end |
#css_rgba ⇒ Object
Present the colour as an RGBA (with alpha) HTML/CSS colour string (e.g., “rgb(0%, 50%, 100%, 1)”). Note that this will perform a #to_rgb operation using the default conversion formula.
101 102 103 |
# File 'lib/spectrum/cmyk.rb', line 101 def css_rgba to_rgb.css_rgba end |
#cyan ⇒ Object
Returns the cyan © component of the CMYK colour as a percentage value.
204 205 206 |
# File 'lib/spectrum/cmyk.rb', line 204 def cyan @c * 100.0 end |
#cyan=(cc) ⇒ Object
Sets the cyan © component of the CMYK colour as a percentage value.
213 214 215 |
# File 'lib/spectrum/cmyk.rb', line 213 def cyan=(cc) @c = Spectrum.normalize(cc / 100.0) end |
#html ⇒ Object
Present the colour as an RGB HTML/CSS colour string (e.g., “#aabbcc”). Note that this will perform a #to_rgb operation using the default conversion formula.
87 88 89 |
# File 'lib/spectrum/cmyk.rb', line 87 def html to_rgb.html end |
#inspect ⇒ Object
189 190 191 |
# File 'lib/spectrum/cmyk.rb', line 189 def inspect "CMYK [%.2f%%, %.2f%%, %.2f%%, %.2f%%]" % [ cyan, magenta, yellow, black ] end |
#k ⇒ Object
Returns the black (K) component of the CMYK colour as a value in the range 0.0 .. 1.0.
269 270 271 |
# File 'lib/spectrum/cmyk.rb', line 269 def k @k end |
#k=(kk) ⇒ Object
Sets the black (K) component of the CMYK colour as a value in the range 0.0 .. 1.0.
278 279 280 |
# File 'lib/spectrum/cmyk.rb', line 278 def k=(kk) @k = Spectrum.normalize(kk) end |
#m ⇒ Object
Returns the magenta (M) component of the CMYK colour as a value in the range 0.0 .. 1.0.
229 230 231 |
# File 'lib/spectrum/cmyk.rb', line 229 def m @m end |
#m=(mm) ⇒ Object
Sets the magenta (M) component of the CMYK colour as a value in the range 0.0 .. 1.0.
238 239 240 |
# File 'lib/spectrum/cmyk.rb', line 238 def m=(mm) @m = Spectrum.normalize(mm) end |
#magenta ⇒ Object
Returns the magenta (M) component of the CMYK colour as a percentage value.
224 225 226 |
# File 'lib/spectrum/cmyk.rb', line 224 def magenta @m * 100.0 end |
#magenta=(mm) ⇒ Object
Sets the magenta (M) component of the CMYK colour as a percentage value.
233 234 235 |
# File 'lib/spectrum/cmyk.rb', line 233 def magenta=(mm) @m = Spectrum.normalize(mm / 100.0) end |
#pdf_fill ⇒ Object
Present the colour as a DeviceCMYK fill colour string for PDF. This will be removed from the default package in color-tools 2.0.
74 75 76 |
# File 'lib/spectrum/cmyk.rb', line 74 def pdf_fill PDF_FORMAT_STR % [ @c, @m, @y, @k, "k" ] end |
#pdf_stroke ⇒ Object
Present the colour as a DeviceCMYK stroke colour string for PDF. This will be removed from the default package in color-tools 2.0.
80 81 82 |
# File 'lib/spectrum/cmyk.rb', line 80 def pdf_stroke PDF_FORMAT_STR % [ @c, @m, @y, @k, "K" ] end |
#to_cmyk ⇒ Object
185 186 187 |
# File 'lib/spectrum/cmyk.rb', line 185 def to_cmyk self end |
#to_grayscale ⇒ Object Also known as: to_greyscale
Converts the CMYK colour to a single greyscale value. There are undoubtedly multiple methods for this conversion, but only a minor variant of the Adobe conversion method will be used:
g = 1.0 - min(1.0, 0.299 * c + 0.587 * m + 0.114 * y + k)
This treats the CMY values similarly to YIQ (NTSC) values and then adds the level of black. This is a variant of the Adobe version because it uses the more precise YIQ (NTSC) conversion values for Y (intensity) rather than the approximates provided by Adobe (0.3, 0.59, and 0.11).
176 177 178 179 180 181 182 |
# File 'lib/spectrum/cmyk.rb', line 176 def to_grayscale c = 0.299 * @c.to_f m = 0.587 * @m.to_f y = 0.114 * @y.to_f g = 1.0 - [1.0, c + m + y + @k].min Spectrum::GrayScale.from_fraction(g) end |
#to_hsl ⇒ Object
Converts to RGB then HSL.
199 200 201 |
# File 'lib/spectrum/cmyk.rb', line 199 def to_hsl to_rgb.to_hsl end |
#to_rgb(use_adobe_method = false) ⇒ Object
Converts the CMYK colour to RGB. Most colour experts strongly suggest that this is not a good idea (some even suggesting that it’s a very bad idea). CMYK represents additive percentages of inks on white paper, whereas RGB represents mixed colour intensities on a black screen.
However, the colour conversion can be done, and there are two different methods for the conversion that provide slightly different results. Adobe PDF conversions are done with the first form.
# Adobe PDF Display Formula
r = 1.0 - min(1.0, c + k)
g = 1.0 - min(1.0, m + k)
b = 1.0 - min(1.0, y + k)
# Other
r = 1.0 - (c * (1.0 - k) + k)
g = 1.0 - (m * (1.0 - k) + k)
b = 1.0 - (y * (1.0 - k) + k)
If we have a CMYK colour of [33% 66% 83% 25%], the first method will give an approximate RGB colour of (107, 23, 0) or #6b1700. The second method will give an approximate RGB colour of (128, 65, 33) or #804121. Which is correct? Although the colours may seem to be drastically different in the RGB colour space, they are very similar colours, differing mostly in intensity. The first is a darker, slightly redder brown; the second is a lighter brown.
Because of this subtlety, both methods are now offered for conversion. The Adobe method is not used by default; to enable it, pass true
to #to_rgb.
Future versions of Spectrum may offer other conversion mechanisms that offer greater colour fidelity, including recognition of ICC colour profiles.
153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/spectrum/cmyk.rb', line 153 def to_rgb(use_adobe_method = false) if use_adobe_method r = 1.0 - [1.0, @c + @k].min g = 1.0 - [1.0, @m + @k].min b = 1.0 - [1.0, @y + @k].min else r = 1.0 - (@c.to_f * (1.0 - @k.to_f) + @k.to_f) g = 1.0 - (@m.to_f * (1.0 - @k.to_f) + @k.to_f) b = 1.0 - (@y.to_f * (1.0 - @k.to_f) + @k.to_f) end Spectrum::RGB.from_fraction(r, g, b) end |
#to_yiq ⇒ Object
Converts to RGB then YIQ.
194 195 196 |
# File 'lib/spectrum/cmyk.rb', line 194 def to_yiq to_rgb.to_yiq end |
#y ⇒ Object
Returns the yellow (Y) component of the CMYK colour as a value in the range 0.0 .. 1.0.
249 250 251 |
# File 'lib/spectrum/cmyk.rb', line 249 def y @y end |
#y=(kk) ⇒ Object
Sets the yellow (Y) component of the CMYK colour as a value in the range 0.0 .. 1.0.
258 259 260 |
# File 'lib/spectrum/cmyk.rb', line 258 def y=(kk) @y = Spectrum.normalize(kk) end |
#yellow ⇒ Object
Returns the yellow (Y) component of the CMYK colour as a percentage value.
244 245 246 |
# File 'lib/spectrum/cmyk.rb', line 244 def yellow @y * 100.0 end |