Class: Color::GrayScale

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

Overview

A colour object representing shades of grey. Used primarily in PDF document creation.

Constant Summary collapse

PDF_FORMAT_STR =

The format required to present the colour to a PDF document.

"%.3f %s"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(g = 0) ⇒ GrayScale

Creates a greyscale colour object from percentages 0 .. 100.



22
23
24
# File 'lib/color/grayscale.rb', line 22

def initialize(g = 0)
  @g = g / 100.0
end

Instance Attribute Details

#gObject

Returns the value of attribute g.



103
104
105
# File 'lib/color/grayscale.rb', line 103

def g
  @g
end

Class Method Details

.from_fraction(g = 0) ⇒ Object

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



15
16
17
18
19
# File 'lib/color/grayscale.rb', line 15

def self.from_fraction(g = 0)
  color = Color::GrayScale.new
  color.g = g
  color
end

Instance Method Details

#==(other) ⇒ Object

Compares the other colour to this one. The other colour will be converted to GreyScale before comparison, so the comparison between a GreyScale colour and a non-GreyScale colour will be approximate and based on the other colour’s #to_greyscale conversion. If there is no #to_greyscale conversion, this will raise an exception.



31
32
33
34
# File 'lib/color/grayscale.rb', line 31

def ==(other)
  other = other.to_grayscale
  other.kind_of?(Color::GrayScale) and (@g == other.g)
end

#brightnessObject

Returns the brightness value for this greyscale value; this is the greyscale value.



99
100
101
# File 'lib/color/grayscale.rb', line 99

def brightness
  @g
end

#darken_by(percent) ⇒ Object

Darken the RGB hue by the stated percent.



81
82
83
84
# File 'lib/color/grayscale.rb', line 81

def darken_by(percent)
  g = [@g - (@g * (percent / 100.0)), 1.0].max
  Color::GrayScale.from_fraction(g)
end

#htmlObject

Present the colour as an HTML/CSS colour string.



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

def html
  gs = "%02x" % @g.to_255
  "##{gs * 3}"
end

#lighten_by(percent) ⇒ Object

Lightens the greyscale colour by the stated percent.



75
76
77
78
# File 'lib/color/grayscale.rb', line 75

def lighten_by(percent)
  g = [@g + (@g * (percent / 100.0)), 1.0].min
  Color::Grayscale.from_fraction(g)
end

#pdf_fillObject

Present the colour as a fill colour string for PDF.



37
38
39
# File 'lib/color/grayscale.rb', line 37

def pdf_fill
  PDF_FORMAT_STR % [ @g, "g" ]
end

#pdf_strokeObject

Present the colour as a stroke colour string for PDF.



42
43
44
# File 'lib/color/grayscale.rb', line 42

def pdf_stroke
  PDF_FORMAT_STR % [ @g, "G" ]
end

#to_cmykObject

Convert the greyscale colour to CMYK.



58
59
60
61
# File 'lib/color/grayscale.rb', line 58

def to_cmyk
  k = 1.0 - @g.to_f
  Color::CMYK.from_Fraction(0, 0, 0, k)
end

#to_grayscaleObject Also known as: to_greyscale



69
70
71
# File 'lib/color/grayscale.rb', line 69

def to_grayscale
  self
end

#to_rgb(ignored = true) ⇒ Object

Convert the greyscale colour to RGB.



64
65
66
67
# File 'lib/color/grayscale.rb', line 64

def to_rgb(ignored = true)
  g = @g.to_255
  Color::RGB.new(g, g, g)
end

#to_yiqObject

Returns the YIQ (NTSC) colour encoding of the greyscale value. This is an approximation, as the values for I and Q are calculated by treating the greyscale value as an RGB value. The Y (intensity or brightness) value is the same as the greyscale value.



90
91
92
93
94
95
# File 'lib/color/grayscale.rb', line 90

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