Class: Color::GrayScale
- Inherits:
-
Object
- Object
- Color::GrayScale
- 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 of a DeviceGrey 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.
"%.3f %s"
Instance Attribute Summary collapse
-
#g ⇒ Object
Returns the value of attribute g.
Class Method Summary collapse
-
.from_fraction(g = 0) ⇒ Object
Creates a greyscale colour object from fractional values 0..1.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Compares the other colour to this one.
-
#brightness ⇒ Object
Returns the brightness value for this greyscale value; this is the greyscale value.
-
#darken_by(percent) ⇒ Object
Darken the RGB hue by the stated percent.
-
#html ⇒ Object
Present the colour as an HTML/CSS colour string.
-
#initialize(g = 0) ⇒ GrayScale
constructor
Creates a greyscale colour object from percentages 0..100.
-
#lighten_by(percent) ⇒ Object
Lightens the greyscale colour by the stated percent.
-
#pdf_fill ⇒ Object
Present the colour as a DeviceGrey fill colour string for PDF.
-
#pdf_stroke ⇒ Object
Present the colour as a DeviceGrey stroke colour string for PDF.
-
#to_cmyk ⇒ Object
Convert the greyscale colour to CMYK.
- #to_grayscale ⇒ Object (also: #to_greyscale)
-
#to_hsl ⇒ Object
Returns the HSL colour encoding of the greyscale value.
-
#to_rgb(ignored = true) ⇒ Object
Convert the greyscale colour to RGB.
-
#to_yiq ⇒ Object
Returns the YIQ (NTSC) colour encoding of the greyscale value.
Constructor Details
Instance Attribute Details
#g ⇒ Object
Returns the value of attribute g.
123 124 125 |
# File 'lib/color/grayscale.rb', line 123 def g @g end |
Class Method Details
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. This will report that two GreyScale values are equivalent if they are within 1e-4 (0.0001) of each other.
43 44 45 46 47 |
# File 'lib/color/grayscale.rb', line 43 def ==(other) other = other.to_grayscale other.kind_of?(Color::GrayScale) and ((@g - other.g).abs <= 1e-4) end |
#brightness ⇒ Object
Returns the brightness value for this greyscale value; this is the greyscale value.
119 120 121 |
# File 'lib/color/grayscale.rb', line 119 def brightness @g end |
#darken_by(percent) ⇒ Object
Darken the RGB hue by the stated percent.
96 97 98 99 |
# File 'lib/color/grayscale.rb', line 96 def darken_by(percent) g = [@g - (@g * (percent / 100.0)), 0.0].max Color::GrayScale.from_fraction(g) end |
#html ⇒ Object
Present the colour as an HTML/CSS colour string.
67 68 69 70 |
# File 'lib/color/grayscale.rb', line 67 def html gs = "%02x" % to_255 "##{gs * 3}" end |
#lighten_by(percent) ⇒ Object
Lightens the greyscale colour by the stated percent.
90 91 92 93 |
# File 'lib/color/grayscale.rb', line 90 def lighten_by(percent) g = [@g + (@g * (percent / 100.0)), 1.0].min Color::GrayScale.from_fraction(g) end |
#pdf_fill ⇒ Object
Present the colour as a DeviceGrey fill colour string for PDF. This will be removed from the default package in color-tools 2.0.
51 52 53 |
# File 'lib/color/grayscale.rb', line 51 def pdf_fill PDF_FORMAT_STR % [ @g, "g" ] end |
#pdf_stroke ⇒ Object
Present the colour as a DeviceGrey stroke colour string for PDF. This will be removed from the default package in color-tools 2.0.
57 58 59 |
# File 'lib/color/grayscale.rb', line 57 def pdf_stroke PDF_FORMAT_STR % [ @g, "G" ] end |
#to_cmyk ⇒ Object
Convert the greyscale colour to CMYK.
73 74 75 76 |
# File 'lib/color/grayscale.rb', line 73 def to_cmyk k = 1.0 - @g.to_f Color::CMYK.from_fraction(0, 0, 0, k) end |
#to_grayscale ⇒ Object Also known as: to_greyscale
84 85 86 |
# File 'lib/color/grayscale.rb', line 84 def to_grayscale self end |
#to_hsl ⇒ Object
Returns the HSL colour encoding of the greyscale value.
113 114 115 |
# File 'lib/color/grayscale.rb', line 113 def to_hsl Color::HSL.from_fraction(0, 0, @g) end |
#to_rgb(ignored = true) ⇒ Object
Convert the greyscale colour to RGB.
79 80 81 82 |
# File 'lib/color/grayscale.rb', line 79 def to_rgb(ignored = true) g = to_255 Color::RGB.new(g, g, g) end |
#to_yiq ⇒ Object
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.
105 106 107 108 109 110 |
# File 'lib/color/grayscale.rb', line 105 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 |