Class Color::RGB
In: lib/color.rb
lib/color/rgb-colors.rb
lib/color/rgb.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

An RGB colour object.

Methods

+   -   ==   adjust_brightness   adjust_hue   adjust_saturation   b   b=   blue   blue=   blue_p   blue_p=   brightness   css_hsl   css_hsla   css_rgb   css_rgba   darken_by   from_fraction   from_html   from_percentage   g   g=   green   green=   green_p   green_p=   html   inspect   lighten_by   max_rgb_as_grayscale   max_rgb_as_greyscale   mix_with   new   pdf_fill   pdf_stroke   r   r=   red   red=   red_p   red_p=   to_cmyk   to_grayscale   to_greyscale   to_hsl   to_rgb   to_yiq  

Classes and Modules

Module Color::RGB::Metallic

Constants

AliceBlue = Color::RGB.new(0xf0, 0xf8, 0xff)
AntiqueWhite = Color::RGB.new(0xfa, 0xeb, 0xd7)
Aqua = Color::RGB.new(0x00, 0xff, 0xff)
Aquamarine = Color::RGB.new(0x7f, 0xff, 0xd4)
Azure = Color::RGB.new(0xf0, 0xff, 0xff)
Beige = Color::RGB.new(0xf5, 0xf5, 0xdc)
Bisque = Color::RGB.new(0xff, 0xe4, 0xc4)
Black = Color::RGB.new(0, 0, 0)
BlanchedAlmond = Color::RGB.new(0xff, 0xeb, 0xcd)
Blue = Color::RGB.new(0x00, 0x00, 0xff)
BlueViolet = Color::RGB.new(0x8a, 0x2b, 0xe2)
Brown = Color::RGB.new(0xa5, 0x2a, 0x2a)
BurlyWood = Color::RGB.new(0xde, 0xb8, 0x87)
Burlywood = BurlyWood
CadetBlue = Color::RGB.new(0x5f, 0x9e, 0xa0)
Carnation = Color::RGB.new(0xff, 0x5e, 0xd0)
Cayenne = Color::RGB.new(0x8d, 0x00, 0x00)
Chartreuse = Color::RGB.new(0x7f, 0xff, 0x00)
Chocolate = Color::RGB.new(0xd2, 0x69, 0x1e)
Coral = Color::RGB.new(0xff, 0x7f, 0x50)
CornflowerBlue = Color::RGB.new(0x64, 0x95, 0xed)
Cornsilk = Color::RGB.new(0xff, 0xf8, 0xdc)
Crimson = Color::RGB.new(0xdc, 0x14, 0x3c)
Cyan = Color::RGB.new(0x00, 0xff, 0xff)
DarkBlue = Color::RGB.new(0x00, 0x00, 0x8b)
DarkCyan = Color::RGB.new(0x00, 0x8b, 0x8b)
DarkGoldenRod = Color::RGB.new(0xb8, 0x86, 0x0b)
DarkGoldenrod = DarkGoldenRod
DarkGray = Color::RGB.new(0xa9, 0xa9, 0xa9)
DarkGreen = Color::RGB.new(0x00, 0x64, 0x00)
DarkGrey = DarkGray
DarkKhaki = Color::RGB.new(0xbd, 0xb7, 0x6b)
DarkMagenta = Color::RGB.new(0x8b, 0x00, 0x8b)
DarkOliveGreen = Color::RGB.new(0x55, 0x6b, 0x2f)
DarkOrange = Color::RGB.new(0xff, 0x8c, 0x00)
DarkOrchid = Color::RGB.new(0x99, 0x32, 0xcc)
DarkRed = Color::RGB.new(0x8b, 0x00, 0x00)
DarkSalmon = Color::RGB.new(0xe9, 0x96, 0x7a)
DarkSeaGreen = Color::RGB.new(0x8f, 0xbc, 0x8f)
DarkSlateBlue = Color::RGB.new(0x48, 0x3d, 0x8b)
DarkSlateGray = Color::RGB.new(0x2f, 0x4f, 0x4f)
DarkSlateGrey = DarkSlateGray
DarkTurquoise = Color::RGB.new(0x00, 0xce, 0xd1)
DarkViolet = Color::RGB.new(0x94, 0x00, 0xd3)
DarkoliveGreen = DarkOliveGreen
Darkorange = Color::RGB.new(0xff, 0x8c, 0x00)
Darksalmon = DarkSalmon
DeepPink = Color::RGB.new(0xff, 0x14, 0x93)
DeepSkyBlue = Color::RGB.new(0x00, 0xbf, 0xbf)
DimGray = Color::RGB.new(0x69, 0x69, 0x69)
DimGrey = DimGray
DodgerBlue = Color::RGB.new(0x1e, 0x90, 0xff)
Feldspar = Color::RGB.new(0xd1, 0x92, 0x75)
FireBrick = Color::RGB.new(0xb2, 0x22, 0x22)
Firebrick = FireBrick
FloralWhite = Color::RGB.new(0xff, 0xfa, 0xf0)
ForestGreen = Color::RGB.new(0x22, 0x8b, 0x22)
Fuchsia = Color::RGB.new(0xff, 0x00, 0xff)
Gainsboro = Color::RGB.new(0xdc, 0xdc, 0xdc)
GhostWhite = Color::RGB.new(0xf8, 0xf8, 0xff)
Gold = Color::RGB.new(0xff, 0xd7, 0x00)
GoldenRod = Color::RGB.new(0xda, 0xa5, 0x20)
Goldenrod = GoldenRod
Gray = Color::RGB.new(0x80, 0x80, 0x80)
Gray10 = Color::RGB.from_percentage(10, 10, 10)
Gray20 = Color::RGB.from_percentage(20, 20, 20)
Gray30 = Color::RGB.from_percentage(30, 30, 30)
Gray40 = Color::RGB.from_percentage(40, 40, 40)
Gray50 = Color::RGB.from_percentage(50, 50, 50)
Gray60 = Color::RGB.from_percentage(60, 60, 60)
Gray70 = Color::RGB.from_percentage(70, 70, 70)
Gray80 = Color::RGB.from_percentage(80, 80, 80)
Gray90 = Color::RGB.from_percentage(90, 90, 90)
Green = Color::RGB.new(0x00, 0x80, 0x00)
GreenYellow = Color::RGB.new(0xad, 0xff, 0x2f)
Grey = Gray
Grey10 = Gray10
Grey20 = Gray20
Grey30 = Gray30
Grey40 = Gray40
Grey50 = Gray50
Grey60 = Gray60
Grey70 = Gray70
Grey80 = Gray80
Grey90 = Gray90
HoneyDew = Color::RGB.new(0xf0, 0xff, 0xf0)
Honeydew = HoneyDew
HotPink = Color::RGB.new(0xff, 0x69, 0xb4)
IndianRed = Color::RGB.new(0xcd, 0x5c, 0x5c)
Indigo = Color::RGB.new(0x4b, 0x00, 0x82)
Ivory = Color::RGB.new(0xff, 0xff, 0xf0)
Khaki = Color::RGB.new(0xf0, 0xe6, 0x8c)
Lavender = Color::RGB.new(0xe6, 0xe6, 0xfa)
LavenderBlush = Color::RGB.new(0xff, 0xf0, 0xf5)
LawnGreen = Color::RGB.new(0x7c, 0xfc, 0x00)
LemonChiffon = Color::RGB.new(0xff, 0xfa, 0xcd)
LightBlue = Color::RGB.new(0xad, 0xd8, 0xe6)
LightCoral = Color::RGB.new(0xf0, 0x80, 0x80)
LightCyan = Color::RGB.new(0xe0, 0xff, 0xff)
LightGoldenRodYellow = Color::RGB.new(0xfa, 0xfa, 0xd2)
LightGoldenrodYellow = LightGoldenRodYellow
LightGray = Color::RGB.new(0xd3, 0xd3, 0xd3)
LightGreen = Color::RGB.new(0x90, 0xee, 0x90)
LightGrey = LightGray
LightPink = Color::RGB.new(0xff, 0xb6, 0xc1)
LightSalmon = Color::RGB.new(0xff, 0xa0, 0x7a)
LightSeaGreen = Color::RGB.new(0x20, 0xb2, 0xaa)
LightSkyBlue = Color::RGB.new(0x87, 0xce, 0xfa)
LightSlateBlue = Color::RGB.new(0x84, 0x70, 0xff)
LightSlateGray = Color::RGB.new(0x77, 0x88, 0x99)
LightSlateGrey = LightSlateGray
LightSteelBlue = Color::RGB.new(0xb0, 0xc4, 0xde)
LightYellow = Color::RGB.new(0xff, 0xff, 0xe0)
Lightsalmon = LightSalmon
LightsteelBlue = LightSteelBlue
Lime = Color::RGB.new(0x00, 0xff, 0x00)
LimeGreen = Color::RGB.new(0x32, 0xcd, 0x32)
Linen = Color::RGB.new(0xfa, 0xf0, 0xe6)
Magenta = Color::RGB.new(0xff, 0x00, 0xff)
Maroon = Color::RGB.new(0x80, 0x00, 0x00)
MediumAquaMarine = Color::RGB.new(0x66, 0xcd, 0xaa)
MediumAquamarine = MediumAquaMarine
MediumBlue = Color::RGB.new(0x00, 0x00, 0xcd)
MediumOrchid = Color::RGB.new(0xba, 0x55, 0xd3)
MediumPurple = Color::RGB.new(0x93, 0x70, 0xdb)
MediumSeaGreen = Color::RGB.new(0x3c, 0xb3, 0x71)
MediumSlateBlue = Color::RGB.new(0x7b, 0x68, 0xee)
MediumSpringGreen = Color::RGB.new(0x00, 0xfa, 0x9a)
MediumTurquoise = Color::RGB.new(0x48, 0xd1, 0xcc)
MediumVioletRed = Color::RGB.new(0xc7, 0x15, 0x85)
MidnightBlue = Color::RGB.new(0x19, 0x19, 0x70)
MintCream = Color::RGB.new(0xf5, 0xff, 0xfa)
MistyRose = Color::RGB.new(0xff, 0xe4, 0xe1)
Moccasin = Color::RGB.new(0xff, 0xe4, 0xb5)
NavajoWhite = Color::RGB.new(0xff, 0xde, 0xad)
Navy = Color::RGB.new(0x00, 0x00, 0x80)
OldLace = Color::RGB.new(0xfd, 0xf5, 0xe6)
Olive = Color::RGB.new(0x80, 0x80, 0x00)
OliveDrab = Color::RGB.new(0x6b, 0x8e, 0x23)
Olivedrab = OliveDrab
Orange = Color::RGB.new(0xff, 0xa5, 0x00)
OrangeRed = Color::RGB.new(0xff, 0x45, 0x00)
Orchid = Color::RGB.new(0xda, 0x70, 0xd6)
PaleGoldenRod = Color::RGB.new(0xee, 0xe8, 0xaa)
PaleGoldenrod = PaleGoldenRod
PaleGreen = Color::RGB.new(0x98, 0xfb, 0x98)
PaleTurquoise = Color::RGB.new(0xaf, 0xee, 0xee)
PaleVioletRed = Color::RGB.new(0xdb, 0x70, 0x93)
PapayaWhip = Color::RGB.new(0xff, 0xef, 0xd5)
PeachPuff = Color::RGB.new(0xff, 0xda, 0xb9)
Peachpuff = PeachPuff
Peru = Color::RGB.new(0xcd, 0x85, 0x3f)
Pink = Color::RGB.new(0xff, 0xc0, 0xcb)
Plum = Color::RGB.new(0xdd, 0xa0, 0xdd)
PowderBlue = Color::RGB.new(0xb0, 0xe0, 0xe6)
Purple = Color::RGB.new(0x80, 0x00, 0x80)
Red = Color::RGB.new(0xff, 0x00, 0x00)
RosyBrown = Color::RGB.new(0xbc, 0x8f, 0x8f)
RoyalBlue = Color::RGB.new(0x41, 0x69, 0xe1)
SaddleBrown = Color::RGB.new(0x8b, 0x45, 0x13)
Salmon = Color::RGB.new(0xfa, 0x80, 0x72)
SandyBrown = Color::RGB.new(0xf4, 0xa4, 0x60)
SeaGreen = Color::RGB.new(0x2e, 0x8b, 0x57)
SeaShell = Color::RGB.new(0xff, 0xf5, 0xee)
Seashell = SeaShell
Sienna = Color::RGB.new(0xa0, 0x52, 0x2d)
Silver = Color::RGB.new(0xc0, 0xc0, 0xc0)
SkyBlue = Color::RGB.new(0x87, 0xce, 0xeb)
SlateBlue = Color::RGB.new(0x6a, 0x5a, 0xcd)
SlateGray = Color::RGB.new(0x70, 0x80, 0x90)
SlateGrey = SlateGray
Snow = Color::RGB.new(0xff, 0xfa, 0xfa)
SpringGreen = Color::RGB.new(0x00, 0xff, 0x7f)
SteelBlue = Color::RGB.new(0x46, 0x82, 0xb4)
Tan = Color::RGB.new(0xd2, 0xb4, 0x8c)
Teal = Color::RGB.new(0x00, 0x80, 0x80)
Thistle = Color::RGB.new(0xd8, 0xbf, 0xd8)
Tomato = Color::RGB.new(0xff, 0x63, 0x47)
Turquoise = Color::RGB.new(0x40, 0xe0, 0xd0)
Violet = Color::RGB.new(0xee, 0x82, 0xee)
VioletRed = Color::RGB.new(0xd0, 0x20, 0x90)
Wheat = Color::RGB.new(0xf5, 0xde, 0xb3)
White = Color::RGB.new(0xff, 0xff, 0xff)
WhiteSmoke = Color::RGB.new(0xf5, 0xf5, 0xf5)
Yellow = Color::RGB.new(0xff, 0xff, 0x00)
YellowGreen = Color::RGB.new(0x9a, 0xcd, 0x32)
PDF_FORMAT_STR = "%.3f %.3f %.3f %s"   The format of a DeviceRGB 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 an RGB colour object from fractional values 0..1.

  Color::RGB.from_fraction(.3, .2, .1)

[Source]

    # File lib/color/rgb.rb, line 33
33:     def from_fraction(r = 0.0, g = 0.0, b = 0.0)
34:       colour = Color::RGB.new
35:       colour.r = r
36:       colour.g = g
37:       colour.b = b
38:       colour
39:     end

Creates an RGB colour object from an HTML colour descriptor (e.g., "fed" or "cabbed;".

  Color::RGB.from_html("fed")
  Color::RGB.from_html("#fed")
  Color::RGB.from_html("#cabbed")
  Color::RGB.from_html("cabbed")

[Source]

    # File lib/color/rgb.rb, line 48
48:     def from_html(html_colour)
49:       html_colour = html_colour.gsub(%r{[#;]}, '')
50:       case html_colour.size 
51:       when 3
52:         colours = html_colour.scan(%r{[0-9A-Fa-f]}).map { |el| (el * 2).to_i(16) }
53:       when 6
54:         colours = html_colour.scan(%r<[0-9A-Fa-f]{2}>).map { |el| el.to_i(16) }
55:       else
56:         raise ArgumentError
57:       end
58: 
59:       Color::RGB.new(*colours)
60:     end

Creates an RGB colour object from percentages 0..100.

  Color::RGB.from_percentage(10, 20 30)

[Source]

    # File lib/color/rgb.rb, line 26
26:     def from_percentage(r = 0, g = 0, b = 0)
27:       from_fraction(r / 100.0, g / 100.0, b / 100.0)
28:     end

Creates an RGB colour object from the standard range 0..255.

  Color::RGB.new(32, 64, 128)
  Color::RGB.new(0x20, 0x40, 0x80)

[Source]

    # File lib/color/rgb.rb, line 82
82:   def initialize(r = 0, g = 0, b = 0)
83:     @r = r / 255.0
84:     @g = g / 255.0
85:     @b = b / 255.0
86:   end

Public Instance methods

Adds another colour to the current colour. The other colour will be converted to RGB before addition. This conversion depends upon a to_rgb method on the other colour.

The addition is done using the RGB Accessor methods to ensure a valid colour in the result.

[Source]

     # File lib/color/rgb.rb, line 415
415:   def +(other)
416:     other = other.to_rgb
417:     rgb = self.dup
418: 
419:     rgb.r += other.r
420:     rgb.g += other.g
421:     rgb.b += other.b
422: 
423:     rgb
424:   end

Subtracts another colour to the current colour. The other colour will be converted to RGB before subtraction. This conversion depends upon a to_rgb method on the other colour.

The subtraction is done using the RGB Accessor methods to ensure a valid colour in the result.

[Source]

     # File lib/color/rgb.rb, line 432
432:   def -(other) 
433:     other = other.to_rgb 
434:     rgb = self.dup
435: 
436:     rgb.r -= other.r
437:     rgb.g -= other.g
438:     rgb.b -= other.b
439: 
440:     rgb
441:   end

Compares the other colour to this one. The other colour will be converted to RGB before comparison, so the comparison between a RGB colour and a non-RGB colour will be approximate and based on the other colour‘s default to_rgb conversion. If there is no to_rgb conversion, this will raise an exception. This will report that two RGB colours are equivalent if all component values are within COLOR_TOLERANCE of each other.

[Source]

    # File lib/color/rgb.rb, line 70
70:   def ==(other)
71:     other = other.to_rgb
72:     other.kind_of?(Color::RGB) and
73:     ((@r - other.r).abs <= Color::COLOR_TOLERANCE) and
74:     ((@g - other.g).abs <= Color::COLOR_TOLERANCE) and
75:     ((@b - other.b).abs <= Color::COLOR_TOLERANCE)
76:   end

Returns a new colour with the brightness adjusted by the specified percentage. Negative percentages will darken the colour; positive percentages will brighten the colour.

  Color::RGB::DarkBlue.adjust_brightness(10)
  Color::RGB::DarkBlue.adjust_brightness(-10)

[Source]

     # File lib/color/rgb.rb, line 283
283:   def adjust_brightness(percent)
284:     percent /= 100.0
285:     percent += 1.0
286:     percent  = [ percent, 2.0 ].min
287:     percent  = [ 0.0, percent ].max
288: 
289:     hsl      = to_hsl
290:     hsl.l   *= percent
291:     hsl.to_rgb
292:   end

Returns a new colour with the hue adjusted by the specified percentage. Negative percentages will reduce the hue; positive percentages will increase the hue.

  Color::RGB::DarkBlue.adjust_hue(10)
  Color::RGB::DarkBlue.adjust_hue(-10)

[Source]

     # File lib/color/rgb.rb, line 317
317:   def adjust_hue(percent)
318:     percent  /= 100.0
319:     percent  += 1.0
320:     percent  = [ percent, 2.0 ].min
321:     percent  = [ 0.0, percent ].max
322: 
323:     hsl      = to_hsl
324:     hsl.h   *= percent
325:     hsl.to_rgb
326:   end

Returns a new colour with the saturation adjusted by the specified percentage. Negative percentages will reduce the saturation; positive percentages will increase the saturation.

  Color::RGB::DarkBlue.adjust_saturation(10)
  Color::RGB::DarkBlue.adjust_saturation(-10)

[Source]

     # File lib/color/rgb.rb, line 300
300:   def adjust_saturation(percent)
301:     percent  /= 100.0
302:     percent  += 1.0
303:     percent  = [ percent, 2.0 ].min
304:     percent  = [ 0.0, percent ].max
305: 
306:     hsl      = to_hsl
307:     hsl.s   *= percent
308:     hsl.to_rgb
309:   end

Returns the blue component of the colour as a fraction in the range 0.0 .. 1.0.

[Source]

     # File lib/color/rgb.rb, line 392
392:   def b
393:     @b
394:   end

Sets the blue component of the colour as a fraction in the range 0.0 .. 1.0.

[Source]

     # File lib/color/rgb.rb, line 405
405:   def b=(bb)
406:     @b = Color.normalize(bb)
407:   end

Returns the blue component of the colour in the normal 0 .. 255 range.

[Source]

     # File lib/color/rgb.rb, line 383
383:   def blue
384:     @b * 255.0
385:   end

Sets the blue component of the colour in the normal 0 .. 255 range.

[Source]

     # File lib/color/rgb.rb, line 396
396:   def blue=(bb)
397:     @b = Color.normalize(bb / 255.0)
398:   end

Returns the blue component of the colour as a percentage.

[Source]

     # File lib/color/rgb.rb, line 387
387:   def blue_p
388:     @b * 100.0
389:   end

Sets the blue component of the colour as a percentage.

[Source]

     # File lib/color/rgb.rb, line 400
400:   def blue_p=(bb)
401:     @b = Color.normalize(bb / 100.0)
402:   end

Returns the brightness value for a colour, a number between 0..1. Based on the Y value of YIQ encoding, representing luminosity, or perceived brightness.

This may be modified in a future version of color-tools to use the luminosity value of HSL.

[Source]

     # File lib/color/rgb.rb, line 268
268:   def brightness
269:     to_yiq.y
270:   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 using the default conversion formula.

[Source]

     # File lib/color/rgb.rb, line 131
131:   def css_hsl
132:     to_hsl.css_hsl
133:   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 using the default conversion formula.

[Source]

     # File lib/color/rgb.rb, line 138
138:   def css_hsla
139:     to_hsl.css_hsla
140:   end

Present the colour as an RGB HTML/CSS colour string (e.g., "rgb(0%, 50%, 100%)"). Note that this will perform a to_rgb operation using the default conversion formula.

[Source]

     # File lib/color/rgb.rb, line 117
117:   def css_rgb
118:     "rgb(%3.2f%%, %3.2f%%, %3.2f%%)" % [ red_p, green_p, blue_p ]
119:   end

Present the colour as an RGBA (with alpha) HTML/CSS colour string (e.g., "rgb(0%, 50%, 100%, 1)"). Note that this will perform a to_rgb operation using the default conversion formula.

[Source]

     # File lib/color/rgb.rb, line 124
124:   def css_rgba
125:     "rgba(%3.2f%%, %3.2f%%, %3.2f%%, %3.2f)" % [ red_p, green_p, blue_p, 1 ]
126:   end

Mix the RGB hue with Black so that the RGB hue is the specified percentage of the resulting colour. Strictly speaking, this isn‘t a darken_by operation.

[Source]

     # File lib/color/rgb.rb, line 245
245:   def darken_by(percent)
246:     mix_with(Black, percent)
247:   end

Returns the green component of the colour as a fraction in the range 0.0 .. 1.0.

[Source]

     # File lib/color/rgb.rb, line 365
365:   def g
366:     @g
367:   end

Sets the green component of the colour as a fraction in the range 0.0 .. 1.0.

[Source]

     # File lib/color/rgb.rb, line 378
378:   def g=(gg)
379:     @g = Color.normalize(gg)
380:   end

Returns the green component of the colour in the normal 0 .. 255 range.

[Source]

     # File lib/color/rgb.rb, line 356
356:   def green
357:     @g * 255.0
358:   end

Sets the green component of the colour in the normal 0 .. 255 range.

[Source]

     # File lib/color/rgb.rb, line 369
369:   def green=(gg)
370:     @g = Color.normalize(gg / 255.0)
371:   end

Returns the green component of the colour as a percentage.

[Source]

     # File lib/color/rgb.rb, line 360
360:   def green_p
361:     @g * 100.0
362:   end

Sets the green component of the colour as a percentage.

[Source]

     # File lib/color/rgb.rb, line 373
373:   def green_p=(gg)
374:     @g = Color.normalize(gg / 100.0)
375:   end

Present the colour as an HTML/CSS colour string.

[Source]

     # File lib/color/rgb.rb, line 101
101:   def html
102:     r = (@r * 255).round
103:     r = 255 if r > 255
104: 
105:     g = (@g * 255).round
106:     g = 255 if g > 255
107: 
108:     b = (@b * 255).round
109:     b = 255 if b > 255
110: 
111:     "#%02x%02x%02x" % [ r, g, b ]
112:   end

[Source]

     # File lib/color/rgb.rb, line 450
450:   def inspect
451:     "RGB [#{html}]"
452:   end

Mix the RGB hue with White so that the RGB hue is the specified percentage of the resulting colour. Strictly speaking, this isn‘t a darken_by operation.

[Source]

     # File lib/color/rgb.rb, line 238
238:   def lighten_by(percent)
239:     mix_with(White, percent)
240:   end

Retrieve the maxmum RGB value from the current colour as a GrayScale colour

[Source]

     # File lib/color/rgb.rb, line 445
445:   def max_rgb_as_grayscale
446:       Color::GrayScale.from_fraction([@r, @g, @b].max)
447:   end
max_rgb_as_greyscale()

Mix the mask colour (which must be an RGB object) with the current colour at the stated opacity percentage (0..100).

[Source]

     # File lib/color/rgb.rb, line 251
251:   def mix_with(mask, opacity)
252:     opacity /= 100.0
253:     rgb = self.dup
254:     
255:     rgb.r = (@r * opacity) + (mask.r * (1 - opacity))
256:     rgb.g = (@g * opacity) + (mask.g * (1 - opacity))
257:     rgb.b = (@b * opacity) + (mask.b * (1 - opacity))
258: 
259:     rgb
260:   end

Present the colour as a DeviceRGB fill colour string for PDF. This will be removed from the default package in color-tools 2.0.

[Source]

    # File lib/color/rgb.rb, line 90
90:   def pdf_fill
91:     PDF_FORMAT_STR % [ @r, @g, @b, "rg" ]
92:   end

Present the colour as a DeviceRGB stroke colour string for PDF. This will be removed from the default package in color-tools 2.0.

[Source]

    # File lib/color/rgb.rb, line 96
96:   def pdf_stroke
97:     PDF_FORMAT_STR % [ @r, @g, @b, "RG" ]
98:   end

Returns the red component of the colour as a fraction in the range 0.0 .. 1.0.

[Source]

     # File lib/color/rgb.rb, line 338
338:   def r
339:     @r
340:   end

Sets the red component of the colour as a fraction in the range 0.0 .. 1.0.

[Source]

     # File lib/color/rgb.rb, line 351
351:   def r=(rr)
352:     @r = Color.normalize(rr)
353:   end

Returns the red component of the colour in the normal 0 .. 255 range.

[Source]

     # File lib/color/rgb.rb, line 329
329:   def red
330:     @r * 255.0
331:   end

Sets the red component of the colour in the normal 0 .. 255 range.

[Source]

     # File lib/color/rgb.rb, line 342
342:   def red=(rr)
343:     @r = Color.normalize(rr / 255.0)
344:   end

Returns the red component of the colour as a percentage.

[Source]

     # File lib/color/rgb.rb, line 333
333:   def red_p
334:     @r * 100.0
335:   end

Sets the red component of the colour as a percentage.

[Source]

     # File lib/color/rgb.rb, line 346
346:   def red_p=(rr)
347:     @r = Color.normalize(rr / 100.0)
348:   end

Converts the RGB colour to CMYK. Most colour experts strongly suggest that this is not a good idea (some even suggesting that it‘s a very bad idea). CMYK represents additive percentages of inks on white paper, whereas RGB represents mixed colour intensities on a black screen.

However, the colour conversion can be done. The basic method is multi-step:

  1. Convert the R, G, and B components to C, M, and Y components.
     c = 1.0 - r
     m = 1.0 - g
     y = 1.0 - b
    
  2. Compute the minimum amount of black (K) required to smooth the colour in inks.
     k = min(c, m, y)
    
  3. Perform undercolour removal on the C, M, and Y components of the colours because less of each colour is needed for each bit of black. Also, regenerate the black (K) based on the undercolour removal so that the colour is more accurately represented in ink.
     c = min(1.0, max(0.0, c - UCR(k)))
     m = min(1.0, max(0.0, m - UCR(k)))
     y = min(1.0, max(0.0, y - UCR(k)))
     k = min(1.0, max(0.0, BG(k)))
    

The undercolour removal function and the black generation functions return a value based on the brightness of the RGB colour.

[Source]

     # File lib/color/rgb.rb, line 168
168:   def to_cmyk
169:     c = 1.0 - @r.to_f
170:     m = 1.0 - @g.to_f
171:     y = 1.0 - @b.to_f
172: 
173:     k = [c, m, y].min
174:     k = k - (k * brightness)
175: 
176:     c = [1.0, [0.0, c - k].max].min
177:     m = [1.0, [0.0, m - k].max].min
178:     y = [1.0, [0.0, y - k].max].min
179:     k = [1.0, [0.0, k].max].min
180: 
181:     Color::CMYK.from_fraction(c, m, y, k)
182:   end

Convert to grayscale.

[Source]

     # File lib/color/rgb.rb, line 272
272:   def to_grayscale
273:     Color::GrayScale.from_fraction(to_hsl.l)
274:   end
to_greyscale()

Alias for to_grayscale

Returns the HSL colour encoding of the RGB value. The conversions here are based on forumlas from www.easyrgb.com/math.php and elsewhere.

[Source]

     # File lib/color/rgb.rb, line 199
199:   def to_hsl
200:     min   = [ @r, @g, @b ].min
201:     max   = [ @r, @g, @b ].max
202:     delta = (max - min).to_f
203: 
204:     lum   = (max + min) / 2.0
205: 
206:     if Color.near_zero?(delta) # close to 0.0, so it's a grey
207:       hue = 0
208:       sat = 0
209:     else
210:       if Color.near_zero_or_less?(lum - 0.5)
211:         sat = delta / (max + min).to_f
212:       else
213:         sat = delta / (2 - max - min).to_f
214:       end
215: 
216:       # This is based on the conversion algorithm from
217:       # http://en.wikipedia.org/wiki/HSV_color_space#Conversion_from_RGB_to_HSL_or_HSV
218:       # Contributed by Adam Johnson
219:       sixth = 1 / 6.0
220:       if @r == max # Color.near_zero_or_less?(@r - max)
221:         hue = (sixth * ((@g - @b) / delta))
222:         hue += 1.0 if @g < @b
223:       elsif @g == max # Color.near_zero_or_less(@g - max)
224:         hue = (sixth * ((@b - @r) / delta)) + (1.0 / 3.0)
225:       elsif @b == max # Color.near_zero_or_less?(@b - max)
226:         hue = (sixth * ((@r - @g) / delta)) + (2.0 / 3.0)
227:       end
228: 
229:       hue += 1 if hue < 0
230:       hue -= 1 if hue > 1
231:     end
232:     Color::HSL.from_fraction(hue, sat, lum)
233:   end

[Source]

     # File lib/color/rgb.rb, line 184
184:   def to_rgb(ignored = nil)
185:     self
186:   end

Returns the YIQ (NTSC) colour encoding of the RGB value.

[Source]

     # File lib/color/rgb.rb, line 189
189:   def to_yiq
190:     y = (@r * 0.299) + (@g *  0.587) + (@b *  0.114)
191:     i = (@r * 0.596) + (@g * -0.275) + (@b * -0.321)
192:     q = (@r * 0.212) + (@g * -0.523) + (@b *  0.311)
193:     Color::YIQ.from_fraction(y, i, q)
194:   end

[Validate]