class Color::CMYK

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.

Constants

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.

Public Class Methods

new(c = 0, m = 0, y = 0, k = 0, radix = 100.0, &block) click to toggle source

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

Color::CMYK.new(30, 0, 80, 30)
# File lib/color/cmyk.rb, line 39
def initialize(c = 0, m = 0, y = 0, k = 0, radix = 100.0, &block) # :yields self:
  @c, @m, @y, @k = [ c, m, y, k ].map { |v| Color.normalize(v / radix) }
  block.call(self) if block
end

Public Instance Methods

black() click to toggle source

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

# File lib/color/cmyk.rb, line 231
def black
  @k * 100.0
end
black=(kk) click to toggle source

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

# File lib/color/cmyk.rb, line 240
def black=(kk)
  @k = Color.normalize(kk / 100.0)
end
c() click to toggle source

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

# File lib/color/cmyk.rb, line 176
def c
  @c
end
c=(cc) click to toggle source

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

# File lib/color/cmyk.rb, line 185
def c=(cc)
  @c = Color.normalize(cc)
end
coerce(other) click to toggle source

Coerces the other Color object into CMYK.

# File lib/color/cmyk.rb, line 14
def coerce(other)
  other.to_cmyk
end
css_hsl() click to toggle source

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.

# File lib/color/cmyk.rb, line 80
def css_hsl
  to_hsl.css_hsl
end
css_hsla() click to toggle source

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.

# File lib/color/cmyk.rb, line 87
def css_hsla
  to_hsl.css_hsla
end
css_rgb() click to toggle source

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.

# File lib/color/cmyk.rb, line 66
def css_rgb
  to_rgb.css_rgb
end
css_rgba() click to toggle source

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.

# File lib/color/cmyk.rb, line 73
def css_rgba
  to_rgb.css_rgba
end
cyan() click to toggle source

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

# File lib/color/cmyk.rb, line 171
def cyan
  @c * 100.0
end
cyan=(cc) click to toggle source

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

# File lib/color/cmyk.rb, line 180
def cyan=(cc)
  @c = Color.normalize(cc / 100.0)
end
from_fraction(c = 0, m = 0, y = 0, k = 0, &block) click to toggle source

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

Color::CMYK.from_fraction(0.3, 0, 0.8, 0.3)
# File lib/color/cmyk.rb, line 22
def from_fraction(c = 0, m = 0, y = 0, k = 0, &block)
  new(c, m, y, k, 1.0, &block)
end
from_percent(c = 0, m = 0, y = 0, k = 0, &block) click to toggle source

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

Color::CMYK.new(30, 0, 80, 30)
# File lib/color/cmyk.rb, line 30
def from_percent(c = 0, m = 0, y = 0, k = 0, &block)
  new(c, m, y, k, &block)
end
html() click to toggle source

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.

# File lib/color/cmyk.rb, line 59
def html
  to_rgb.html
end
inspect() click to toggle source
# File lib/color/cmyk.rb, line 156
def inspect
  "CMYK [%.2f%%, %.2f%%, %.2f%%, %.2f%%]" % [ cyan, magenta, yellow, black ]
end
k() click to toggle source

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

# File lib/color/cmyk.rb, line 236
def k
  @k
end
k=(kk) click to toggle source

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

# File lib/color/cmyk.rb, line 245
def k=(kk)
  @k = Color.normalize(kk)
end
m() click to toggle source

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

# File lib/color/cmyk.rb, line 196
def m
  @m
end
m=(mm) click to toggle source

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

# File lib/color/cmyk.rb, line 205
def m=(mm)
  @m = Color.normalize(mm)
end
magenta() click to toggle source

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

# File lib/color/cmyk.rb, line 191
def magenta
  @m * 100.0
end
magenta=(mm) click to toggle source

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

# File lib/color/cmyk.rb, line 200
def magenta=(mm)
  @m = Color.normalize(mm / 100.0)
end
pdf_fill() click to toggle source

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

# File lib/color/cmyk.rb, line 46
def pdf_fill
  PDF_FORMAT_STR % [ @c, @m, @y, @k, "k" ]
end
pdf_stroke() click to toggle source

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

# File lib/color/cmyk.rb, line 52
def pdf_stroke
  PDF_FORMAT_STR % [ @c, @m, @y, @k, "K" ]
end
to_a() click to toggle source
# File lib/color/cmyk.rb, line 249
def to_a
  [ c, m, y, k ]
end
to_cmyk() click to toggle source
# File lib/color/cmyk.rb, line 152
def to_cmyk
  self
end
to_grayscale() click to toggle source

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

# File lib/color/cmyk.rb, line 143
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
  Color::GrayScale.from_fraction(g)
end
Also aliased as: to_greyscale
to_greyscale()
Alias for: to_grayscale
to_hsl() click to toggle source

Converts to RGB then HSL.

# File lib/color/cmyk.rb, line 166
def to_hsl
  to_rgb.to_hsl
end
to_rgb(use_adobe_method = false) click to toggle source

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.

# File lib/color/cmyk.rb, line 125
def to_rgb(use_adobe_method = false)
  if use_adobe_method
    Color::RGB.from_fraction(*adobe_cmyk_rgb)
  else
    Color::RGB.from_fraction(*standard_cmyk_rgb)
  end
end
to_yiq() click to toggle source

Converts to RGB then YIQ.

# File lib/color/cmyk.rb, line 161
def to_yiq
  to_rgb.to_yiq
end
y() click to toggle source

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

# File lib/color/cmyk.rb, line 216
def y
  @y
end
y=(kk) click to toggle source

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

# File lib/color/cmyk.rb, line 225
def y=(kk)
  @y = Color.normalize(kk)
end
yellow() click to toggle source

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

# File lib/color/cmyk.rb, line 211
def yellow
  @y * 100.0
end
yellow=(yy) click to toggle source

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

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

Private Instance Methods

adobe_cmyk_rgb() click to toggle source

Implements the Adobe PDF conversion of CMYK to RGB.

# File lib/color/cmyk.rb, line 255
def adobe_cmyk_rgb
  [ @c, @m, @y ].map { |v| 1.0 - [ 1.0, v + @k ].min }
end
standard_cmyk_rgb() click to toggle source

Implements the standard conversion of CMYK to RGB.

# File lib/color/cmyk.rb, line 260
def standard_cmyk_rgb
  [ @c, @m, @y ].map { |v| 1.0 - (v * (1.0 - k) + k) }
end