Class: Color::CMYK

Inherits:
Object
  • Object
show all
Defined in:
lib/color/cmyk.rb,
lib/color.rb

Overview

An CMYK colour object.

Constant Summary collapse

PDF_FORMAT_STR =
"%.3f %.3f %.3f %.3f %s"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(c = 0, m = 0, y = 0, k = 0) ⇒ CMYK

Creates a CMYK colour object from percentages.



37
38
39
40
41
42
# File 'lib/color/cmyk.rb', line 37

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

Instance Attribute Details

#cObject

Returns the value of attribute c.



137
138
139
# File 'lib/color/cmyk.rb', line 137

def c
  @c
end

#kObject

Returns the value of attribute k.



137
138
139
# File 'lib/color/cmyk.rb', line 137

def k
  @k
end

#mObject

Returns the value of attribute m.



137
138
139
# File 'lib/color/cmyk.rb', line 137

def m
  @m
end

#yObject

Returns the value of attribute y.



137
138
139
# File 'lib/color/cmyk.rb', line 137

def y
  @y
end

Class Method Details

.from_fraction(c = 0, m = 0, y = 0, k = 0) ⇒ Object

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



27
28
29
30
31
32
33
34
# File 'lib/color/cmyk.rb', line 27

def self.from_fraction(c = 0, m = 0, y = 0, k = 0)
  colour = Color::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.



17
18
19
20
21
22
23
24
# File 'lib/color/cmyk.rb', line 17

def ==(other)
  other = other.to_cmyk
  other.kind_of?(Color::CMYK) and
  (@c == other.c) and
  (@m == other.m) and
  (@y == other.y) and
  (@k == other.k)
end

#htmlObject

Present the colour as an RGB HTML/CSS colour string. Note that this will perform a #to_rgb



56
57
58
# File 'lib/color/cmyk.rb', line 56

def html
  to_rgb.html
end

#pdf_fillObject

Present the colour as a DeviceCMYK fill colour string for PDF.



45
46
47
# File 'lib/color/cmyk.rb', line 45

def pdf_fill
  PDF_FORMAT_STR % [ @c, @m, @y, @k, "k" ]
end

#pdf_strokeObject

Present the colour as a DeviceCMYK stroke colour string for PDF.



50
51
52
# File 'lib/color/cmyk.rb', line 50

def pdf_stroke
  PDF_FORMAT_STR % [ @c, @m, @y, @k, "K" ]
end

#to_cmykObject



124
125
126
# File 'lib/color/cmyk.rb', line 124

def to_cmyk
  self
end

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



115
116
117
118
119
120
121
# File 'lib/color/cmyk.rb', line 115

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

#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 in color-tools 1.2 or later. The Adobe method is not used by default; to enable it, pass true to #to_rgb.



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/color/cmyk.rb', line 91

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
  Color::RGB.from_fraction(r, g, b)
end

#to_yiqObject

Returns the YIQ (NTSC) colour approximation of the CMYK value. This is done by first con



130
131
132
133
134
135
# File 'lib/color/cmyk.rb', line 130

def to_yiq
  y = (@r * 0.299) + (@g *  0.587) + (@b *  0.114)
  i = (@r * 0.596) + (@g * -0.275) + (@b * -0.321)
  q = (@r * 0.212) + (@g * -0.523) + (@b *  0.311)
  Color::YIQ.from_fraction(y, i, q)
end