Class: Color::RGB

Inherits:
Object
  • Object
show all
Defined in:
lib/rmthemegen/color/color/rgb.rb,
lib/rmthemegen/color/color.rb,
lib/rmthemegen/rgb_contrast_methods.rb,
lib/rmthemegen/color/color/rgb-colors.rb

Overview

– Color Colour management with Ruby rubyforge.org/projects/color

Version 1.4.1

Licensed under a MIT-style licence. See Licence.txt in the main distribution for full licensing information.

Copyright © 2005 - 2010 Austin Ziegler and Matt Lyon ++

Defined Under Namespace

Modules: Metallic

Constant Summary collapse

PDF_FORMAT_STR =

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.

"%.3f %.3f %.3f %s"
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)
Color::RGB.new(0xff, 0xde, 0xad)
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)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(r = 0, g = 0, b = 0) ⇒ RGB

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

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


80
81
82
83
84
# File 'lib/rmthemegen/color/color/rgb.rb', line 80

def initialize(r = 0, g = 0, b = 0)
  @r = r / 255.0
  @g = g / 255.0
  @b = b / 255.0
end

Class Method Details

.from_fraction(r = 0.0, g = 0.0, b = 0.0) ⇒ Object

Creates an RGB colour object from fractional values 0..1.

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


31
32
33
34
35
36
37
# File 'lib/rmthemegen/color/color/rgb.rb', line 31

def from_fraction(r = 0.0, g = 0.0, b = 0.0)
  colour = Color::RGB.new
  colour.r = r
  colour.g = g
  colour.b = b
  colour
end

.from_html(html_colour) ⇒ Object

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")


46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rmthemegen/color/color/rgb.rb', line 46

def from_html(html_colour)
  html_colour = html_colour.gsub(%r{[#;]}, '')
  case html_colour.size 
  when 3
    colours = html_colour.scan(%r{[0-9A-Fa-f]}).map { |el| (el * 2).to_i(16) }
  when 6
    colours = html_colour.scan(%r<[0-9A-Fa-f]{2}>).map { |el| el.to_i(16) }
  else
    raise ArgumentError
  end

  Color::RGB.new(*colours)
end

.from_percentage(r = 0, g = 0, b = 0) ⇒ Object

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

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


24
25
26
# File 'lib/rmthemegen/color/color/rgb.rb', line 24

def from_percentage(r = 0, g = 0, b = 0)
  from_fraction(r / 100.0, g / 100.0, b / 100.0)
end

Instance Method Details

#+(other) ⇒ Object

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.



413
414
415
416
417
418
419
420
421
422
# File 'lib/rmthemegen/color/color/rgb.rb', line 413

def +(other)
  other = other.to_rgb
  rgb = self.dup

  rgb.r += other.r
  rgb.g += other.g
  rgb.b += other.b

  rgb
end

#-(other) ⇒ Object

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.



430
431
432
433
434
435
436
437
438
439
# File 'lib/rmthemegen/color/color/rgb.rb', line 430

def -(other) 
  other = other.to_rgb 
  rgb = self.dup

  rgb.r -= other.r
  rgb.g -= other.g
  rgb.b -= other.b

  rgb
end

#==(other) ⇒ Object

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.



68
69
70
71
72
73
74
# File 'lib/rmthemegen/color/color/rgb.rb', line 68

def ==(other)
  other = other.to_rgb
  other.kind_of?(Color::RGB) and
  ((@r - other.r).abs <= Color::COLOR_TOLERANCE) and
  ((@g - other.g).abs <= Color::COLOR_TOLERANCE) and
  ((@b - other.b).abs <= Color::COLOR_TOLERANCE)
end

#adjust_brightness(percent) ⇒ Object

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)


281
282
283
284
285
286
287
288
289
290
# File 'lib/rmthemegen/color/color/rgb.rb', line 281

def adjust_brightness(percent)
  percent /= 100.0
  percent += 1.0
  percent  = [ percent, 2.0 ].min
  percent  = [ 0.0, percent ].max

  hsl      = to_hsl
  hsl.l   *= percent
  hsl.to_rgb
end

#adjust_hue(percent) ⇒ Object

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)


315
316
317
318
319
320
321
322
323
324
# File 'lib/rmthemegen/color/color/rgb.rb', line 315

def adjust_hue(percent)
  percent  /= 100.0
  percent  += 1.0
  percent  = [ percent, 2.0 ].min
  percent  = [ 0.0, percent ].max

  hsl      = to_hsl
  hsl.h   *= percent
  hsl.to_rgb
end

#adjust_saturation(percent) ⇒ Object

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)


298
299
300
301
302
303
304
305
306
307
# File 'lib/rmthemegen/color/color/rgb.rb', line 298

def adjust_saturation(percent)
  percent  /= 100.0
  percent  += 1.0
  percent  = [ percent, 2.0 ].min
  percent  = [ 0.0, percent ].max

  hsl      = to_hsl
  hsl.s   *= percent
  hsl.to_rgb
end

#bObject

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



390
391
392
# File 'lib/rmthemegen/color/color/rgb.rb', line 390

def b
  @b
end

#b=(bb) ⇒ Object

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



403
404
405
# File 'lib/rmthemegen/color/color/rgb.rb', line 403

def b=(bb)
  @b = Color.normalize(bb)
end

#blueObject

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



381
382
383
# File 'lib/rmthemegen/color/color/rgb.rb', line 381

def blue
  @b * 255.0
end

#blue=(bb) ⇒ Object

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



394
395
396
# File 'lib/rmthemegen/color/color/rgb.rb', line 394

def blue=(bb)
  @b = Color.normalize(bb / 255.0)
end

#blue_pObject

Returns the blue component of the colour as a percentage.



385
386
387
# File 'lib/rmthemegen/color/color/rgb.rb', line 385

def blue_p
  @b * 100.0
end

#blue_p=(bb) ⇒ Object

Sets the blue component of the colour as a percentage.



398
399
400
# File 'lib/rmthemegen/color/color/rgb.rb', line 398

def blue_p=(bb)
  @b = Color.normalize(bb / 100.0)
end

#brightnessObject

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.



266
267
268
# File 'lib/rmthemegen/color/color/rgb.rb', line 266

def brightness
  to_yiq.y
end

#contrast(other_rgb) ⇒ Object

Outputs how much contrast this color has with another rgb color. Computes the same regardless of which one is considered foreground. If the other color does not have a to_rgb method, this will throw an exception anything over about 0.22 should have a high likelihood of begin legible. otherwise, to be safe go with something > 0.3



458
459
460
461
462
463
464
465
466
467
468
469
# File 'lib/rmthemegen/color/color/rgb.rb', line 458

def contrast(other_rgb)
  if other_rgb.respond_to? :to_rgb then
    c2 = other_rgb.to_rgb
  else
    raise "rgb.rb unable to calculate contrast with object #{other_rgb.to_s}"
  end 
  #the following numbers have been set with some care.
  return ( 
  self.diff_bri(other_rgb)*0.65 + 
  self.diff_hue(other_rgb)*0.20 + 
  self.diff_lum(other_rgb)*0.15 ) 
end

#css_hslObject

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.



129
130
131
# File 'lib/rmthemegen/color/color/rgb.rb', line 129

def css_hsl
  to_hsl.css_hsl
end

#css_hslaObject

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.



136
137
138
# File 'lib/rmthemegen/color/color/rgb.rb', line 136

def css_hsla
  to_hsl.css_hsla
end

#css_rgbObject

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.



115
116
117
# File 'lib/rmthemegen/color/color/rgb.rb', line 115

def css_rgb
  "rgb(%3.2f%%, %3.2f%%, %3.2f%%)" % [ red_p, green_p, blue_p ]
end

#css_rgbaObject

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.



122
123
124
# File 'lib/rmthemegen/color/color/rgb.rb', line 122

def css_rgba
  "rgba(%3.2f%%, %3.2f%%, %3.2f%%, %3.2f)" % [ red_p, green_p, blue_p, 1 ]
end

#darken_by(percent) ⇒ Object

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.



243
244
245
# File 'lib/rmthemegen/color/color/rgb.rb', line 243

def darken_by(percent)
  mix_with(Black, percent)
end

#diff_bri(rgb) ⇒ Object

provides the brightness difference.



487
488
489
490
491
492
# File 'lib/rmthemegen/color/color/rgb.rb', line 487

def diff_bri(rgb)
  rgb=rgb.to_rgb
	br1 = (299 * rgb.r + 587 * rgb.g + 114 * rgb.b) ;
br2 = (299 * self.r + 587 * self.g + 114 * self.b) ;
return (br1-br2).abs/1000; 
end

#diff_hue(rgb) ⇒ Object

difference in the two colors’ hue



503
504
505
506
507
508
# File 'lib/rmthemegen/color/color/rgb.rb', line 503

def diff_hue(rgb)
  rgb=rgb.to_rgb
  return ((self.r-rgb.r).abs +
         (self.g-rgb.g).abs +
         (self.b-rgb.b).abs)/3 
end

#diff_lum(rgb) ⇒ Object

provides the luminosity difference between two rbg vals



473
474
475
476
477
478
479
480
481
482
483
484
# File 'lib/rmthemegen/color/color/rgb.rb', line 473

def diff_lum(rgb)
  rgb=rgb.to_rgb
 l1 = 0.2126 * (rgb.r) ** 2.2 +
       0.7152 * (rgb.b) ** 2.2 +
       0.0722 * (rgb.g) ** 2.2;
 
  l2 = 0.2126 *  (self.r) ** 2.2 +
        0.7152 * (self.b) ** 2.2 +
        0.0722 * (self.g) ** 2.2;
    
   return ( ( ([l1,l2].max) + 0.05 )/ ( ([l1,l2].min) + 0.05 ) - 1 ) / 20  
end

#diff_pyt(rgb) ⇒ Object

provides the euclidean distance between the two color values



495
496
497
498
499
500
# File 'lib/rmthemegen/color/color/rgb.rb', line 495

def diff_pyt(rgb)
  rgb=rgb.to_rgb
  (((rgb.r - self.r)**2 + 
  (rgb.g - self.g)**2 + 
  (rgb.b - self.b)**2)**0.5)/(1.7320508075688772)
end

#gObject

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



363
364
365
# File 'lib/rmthemegen/color/color/rgb.rb', line 363

def g
  @g
end

#g=(gg) ⇒ Object

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



376
377
378
# File 'lib/rmthemegen/color/color/rgb.rb', line 376

def g=(gg)
  @g = Color.normalize(gg)
end

#greenObject

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



354
355
356
# File 'lib/rmthemegen/color/color/rgb.rb', line 354

def green
  @g * 255.0
end

#green=(gg) ⇒ Object

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



367
368
369
# File 'lib/rmthemegen/color/color/rgb.rb', line 367

def green=(gg)
  @g = Color.normalize(gg / 255.0)
end

#green_pObject

Returns the green component of the colour as a percentage.



358
359
360
# File 'lib/rmthemegen/color/color/rgb.rb', line 358

def green_p
  @g * 100.0
end

#green_p=(gg) ⇒ Object

Sets the green component of the colour as a percentage.



371
372
373
# File 'lib/rmthemegen/color/color/rgb.rb', line 371

def green_p=(gg)
  @g = Color.normalize(gg / 100.0)
end

#htmlObject

Present the colour as an HTML/CSS colour string.



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/rmthemegen/color/color/rgb.rb', line 99

def html
  r = (@r * 255).round
  r = 255 if r > 255

  g = (@g * 255).round
  g = 255 if g > 255

  b = (@b * 255).round
  b = 255 if b > 255

  "#%02x%02x%02x" % [ r, g, b ]
end

#inspectObject



448
449
450
# File 'lib/rmthemegen/color/color/rgb.rb', line 448

def inspect
  "RGB [#{html}]"
end

#lighten_by(percent) ⇒ Object

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.



236
237
238
# File 'lib/rmthemegen/color/color/rgb.rb', line 236

def lighten_by(percent)
  mix_with(White, percent)
end

#max_rgb_as_grayscaleObject Also known as: max_rgb_as_greyscale

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



443
444
445
# File 'lib/rmthemegen/color/color/rgb.rb', line 443

def max_rgb_as_grayscale
    Color::GrayScale.from_fraction([@r, @g, @b].max)
end

#mix_with(mask, opacity) ⇒ Object

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



249
250
251
252
253
254
255
256
257
258
# File 'lib/rmthemegen/color/color/rgb.rb', line 249

def mix_with(mask, opacity)
  opacity /= 100.0
  rgb = self.dup
  
  rgb.r = (@r * opacity) + (mask.r * (1 - opacity))
  rgb.g = (@g * opacity) + (mask.g * (1 - opacity))
  rgb.b = (@b * opacity) + (mask.b * (1 - opacity))

  rgb
end

#next_gaussian(mean) ⇒ Object

returns a random number distributed about a mean (in [0..1) ) hopefully this algorithm delivers numbers more tightly clustered about their mean



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rmthemegen/rgb_contrast_methods.rb', line 85

def next_gaussian(mean)
  y = (-(1.0/8.0)*(Math.log(rand)))
  y = rand >= 0.5 ? y : -y
  # so now y should be in [-0.5 .. 0.5]
  y= y + mean
  y= y > 1.0 ? 1.0 : y
  y= y < 0.0 ? 0.0 : y
 # puts "next_gaussian: "+ y.to_s
 
  return y
end

#pdf_fillObject

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



88
89
90
# File 'lib/rmthemegen/color/color/rgb.rb', line 88

def pdf_fill
  PDF_FORMAT_STR % [ @r, @g, @b, "rg" ]
end

#pdf_strokeObject

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



94
95
96
# File 'lib/rmthemegen/color/color/rgb.rb', line 94

def pdf_stroke
  PDF_FORMAT_STR % [ @r, @g, @b, "RG" ]
end

#rObject

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



336
337
338
# File 'lib/rmthemegen/color/color/rgb.rb', line 336

def r
  @r
end

#r=(rr) ⇒ Object

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



349
350
351
# File 'lib/rmthemegen/color/color/rgb.rb', line 349

def r=(rr)
  @r = Color.normalize(rr)
end

#redObject

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



327
328
329
# File 'lib/rmthemegen/color/color/rgb.rb', line 327

def red
  @r * 255.0
end

#red=(rr) ⇒ Object

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



340
341
342
# File 'lib/rmthemegen/color/color/rgb.rb', line 340

def red=(rr)
  @r = Color.normalize(rr / 255.0)
end

#red_pObject

Returns the red component of the colour as a percentage.



331
332
333
# File 'lib/rmthemegen/color/color/rgb.rb', line 331

def red_p
  @r * 100.0
end

#red_p=(rr) ⇒ Object

Sets the red component of the colour as a percentage.



344
345
346
# File 'lib/rmthemegen/color/color/rgb.rb', line 344

def red_p=(rr)
  @r = Color.normalize(rr / 100.0)
end

#to_cmykObject

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.



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/rmthemegen/color/color/rgb.rb', line 166

def to_cmyk
  c = 1.0 - @r.to_f
  m = 1.0 - @g.to_f
  y = 1.0 - @b.to_f

  k = [c, m, y].min
  k = k - (k * brightness)

  c = [1.0, [0.0, c - k].max].min
  m = [1.0, [0.0, m - k].max].min
  y = [1.0, [0.0, y - k].max].min
  k = [1.0, [0.0, k].max].min

  Color::CMYK.from_fraction(c, m, y, k)
end

#to_grayscaleObject Also known as: to_greyscale

Convert to grayscale.



270
271
272
# File 'lib/rmthemegen/color/color/rgb.rb', line 270

def to_grayscale
  Color::GrayScale.from_fraction(to_hsl.l)
end

#to_hslObject

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



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/rmthemegen/color/color/rgb.rb', line 197

def to_hsl
  min   = [ @r, @g, @b ].min
  max   = [ @r, @g, @b ].max
  delta = (max - min).to_f

  lum   = (max + min) / 2.0

  if Color.near_zero?(delta) # close to 0.0, so it's a grey
    hue = 0
    sat = 0
  else
    if Color.near_zero_or_less?(lum - 0.5)
      sat = delta / (max + min).to_f
    else
      sat = delta / (2 - max - min).to_f
    end

    # This is based on the conversion algorithm from
    # http://en.wikipedia.org/wiki/HSV_color_space#Conversion_from_RGB_to_HSL_or_HSV
    # Contributed by Adam Johnson
    sixth = 1 / 6.0
    if @r == max # Color.near_zero_or_less?(@r - max)
      hue = (sixth * ((@g - @b) / delta))
      hue += 1.0 if @g < @b
    elsif @g == max # Color.near_zero_or_less(@g - max)
      hue = (sixth * ((@b - @r) / delta)) + (1.0 / 3.0)
    elsif @b == max # Color.near_zero_or_less?(@b - max)
      hue = (sixth * ((@r - @g) / delta)) + (2.0 / 3.0)
    end

    hue += 1 if hue < 0
    hue -= 1 if hue > 1
  end
  Color::HSL.from_fraction(hue, sat, lum)
end

#to_rgb(ignored = nil) ⇒ Object



182
183
184
# File 'lib/rmthemegen/color/color/rgb.rb', line 182

def to_rgb(ignored = nil)
  self
end

#to_yiqObject

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



187
188
189
190
191
192
# File 'lib/rmthemegen/color/color/rgb.rb', line 187

def to_yiq
  y = (@r * 0.299) + (@g *  0.587) + (@b *  0.114)
  i = (@r * 0.596) + (@g * -0.275) + (@b * -0.321)
  q = (@r * 0.212) + (@g * -0.523) + (@b *  0.311)
  Color::YIQ.from_fraction(y, i, q)
end