Class Color::GrayScale
In: lib/color.rb
lib/color/grayscale.rb
Parent: Object
Enumerable Gimp CMYK\n[lib/color.rb\nlib/color/cmyk.rb] GrayScale\n[lib/color.rb\nlib/color/grayscale.rb] RGB\n[lib/color.rb\nlib/color/rgb-colors.rb\nlib/color/rgb.rb] YIQ\n[lib/color.rb\nlib/color/yiq.rb] HSL MonoContrast lib/color/cmyk.rb lib/color/grayscale.rb lib/color/rgb.rb lib/color/yiq.rb lib/color/hsl.rb CSS lib/color/palette/gimp.rb lib/color/palette/monocontrast.rb Palette Color dot/m_9_0.png

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

Methods

Constants

PDF_FORMAT_STR = "%.3f %s"   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.

Attributes

g  [RW] 

Public Class methods

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

  Color::GreyScale.from_fraction(0.5)

[Source]

    # File lib/color/grayscale.rb, line 23
23:   def self.from_fraction(g = 0)
24:     color = Color::GrayScale.new
25:     color.g = g
26:     color
27:   end

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

  Color::GrayScale.new(50)

[Source]

    # File lib/color/grayscale.rb, line 32
32:   def initialize(g = 0)
33:     @g = g / 100.0
34:   end

Public Instance methods

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.

[Source]

    # File lib/color/grayscale.rb, line 43
43:   def ==(other)
44:     other = other.to_grayscale
45:     other.kind_of?(Color::GrayScale) and
46:     ((@g - other.g).abs <= 1e-4)
47:   end

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

[Source]

     # File lib/color/grayscale.rb, line 119
119:   def brightness
120:     @g
121:   end

Darken the RGB hue by the stated percent.

[Source]

    # File lib/color/grayscale.rb, line 96
96:   def darken_by(percent)
97:     g = [@g - (@g * (percent / 100.0)), 0.0].max
98:     Color::GrayScale.from_fraction(g)
99:   end

Present the colour as an HTML/CSS colour string.

[Source]

    # File lib/color/grayscale.rb, line 67
67:   def html
68:     gs = "%02x" % to_255
69:     "##{gs * 3}"
70:   end

Lightens the greyscale colour by the stated percent.

[Source]

    # File lib/color/grayscale.rb, line 90
90:   def lighten_by(percent)
91:     g = [@g + (@g * (percent / 100.0)), 1.0].min
92:     Color::GrayScale.from_fraction(g)
93:   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.

[Source]

    # File lib/color/grayscale.rb, line 51
51:   def pdf_fill
52:     PDF_FORMAT_STR % [ @g, "g" ]
53:   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.

[Source]

    # File lib/color/grayscale.rb, line 57
57:   def pdf_stroke
58:     PDF_FORMAT_STR % [ @g, "G" ]
59:   end

Convert the greyscale colour to CMYK.

[Source]

    # File lib/color/grayscale.rb, line 73
73:   def to_cmyk
74:     k = 1.0 - @g.to_f
75:     Color::CMYK.from_fraction(0, 0, 0, k)
76:   end

[Source]

    # File lib/color/grayscale.rb, line 84
84:   def to_grayscale
85:     self
86:   end
to_greyscale()

Alias for to_grayscale

Returns the HSL colour encoding of the greyscale value.

[Source]

     # File lib/color/grayscale.rb, line 113
113:   def to_hsl
114:     Color::HSL.from_fraction(0, 0, @g)
115:   end

Convert the greyscale colour to RGB.

[Source]

    # File lib/color/grayscale.rb, line 79
79:   def to_rgb(ignored = true)
80:     g = to_255
81:     Color::RGB.new(g, g, g)
82:   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.

[Source]

     # File lib/color/grayscale.rb, line 105
105:   def to_yiq
106:     y = @g
107:     i = (@g * 0.596) + (@g * -0.275) + (@g * -0.321)
108:     q = (@g * 0.212) + (@g * -0.523) + (@g *  0.311)
109:     Color::YIQ.from_fraction(y, i, q)
110:   end

Private Instance methods

[Source]

    # File lib/color/grayscale.rb, line 61
61:   def to_255
62:     [(@g * 255).round, 255].min
63:   end

[Validate]