Class Color::GrayScale
In: lib/color.rb
lib/color/grayscale.rb
Parent: Object
Enumerable Gimp AdobeColor 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/adobecolor.rb lib/color/palette/monocontrast.rb Palette Color dot/m_10_0.png

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

Methods

+   -   ==   brightness   css_hsl   css_hsla   css_rgb   css_rgba   darken_by   from_fraction   from_percent   g   g=   gray   gray=   grey   grey=   html   inspect   lighten_by   new   pdf_fill   pdf_stroke   to_255   to_cmyk   to_grayscale   to_greyscale   to_hsl   to_rgb   to_yiq  

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.

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 26
26:   def self.from_fraction(g = 0)
27:     color = Color::GrayScale.new
28:     color.g = g
29:     color
30:   end

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

  Color::GrayScale.from_percent(50)

[Source]

    # File lib/color/grayscale.rb, line 35
35:   def self.from_percent(g = 0)
36:     Color::GrayScale.new(g)
37:   end

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

  Color::GrayScale.new(50)

[Source]

    # File lib/color/grayscale.rb, line 42
42:   def initialize(g = 0)
43:     @g = g / 100.0
44:   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.

[Source]

     # File lib/color/grayscale.rb, line 186
186:   def +(other)
187:     other = other.to_grayscale
188:     ng = self.dup
189:     ng.g += other.g
190:     ng
191:   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.

[Source]

     # File lib/color/grayscale.rb, line 199
199:   def -(other)
200:     other = other.to_grayscale 
201:     ng = self.dup
202:     ng.g -= other.g
203:     ng
204:   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.

[Source]

    # File lib/color/grayscale.rb, line 53
53:   def ==(other)
54:     other = other.to_grayscale
55:     other.kind_of?(Color::GrayScale) and
56:     ((@g - other.g).abs <= Color::COLOR_TOLERANCE)
57:   end

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

[Source]

     # File lib/color/grayscale.rb, line 154
154:   def brightness
155:     @g
156:   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.

[Source]

    # File lib/color/grayscale.rb, line 96
96:   def css_hsl
97:     to_hsl.css_hsl
98:   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.

[Source]

     # File lib/color/grayscale.rb, line 103
103:   def css_hsla
104:     to_hsl.css_hsla
105:   end

Present the colour as an RGB HTML/CSS colour string (e.g., "rgb(0%, 50%, 100%)").

[Source]

    # File lib/color/grayscale.rb, line 84
84:   def css_rgb
85:     "rgb(%3.2f%%, %3.2f%%, %3.2f%%)" % [ gray, gray, gray ]
86:   end

Present the colour as an RGBA (with alpha) HTML/CSS colour string (e.g., "rgb(0%, 50%, 100%, 1)").

[Source]

    # File lib/color/grayscale.rb, line 90
90:   def css_rgba
91:     "rgba(%3.2f%%, %3.2f%%, %3.2f%%, %1.2f)" % [ gray, gray, gray, 1 ]
92:   end

Darken the greyscale colour by the stated percent.

[Source]

     # File lib/color/grayscale.rb, line 131
131:   def darken_by(percent)
132:     g = [@g - (@g * (percent / 100.0)), 0.0].max
133:     Color::GrayScale.from_fraction(g)
134:   end

Returns the grayscale value as a fractional value of white in the range 0.0 .. 1.0.

[Source]

     # File lib/color/grayscale.rb, line 166
166:   def g
167:     @g
168:   end

Returns the grayscale value as a fractional value of white in the range 0.0 .. 1.0.

[Source]

     # File lib/color/grayscale.rb, line 176
176:   def g=(gg)
177:     @g = Color.normalize(gg)
178:   end

Returns the grayscale value as a percentage of white (100% gray is white).

[Source]

     # File lib/color/grayscale.rb, line 160
160:   def gray
161:     @g * 100.0
162:   end

Sets the grayscale value as a percentage of white.

[Source]

     # File lib/color/grayscale.rb, line 170
170:   def gray=(gg)
171:     @g = Color.normalize(gg / 100.0)
172:   end
grey()

Alias for gray

grey=(gg)

Alias for gray=

Present the colour as an HTML/CSS colour string.

[Source]

    # File lib/color/grayscale.rb, line 77
77:   def html
78:     gs = "%02x" % to_255
79:     "##{gs * 3}"
80:   end

[Source]

     # File lib/color/grayscale.rb, line 206
206:   def inspect
207:     "Gray [%.2f%%]" % [ gray ]
208:   end

Lightens the greyscale colour by the stated percent.

[Source]

     # File lib/color/grayscale.rb, line 125
125:   def lighten_by(percent)
126:     g = [@g + (@g * (percent / 100.0)), 1.0].min
127:     Color::GrayScale.from_fraction(g)
128:   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 61
61:   def pdf_fill
62:     PDF_FORMAT_STR % [ @g, "g" ]
63:   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 67
67:   def pdf_stroke
68:     PDF_FORMAT_STR % [ @g, "G" ]
69:   end

Convert the greyscale colour to CMYK.

[Source]

     # File lib/color/grayscale.rb, line 108
108:   def to_cmyk
109:     k = 1.0 - @g.to_f
110:     Color::CMYK.from_fraction(0, 0, 0, k)
111:   end

Reflexive conversion.

[Source]

     # File lib/color/grayscale.rb, line 119
119:   def to_grayscale
120:     self
121:   end
to_greyscale()

Alias for to_grayscale

Returns the HSL colour encoding of the greyscale value.

[Source]

     # File lib/color/grayscale.rb, line 148
148:   def to_hsl
149:     Color::HSL.from_fraction(0, 0, @g)
150:   end

Convert the greyscale colour to RGB.

[Source]

     # File lib/color/grayscale.rb, line 114
114:   def to_rgb(ignored = true)
115:     Color::RGB.from_fraction(g, g, g)
116:   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 140
140:   def to_yiq
141:     y = @g
142:     i = (@g * 0.596) + (@g * -0.275) + (@g * -0.321)
143:     q = (@g * 0.212) + (@g * -0.523) + (@g *  0.311)
144:     Color::YIQ.from_fraction(y, i, q)
145:   end

Private Instance methods

[Source]

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

[Validate]