Class: Color::CMYK

Inherits:
Object
  • Object
show all
Defined in:
lib/color/cmyk.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.



35
36
37
38
39
40
# File 'lib/color/cmyk.rb', line 35

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.



71
72
73
# File 'lib/color/cmyk.rb', line 71

def c
  @c
end

#kObject

Returns the value of attribute k.



71
72
73
# File 'lib/color/cmyk.rb', line 71

def k
  @k
end

#mObject

Returns the value of attribute m.



71
72
73
# File 'lib/color/cmyk.rb', line 71

def m
  @m
end

#yObject

Returns the value of attribute y.



71
72
73
# File 'lib/color/cmyk.rb', line 71

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.



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

def self.from_fraction(c = 0, m = 0, y = 0, k = 0)
  colour = Color::CMYK.new
  colour.instance_variable_set(:@c, c)
  colour.instance_variable_set(:@m, m)
  colour.instance_variable_set(:@y, y)
  colour.instance_variable_set(:@k, k)
  colour
end

Instance Method Details

#==(other) ⇒ Object



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

def ==(other)
  other = other.to_cmyk if other.kind_of?(Color::RGB)
  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 HTML/CSS colour string.



53
54
55
# File 'lib/color/cmyk.rb', line 53

def html
  to_rgb.html
end

#pdf_fillObject

Present the colour as a fill colour string for PDF.



43
44
45
# File 'lib/color/cmyk.rb', line 43

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

#pdf_strokeObject

Present the colour as a stroke colour string for PDF.



48
49
50
# File 'lib/color/cmyk.rb', line 48

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

#to_cmykObject



67
68
69
# File 'lib/color/cmyk.rb', line 67

def to_cmyk
  self.dup
end

#to_rgbObject

Convert the CMYK colour to RGB. Note that colour experts strongly suggest that this is a bad idea, as CMYK represents percentages of inks, not mixed colour intensities like RGB.



60
61
62
63
64
65
# File 'lib/color/cmyk.rb', line 60

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