class Spectrum::GrayScale
A colour object representing shades of grey. Used primarily in PDF document creation.
Constants
- 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.
Public Class Methods
Creates a greyscale colour object from fractional values 0..1.
Spectrum::GreyScale.from_fraction(0.5)
# File lib/spectrum/grayscale.rb, line 24 def self.from_fraction(g = 0) color = Spectrum::GrayScale.new color.g = g color end
Creates a greyscale colour object from percentages 0..100.
Spectrum::GrayScale.from_percent(50)
# File lib/spectrum/grayscale.rb, line 33 def self.from_percent(g = 0) Spectrum::GrayScale.new(g) end
Creates a greyscale colour object from percentages 0..100.
Spectrum::GrayScale.new(50)
# File lib/spectrum/grayscale.rb, line 40 def initialize(g = 0) @g = g / 100.0 end
Public Instance Methods
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.
# File lib/spectrum/grayscale.rb, line 184 def +(other) other = other.to_grayscale ng = self.dup ng.g += other.g ng end
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.
# File lib/spectrum/grayscale.rb, line 197 def -(other) other = other.to_grayscale ng = self.dup ng.g -= other.g ng end
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.
# File lib/spectrum/grayscale.rb, line 51 def ==(other) other = other.to_grayscale other.kind_of?(Spectrum::GrayScale) and ((@g - other.g).abs <= Spectrum::COLOR_TOLERANCE) end
Returns the brightness value for this greyscale value; this is the greyscale value itself.
# File lib/spectrum/grayscale.rb, line 152 def brightness @g end
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.
# File lib/spectrum/grayscale.rb, line 94 def css_hsl to_hsl.css_hsl end
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.
# File lib/spectrum/grayscale.rb, line 101 def css_hsla to_hsl.css_hsla end
Present the colour as an RGB HTML/CSS colour string (e.g., “rgb(0%, 50%, 100%)”).
# File lib/spectrum/grayscale.rb, line 82 def css_rgb "rgb(%3.2f%%, %3.2f%%, %3.2f%%)" % [ gray, gray, gray ] end
Present the colour as an RGBA (with alpha) HTML/CSS colour string (e.g., “rgb(0%, 50%, 100%, 1)”).
# File lib/spectrum/grayscale.rb, line 88 def css_rgba "rgba(%3.2f%%, %3.2f%%, %3.2f%%, %1.2f)" % [ gray, gray, gray, 1 ] end
Darken the greyscale colour by the stated percent.
# File lib/spectrum/grayscale.rb, line 129 def darken_by(percent) g = [@g - (@g * (percent / 100.0)), 0.0].max Spectrum::GrayScale.from_fraction(g) end
Returns the grayscale value as a fractional value of white in the range 0.0 .. 1.0.
# File lib/spectrum/grayscale.rb, line 164 def g @g end
Returns the grayscale value as a fractional value of white in the range 0.0 .. 1.0.
# File lib/spectrum/grayscale.rb, line 174 def g=(gg) @g = Spectrum.normalize(gg) end
Returns the grayscale value as a percentage of white (100% gray is white).
# File lib/spectrum/grayscale.rb, line 158 def gray @g * 100.0 end
Sets the grayscale value as a percentage of white.
# File lib/spectrum/grayscale.rb, line 168 def gray=(gg) @g = Spectrum.normalize(gg / 100.0) end
Present the colour as an HTML/CSS colour string.
# File lib/spectrum/grayscale.rb, line 75 def html gs = "%02x" % to_255 "##{gs * 3}" end
# File lib/spectrum/grayscale.rb, line 204 def inspect "Gray [%.2f%%]" % [ gray ] end
Lightens the greyscale colour by the stated percent.
# File lib/spectrum/grayscale.rb, line 123 def lighten_by(percent) g = [@g + (@g * (percent / 100.0)), 1.0].min Spectrum::GrayScale.from_fraction(g) end
Present the colour as a DeviceGrey fill colour string for PDF. This will be removed from the default package in color-tools 2.0.
# File lib/spectrum/grayscale.rb, line 59 def pdf_fill PDF_FORMAT_STR % [ @g, "g" ] end
Present the colour as a DeviceGrey stroke colour string for PDF. This will be removed from the default package in color-tools 2.0.
# File lib/spectrum/grayscale.rb, line 65 def pdf_stroke PDF_FORMAT_STR % [ @g, "G" ] end
Convert the greyscale colour to CMYK.
# File lib/spectrum/grayscale.rb, line 106 def to_cmyk k = 1.0 - @g.to_f Spectrum::CMYK.from_fraction(0, 0, 0, k) end
Reflexive conversion.
# File lib/spectrum/grayscale.rb, line 117 def to_grayscale self end
Returns the HSL colour encoding of the greyscale value.
# File lib/spectrum/grayscale.rb, line 146 def to_hsl Spectrum::HSL.from_fraction(0, 0, @g) end
Convert the greyscale colour to RGB.
# File lib/spectrum/grayscale.rb, line 112 def to_rgb(ignored = true) Spectrum::RGB.from_fraction(g, g, g) end
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.
# File lib/spectrum/grayscale.rb, line 138 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) Spectrum::YIQ.from_fraction(y, i, q) end
Private Instance Methods
# File lib/spectrum/grayscale.rb, line 69 def to_255 [(@g * 255).round, 255].min end