Class: ColorLib::GrayScale
- Inherits:
-
Object
- Object
- ColorLib::GrayScale
- Defined in:
- lib/color_lib/grayscale.rb,
lib/color_lib.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"
Class Method Summary collapse
-
.from_fraction(g = 0) ⇒ Object
Creates a greyscale colour object from fractional values 0..1.
-
.from_percent(g = 0) ⇒ Object
Creates a greyscale colour object from percentages 0..100.
Instance Method Summary collapse
-
#+(other) ⇒ Object
Adds another colour to the current colour.
-
#-(other) ⇒ Object
Subtracts another colour to the current colour.
-
#==(other) ⇒ Object
Compares the other colour to this one.
-
#brightness ⇒ Object
Returns the brightness value for this greyscale value; this is the greyscale value itself.
-
#css_hsl ⇒ Object
Present the colour as an HSL HTML/CSS colour string (e.g., “hsl(180, 25%, 35%)”).
-
#css_hsla ⇒ Object
Present the colour as an HSLA (with alpha) HTML/CSS colour string (e.g., “hsla(180, 25%, 35%, 1)”).
-
#css_rgb ⇒ Object
Present the colour as an RGB HTML/CSS colour string (e.g., “rgb(0%, 50%, 100%)”).
-
#css_rgba ⇒ Object
Present the colour as an RGBA (with alpha) HTML/CSS colour string (e.g., “rgb(0%, 50%, 100%, 1)”).
-
#darken_by(percent) ⇒ Object
Darken the greyscale colour by the stated percent.
-
#g ⇒ Object
Returns the grayscale value as a fractional value of white in the range 0.0 ..
-
#g=(gg) ⇒ Object
Returns the grayscale value as a fractional value of white in the range 0.0 ..
-
#gray ⇒ Object
(also: #grey)
Returns the grayscale value as a percentage of white (100% gray is white).
-
#gray=(gg) ⇒ Object
(also: #grey=)
Sets the grayscale value as a percentage of white.
-
#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.
- #inspect ⇒ Object
-
#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)
Reflexive conversion.
-
#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
Class Method Details
Instance Method Details
#+(other) ⇒ Object
Adds another colour to the current colour. The other colour will be converted to grayscale before addition. This conversion depends upon a #to_grayscale method on the other colour.
The addition is done using the grayscale accessor methods to ensure a valid colour in the result.
177 178 179 180 181 182 |
# File 'lib/color_lib/grayscale.rb', line 177 def +(other) other = other.to_grayscale ng = self.dup ng.g += other.g ng end |
#-(other) ⇒ Object
Subtracts another colour to the current colour. The other colour will be converted to grayscale before subtraction. This conversion depends upon a #to_grayscale method on the other colour.
The subtraction is done using the grayscale accessor methods to ensure a valid colour in the result.
190 191 192 193 194 195 |
# File 'lib/color_lib/grayscale.rb', line 190 def -(other) other = other.to_grayscale ng = self.dup ng.g -= other.g ng end |
#==(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 COLOR_TOLERANCE of each other.
39 40 41 42 43 |
# File 'lib/color_lib/grayscale.rb', line 39 def ==(other) other = other.to_grayscale other.kind_of?(ColorLib::GrayScale) and ((@g - other.g).abs <= ColorLib::COLOR_TOLERANCE) end |
#brightness ⇒ Object
Returns the brightness value for this greyscale value; this is the greyscale value itself.
142 143 144 |
# File 'lib/color_lib/grayscale.rb', line 142 def brightness @g end |
#css_hsl ⇒ Object
Present the colour as an HSL HTML/CSS colour string (e.g., “hsl(180, 25%, 35%)”). Note that this will perform a #to_hsl operation.
83 84 85 |
# File 'lib/color_lib/grayscale.rb', line 83 def css_hsl to_hsl.css_hsl end |
#css_hsla ⇒ Object
Present the colour as an HSLA (with alpha) HTML/CSS colour string (e.g., “hsla(180, 25%, 35%, 1)”). Note that this will perform a #to_hsl operation.
90 91 92 |
# File 'lib/color_lib/grayscale.rb', line 90 def css_hsla to_hsl.css_hsla end |
#css_rgb ⇒ Object
Present the colour as an RGB HTML/CSS colour string (e.g., “rgb(0%, 50%, 100%)”).
71 72 73 |
# File 'lib/color_lib/grayscale.rb', line 71 def css_rgb "rgb(%3.2f%%, %3.2f%%, %3.2f%%)" % [gray, gray, gray] end |
#css_rgba ⇒ Object
Present the colour as an RGBA (with alpha) HTML/CSS colour string (e.g., “rgb(0%, 50%, 100%, 1)”).
77 78 79 |
# File 'lib/color_lib/grayscale.rb', line 77 def css_rgba "rgba(%3.2f%%, %3.2f%%, %3.2f%%, %1.2f)" % [gray, gray, gray, 1] end |
#darken_by(percent) ⇒ Object
Darken the greyscale colour by the stated percent.
119 120 121 122 |
# File 'lib/color_lib/grayscale.rb', line 119 def darken_by(percent) g = [@g - (@g * (percent / 100.0)), 0.0].max ColorLib::GrayScale.from_fraction(g) end |
#g ⇒ Object
Returns the grayscale value as a fractional value of white in the range 0.0 .. 1.0.
155 156 157 |
# File 'lib/color_lib/grayscale.rb', line 155 def g @g end |
#g=(gg) ⇒ Object
Returns the grayscale value as a fractional value of white in the range 0.0 .. 1.0.
167 168 169 |
# File 'lib/color_lib/grayscale.rb', line 167 def g=(gg) @g = ColorLib.normalize(gg) end |
#gray ⇒ Object Also known as: grey
Returns the grayscale value as a percentage of white (100% gray is white).
148 149 150 |
# File 'lib/color_lib/grayscale.rb', line 148 def gray @g * 100.0 end |
#gray=(gg) ⇒ Object Also known as: grey=
Sets the grayscale value as a percentage of white.
160 161 162 |
# File 'lib/color_lib/grayscale.rb', line 160 def gray=(gg) @g = ColorLib.normalize(gg / 100.0) end |
#html ⇒ Object
Present the colour as an HTML/CSS colour string.
64 65 66 67 |
# File 'lib/color_lib/grayscale.rb', line 64 def html gs = "%02x" % to_255 "##{gs * 3}" end |
#inspect ⇒ Object
197 198 199 |
# File 'lib/color_lib/grayscale.rb', line 197 def inspect "Gray [%.2f%%]" % [gray] end |
#lighten_by(percent) ⇒ Object
Lightens the greyscale colour by the stated percent.
113 114 115 116 |
# File 'lib/color_lib/grayscale.rb', line 113 def lighten_by(percent) g = [@g + (@g * (percent / 100.0)), 1.0].min ColorLib::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.
47 48 49 |
# File 'lib/color_lib/grayscale.rb', line 47 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.
53 54 55 |
# File 'lib/color_lib/grayscale.rb', line 53 def pdf_stroke PDF_FORMAT_STR % [@g, "G"] end |
#to_cmyk ⇒ Object
Convert the greyscale colour to CMYK.
95 96 97 98 |
# File 'lib/color_lib/grayscale.rb', line 95 def to_cmyk k = 1.0 - @g.to_f ColorLib::CMYK.from_fraction(0, 0, 0, k) end |
#to_grayscale ⇒ Object Also known as: to_greyscale
Reflexive conversion.
106 107 108 |
# File 'lib/color_lib/grayscale.rb', line 106 def to_grayscale self end |
#to_hsl ⇒ Object
Returns the HSL colour encoding of the greyscale value.
136 137 138 |
# File 'lib/color_lib/grayscale.rb', line 136 def to_hsl ColorLib::HSL.from_fraction(0, 0, @g) end |
#to_rgb(ignored = true) ⇒ Object
Convert the greyscale colour to RGB.
101 102 103 |
# File 'lib/color_lib/grayscale.rb', line 101 def to_rgb(ignored = true) ColorLib::RGB.from_fraction(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.
128 129 130 131 132 133 |
# File 'lib/color_lib/grayscale.rb', line 128 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) ColorLib::YIQ.from_fraction(y, i, q) end |