Class | Color::CMYK |
In: |
lib/color.rb
lib/color/cmyk.rb |
Parent: | Object |
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. |
c | [RW] | |
k | [RW] | |
m | [RW] | |
y | [RW] |
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 41 41: def self.from_fraction(c = 0, m = 0, y = 0, k = 0) 42: colour = Color::CMYK.new 43: colour.c = c 44: colour.m = m 45: colour.y = y 46: colour.k = k 47: colour 48: end
Creates a CMYK colour object from percentages. Internally, the colour is managed as fractional values 0..1.
Color::CMYK.from_fraction(30, 0, 80, 30)
# File lib/color/cmyk.rb, line 54 54: def initialize(c = 0, m = 0, y = 0, k = 0) 55: @c = c / 100.0 56: @m = m / 100.0 57: @y = y / 100.0 58: @k = k / 100.0 59: end
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 1e-4 (0.0001) of each other.
# File lib/color/cmyk.rb, line 29 29: def ==(other) 30: other = other.to_cmyk 31: other.kind_of?(Color::CMYK) and 32: ((@c - other.c).abs <= 1e-4) and 33: ((@m - other.m).abs <= 1e-4) and 34: ((@y - other.y).abs <= 1e-4) and 35: ((@k - other.k).abs <= 1e-4) 36: 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.
# File lib/color/cmyk.rb, line 63 63: def pdf_fill 64: PDF_FORMAT_STR % [ @c, @m, @y, @k, "k" ] 65: 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.
# File lib/color/cmyk.rb, line 69 69: def pdf_stroke 70: PDF_FORMAT_STR % [ @c, @m, @y, @k, "K" ] 71: 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).
# File lib/color/cmyk.rb, line 137 137: def to_grayscale 138: c = 0.299 * @c.to_f 139: m = 0.587 * @m.to_f 140: y = 0.114 * @y.to_f 141: g = 1.0 - [1.0, c + m + y + @k].min 142: Color::GrayScale.from_fraction(g) 143: 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 in color-tools 1.2 or later. The Adobe method is not used by default; to enable it, pass true to to_rgb.
Future versions of color-tools may offer other conversion mechanisms that offer greater colour fidelity.
# File lib/color/cmyk.rb, line 113 113: def to_rgb(use_adobe_method = false) 114: if use_adobe_method 115: r = 1.0 - [1.0, @c + @k].min 116: g = 1.0 - [1.0, @m + @k].min 117: b = 1.0 - [1.0, @y + @k].min 118: else 119: r = 1.0 - (@c.to_f * (1.0 - @k.to_f) + @k.to_f) 120: g = 1.0 - (@m.to_f * (1.0 - @k.to_f) + @k.to_f) 121: b = 1.0 - (@y.to_f * (1.0 - @k.to_f) + @k.to_f) 122: end 123: Color::RGB.from_fraction(r, g, b) 124: end