Class: Color::CMYK

Class | Color::CMYK
In: | lib/color.rb lib/color/cmyk.rb
Parent: | Object

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

from_fraction(c = 0, m = 0, y = 0, k = 0)

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

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

from_percent(c = 0, m = 0, y = 0, k = 0)

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

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

new(c = 0, m = 0, y = 0, k = 0)

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

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

### Public Instance methods

==(other)

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.

black()

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

black=(kk)

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

c()

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

c=(cc)

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

css_hsl()

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.

css_hsla()

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.

css_rgb()

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.

css_rgba()

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.

cyan()

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

cyan=(cc)

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

html()

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.

inspect()
k()

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

k=(kk)

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

m()

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

m=(mm)

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

magenta()

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

magenta=(mm)

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

pdf_fill()

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

pdf_stroke()

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

to_cmyk()
to_grayscale()

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

to_greyscale()

Alias for to_grayscale

to_hsl()

Converts to RGB then HSL.

to_rgb(use_adobe_method = false)

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.

to_yiq()

Converts to RGB then YIQ.

y()

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

y=(kk)

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

yellow()

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

yellow=(yy)

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

[Validate]