Class Color::CMYK
In: lib/color.rb
lib/color/cmyk.rb
Parent: Object
Enumerable Gimp AdobeColor CMYK\n[lib/color.rb\nlib/color/cmyk.rb] GrayScale\n[lib/color.rb\nlib/color/grayscale.rb] RGB\n[lib/color.rb\nlib/color/rgb-colors.rb\nlib/color/rgb.rb] YIQ\n[lib/color.rb\nlib/color/yiq.rb] HSL MonoContrast lib/color/cmyk.rb lib/color/grayscale.rb lib/color/rgb.rb lib/color/yiq.rb lib/color/hsl.rb CSS lib/color/palette/gimp.rb lib/color/palette/adobecolor.rb lib/color/palette/monocontrast.rb Palette Color dot/m_10_0.png

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.

Methods

==   black   black=   c   c=   css_hsl   css_hsla   css_rgb   css_rgba   cyan   cyan=   from_fraction   from_percent   html   inspect   k   k=   m   m=   magenta   magenta=   new   pdf_fill   pdf_stroke   to_cmyk   to_grayscale   to_greyscale   to_hsl   to_rgb   to_yiq   y   y=   yellow   yellow=  

Constants

PDF_FORMAT_STR = "%.3f %.3f %.3f %.3f %s"   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.

Public Class methods

Creates a CMYK colour object from fractional values 0..1.

  Color::CMYK.from_fraction(0.3, 0, 0.8, 0.3)

[Source]

    # File lib/color/cmyk.rb, line 44
44:   def self.from_fraction(c = 0, m = 0, y = 0, k = 0)
45:     colour = Color::CMYK.new
46:     colour.c = c
47:     colour.m = m
48:     colour.y = y
49:     colour.k = k
50:     colour
51:   end

Creates a CMYK colour object from percentages. Internally, the colour is managed as fractional values 0..1.

  Color::CMYK.new(30, 0, 80, 30)

[Source]

    # File lib/color/cmyk.rb, line 57
57:   def self.from_percent(c = 0, m = 0, y = 0, k = 0)
58:     Color::CMYK.new(c, m, y, k)
59:   end

Creates a CMYK colour object from percentages. Internally, the colour is managed as fractional values 0..1.

  Color::CMYK.new(30, 0, 80, 30)

[Source]

    # File lib/color/cmyk.rb, line 65
65:   def initialize(c = 0, m = 0, y = 0, k = 0)
66:     @c = c / 100.0
67:     @m = m / 100.0
68:     @y = y / 100.0
69:     @k = k / 100.0
70:   end

Public Instance methods

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.

[Source]

    # File lib/color/cmyk.rb, line 32
32:   def ==(other)
33:     other = other.to_cmyk
34:     other.kind_of?(Color::CMYK) and
35:     ((@c - other.c).abs <= Color::COLOR_TOLERANCE) and
36:     ((@m - other.m).abs <= Color::COLOR_TOLERANCE) and
37:     ((@y - other.y).abs <= Color::COLOR_TOLERANCE) and
38:     ((@k - other.k).abs <= Color::COLOR_TOLERANCE)
39:   end

Returns the black (K) component of the CMYK colour as a percentage value.

[Source]

     # File lib/color/cmyk.rb, line 264
264:   def black
265:     @k * 100.0
266:   end

Sets the black (K) component of the CMYK colour as a percentage value.

[Source]

     # File lib/color/cmyk.rb, line 273
273:   def black=(kk)
274:     @k = Color.normalize(kk / 100.0)
275:   end

Returns the cyan (C) component of the CMYK colour as a value in the range 0.0 .. 1.0.

[Source]

     # File lib/color/cmyk.rb, line 209
209:   def c
210:     @c
211:   end

Sets the cyan (C) component of the CMYK colour as a value in the range 0.0 .. 1.0.

[Source]

     # File lib/color/cmyk.rb, line 218
218:   def c=(cc)
219:     @c = Color.normalize(cc)
220:   end

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.

[Source]

     # File lib/color/cmyk.rb, line 108
108:   def css_hsl
109:     to_hsl.css_hsl
110:   end

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.

[Source]

     # File lib/color/cmyk.rb, line 115
115:   def css_hsla
116:     to_hsl.css_hsla
117:   end

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.

[Source]

    # File lib/color/cmyk.rb, line 94
94:   def css_rgb
95:     to_rgb.css_rgb
96:   end

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.

[Source]

     # File lib/color/cmyk.rb, line 101
101:   def css_rgba
102:     to_rgb.css_rgba
103:   end

Returns the cyan (C) component of the CMYK colour as a percentage value.

[Source]

     # File lib/color/cmyk.rb, line 204
204:   def cyan
205:     @c * 100.0
206:   end

Sets the cyan (C) component of the CMYK colour as a percentage value.

[Source]

     # File lib/color/cmyk.rb, line 213
213:   def cyan=(cc)
214:     @c = Color.normalize(cc / 100.0)
215:   end

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.

[Source]

    # File lib/color/cmyk.rb, line 87
87:   def html
88:     to_rgb.html
89:   end

[Source]

     # File lib/color/cmyk.rb, line 189
189:   def inspect
190:     "CMYK [%.2f%%, %.2f%%, %.2f%%, %.2f%%]" % [ cyan, magenta, yellow, black ]
191:   end

Returns the black (K) component of the CMYK colour as a value in the range 0.0 .. 1.0.

[Source]

     # File lib/color/cmyk.rb, line 269
269:   def k
270:     @k
271:   end

Sets the black (K) component of the CMYK colour as a value in the range 0.0 .. 1.0.

[Source]

     # File lib/color/cmyk.rb, line 278
278:   def k=(kk)
279:     @k = Color.normalize(kk)
280:   end

Returns the magenta (M) component of the CMYK colour as a value in the range 0.0 .. 1.0.

[Source]

     # File lib/color/cmyk.rb, line 229
229:   def m
230:     @m
231:   end

Sets the magenta (M) component of the CMYK colour as a value in the range 0.0 .. 1.0.

[Source]

     # File lib/color/cmyk.rb, line 238
238:   def m=(mm)
239:     @m = Color.normalize(mm)
240:   end

Returns the magenta (M) component of the CMYK colour as a percentage value.

[Source]

     # File lib/color/cmyk.rb, line 224
224:   def magenta
225:     @m * 100.0
226:   end

Sets the magenta (M) component of the CMYK colour as a percentage value.

[Source]

     # File lib/color/cmyk.rb, line 233
233:   def magenta=(mm)
234:     @m = Color.normalize(mm / 100.0)
235:   end

Present the colour as a DeviceCMYK fill colour string for PDF. This will be removed from the default package in color-tools 2.0.

[Source]

    # File lib/color/cmyk.rb, line 74
74:   def pdf_fill
75:     PDF_FORMAT_STR % [ @c, @m, @y, @k, "k" ]
76:   end

Present the colour as a DeviceCMYK stroke colour string for PDF. This will be removed from the default package in color-tools 2.0.

[Source]

    # File lib/color/cmyk.rb, line 80
80:   def pdf_stroke
81:     PDF_FORMAT_STR % [ @c, @m, @y, @k, "K" ]
82:   end

[Source]

     # File lib/color/cmyk.rb, line 185
185:   def to_cmyk
186:     self
187:   end

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).

[Source]

     # File lib/color/cmyk.rb, line 176
176:   def to_grayscale
177:     c = 0.299 * @c.to_f
178:     m = 0.587 * @m.to_f
179:     y = 0.114 * @y.to_f
180:     g = 1.0 - [1.0, c + m + y + @k].min
181:     Color::GrayScale.from_fraction(g)
182:   end
to_greyscale()

Alias for to_grayscale

Converts to RGB then HSL.

[Source]

     # File lib/color/cmyk.rb, line 199
199:   def to_hsl
200:     to_rgb.to_hsl
201:   end

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 Color may offer other conversion mechanisms that offer greater colour fidelity, including recognition of ICC colour profiles.

[Source]

     # File lib/color/cmyk.rb, line 153
153:   def to_rgb(use_adobe_method = false)
154:     if use_adobe_method
155:       r = 1.0 - [1.0, @c + @k].min
156:       g = 1.0 - [1.0, @m + @k].min
157:       b = 1.0 - [1.0, @y + @k].min
158:     else
159:       r = 1.0 - (@c.to_f * (1.0 - @k.to_f) + @k.to_f)
160:       g = 1.0 - (@m.to_f * (1.0 - @k.to_f) + @k.to_f)
161:       b = 1.0 - (@y.to_f * (1.0 - @k.to_f) + @k.to_f)
162:     end
163:     Color::RGB.from_fraction(r, g, b)
164:   end

Converts to RGB then YIQ.

[Source]

     # File lib/color/cmyk.rb, line 194
194:   def to_yiq
195:     to_rgb.to_yiq
196:   end

Returns the yellow (Y) component of the CMYK colour as a value in the range 0.0 .. 1.0.

[Source]

     # File lib/color/cmyk.rb, line 249
249:   def y
250:     @y
251:   end

Sets the yellow (Y) component of the CMYK colour as a value in the range 0.0 .. 1.0.

[Source]

     # File lib/color/cmyk.rb, line 258
258:   def y=(kk)
259:     @y = Color.normalize(kk)
260:   end

Returns the yellow (Y) component of the CMYK colour as a percentage value.

[Source]

     # File lib/color/cmyk.rb, line 244
244:   def yellow
245:     @y * 100.0
246:   end

Sets the yellow (Y) component of the CMYK colour as a percentage value.

[Source]

     # File lib/color/cmyk.rb, line 253
253:   def yellow=(yy)
254:     @y = Color.normalize(yy / 100.0)
255:   end

[Validate]