Class: ColorLib::RGB
- Inherits:
-
Object
- Object
- ColorLib::RGB
- Defined in:
- lib/color_lib/rgb.rb,
lib/color_lib.rb,
lib/color_lib/rgb-colors.rb
Overview
An RGB colour object.
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 =
ColorLib::RGB.new(0xf0, 0xf8, 0xff)
- AntiqueWhite =
ColorLib::RGB.new(0xfa, 0xeb, 0xd7)
- Aqua =
ColorLib::RGB.new(0x00, 0xff, 0xff)
- Aquamarine =
ColorLib::RGB.new(0x7f, 0xff, 0xd4)
- Azure =
ColorLib::RGB.new(0xf0, 0xff, 0xff)
- Beige =
ColorLib::RGB.new(0xf5, 0xf5, 0xdc)
- Bisque =
ColorLib::RGB.new(0xff, 0xe4, 0xc4)
- Black =
ColorLib::RGB.new(0, 0, 0)
- BlanchedAlmond =
ColorLib::RGB.new(0xff, 0xeb, 0xcd)
- Blue =
ColorLib::RGB.new(0x00, 0x00, 0xff)
- BlueViolet =
ColorLib::RGB.new(0x8a, 0x2b, 0xe2)
- Brown =
ColorLib::RGB.new(0xa5, 0x2a, 0x2a)
- BurlyWood =
ColorLib::RGB.new(0xde, 0xb8, 0x87)
- Burlywood =
BurlyWood- CadetBlue =
ColorLib::RGB.new(0x5f, 0x9e, 0xa0)
- Carnation =
ColorLib::RGB.new(0xff, 0x5e, 0xd0)
- Cayenne =
ColorLib::RGB.new(0x8d, 0x00, 0x00)
- Chartreuse =
ColorLib::RGB.new(0x7f, 0xff, 0x00)
- Chocolate =
ColorLib::RGB.new(0xd2, 0x69, 0x1e)
- Coral =
ColorLib::RGB.new(0xff, 0x7f, 0x50)
- CornflowerBlue =
ColorLib::RGB.new(0x64, 0x95, 0xed)
- Cornsilk =
ColorLib::RGB.new(0xff, 0xf8, 0xdc)
- Crimson =
ColorLib::RGB.new(0xdc, 0x14, 0x3c)
- Cyan =
ColorLib::RGB.new(0x00, 0xff, 0xff)
- DarkBlue =
ColorLib::RGB.new(0x00, 0x00, 0x8b)
- DarkCyan =
ColorLib::RGB.new(0x00, 0x8b, 0x8b)
- DarkGoldenRod =
ColorLib::RGB.new(0xb8, 0x86, 0x0b)
- DarkGoldenrod =
DarkGoldenRod- DarkGray =
ColorLib::RGB.new(0xa9, 0xa9, 0xa9)
- DarkGreen =
ColorLib::RGB.new(0x00, 0x64, 0x00)
- DarkGrey =
DarkGray- DarkKhaki =
ColorLib::RGB.new(0xbd, 0xb7, 0x6b)
- DarkMagenta =
ColorLib::RGB.new(0x8b, 0x00, 0x8b)
- DarkOliveGreen =
ColorLib::RGB.new(0x55, 0x6b, 0x2f)
- DarkOrange =
ColorLib::RGB.new(0xff, 0x8c, 0x00)
- DarkOrchid =
ColorLib::RGB.new(0x99, 0x32, 0xcc)
- DarkRed =
ColorLib::RGB.new(0x8b, 0x00, 0x00)
- DarkSalmon =
ColorLib::RGB.new(0xe9, 0x96, 0x7a)
- DarkSeaGreen =
ColorLib::RGB.new(0x8f, 0xbc, 0x8f)
- DarkSlateBlue =
ColorLib::RGB.new(0x48, 0x3d, 0x8b)
- DarkSlateGray =
ColorLib::RGB.new(0x2f, 0x4f, 0x4f)
- DarkSlateGrey =
DarkSlateGray- DarkTurquoise =
ColorLib::RGB.new(0x00, 0xce, 0xd1)
- DarkViolet =
ColorLib::RGB.new(0x94, 0x00, 0xd3)
- DarkoliveGreen =
DarkOliveGreen- Darkorange =
ColorLib::RGB.new(0xff, 0x8c, 0x00)
- Darksalmon =
DarkSalmon- DeepPink =
ColorLib::RGB.new(0xff, 0x14, 0x93)
- DeepSkyBlue =
ColorLib::RGB.new(0x00, 0xbf, 0xbf)
- DimGray =
ColorLib::RGB.new(0x69, 0x69, 0x69)
- DimGrey =
DimGray- DodgerBlue =
ColorLib::RGB.new(0x1e, 0x90, 0xff)
- Feldspar =
ColorLib::RGB.new(0xd1, 0x92, 0x75)
- FireBrick =
ColorLib::RGB.new(0xb2, 0x22, 0x22)
- Firebrick =
FireBrick- FloralWhite =
ColorLib::RGB.new(0xff, 0xfa, 0xf0)
- ForestGreen =
ColorLib::RGB.new(0x22, 0x8b, 0x22)
- Fuchsia =
ColorLib::RGB.new(0xff, 0x00, 0xff)
- Gainsboro =
ColorLib::RGB.new(0xdc, 0xdc, 0xdc)
- GhostWhite =
ColorLib::RGB.new(0xf8, 0xf8, 0xff)
- Gold =
ColorLib::RGB.new(0xff, 0xd7, 0x00)
- GoldenRod =
ColorLib::RGB.new(0xda, 0xa5, 0x20)
- Goldenrod =
GoldenRod- Gray =
ColorLib::RGB.new(0x80, 0x80, 0x80)
- Gray10 =
ColorLib::RGB.from_percentage(10, 10, 10)
- Gray20 =
ColorLib::RGB.from_percentage(20, 20, 20)
- Gray30 =
ColorLib::RGB.from_percentage(30, 30, 30)
- Gray40 =
ColorLib::RGB.from_percentage(40, 40, 40)
- Gray50 =
ColorLib::RGB.from_percentage(50, 50, 50)
- Gray60 =
ColorLib::RGB.from_percentage(60, 60, 60)
- Gray70 =
ColorLib::RGB.from_percentage(70, 70, 70)
- Gray80 =
ColorLib::RGB.from_percentage(80, 80, 80)
- Gray90 =
ColorLib::RGB.from_percentage(90, 90, 90)
- Green =
ColorLib::RGB.new(0x00, 0x80, 0x00)
- GreenYellow =
ColorLib::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 =
ColorLib::RGB.new(0xf0, 0xff, 0xf0)
- Honeydew =
HoneyDew- HotPink =
ColorLib::RGB.new(0xff, 0x69, 0xb4)
- IndianRed =
ColorLib::RGB.new(0xcd, 0x5c, 0x5c)
- Indigo =
ColorLib::RGB.new(0x4b, 0x00, 0x82)
- Ivory =
ColorLib::RGB.new(0xff, 0xff, 0xf0)
- Khaki =
ColorLib::RGB.new(0xf0, 0xe6, 0x8c)
- Lavender =
ColorLib::RGB.new(0xe6, 0xe6, 0xfa)
- LavenderBlush =
ColorLib::RGB.new(0xff, 0xf0, 0xf5)
- LawnGreen =
ColorLib::RGB.new(0x7c, 0xfc, 0x00)
- LemonChiffon =
ColorLib::RGB.new(0xff, 0xfa, 0xcd)
- LightBlue =
ColorLib::RGB.new(0xad, 0xd8, 0xe6)
- LightCoral =
ColorLib::RGB.new(0xf0, 0x80, 0x80)
- LightCyan =
ColorLib::RGB.new(0xe0, 0xff, 0xff)
- LightGoldenRodYellow =
ColorLib::RGB.new(0xfa, 0xfa, 0xd2)
- LightGoldenrodYellow =
LightGoldenRodYellow- LightGray =
ColorLib::RGB.new(0xd3, 0xd3, 0xd3)
- LightGreen =
ColorLib::RGB.new(0x90, 0xee, 0x90)
- LightGrey =
LightGray- LightPink =
ColorLib::RGB.new(0xff, 0xb6, 0xc1)
- LightSalmon =
ColorLib::RGB.new(0xff, 0xa0, 0x7a)
- LightSeaGreen =
ColorLib::RGB.new(0x20, 0xb2, 0xaa)
- LightSkyBlue =
ColorLib::RGB.new(0x87, 0xce, 0xfa)
- LightSlateBlue =
ColorLib::RGB.new(0x84, 0x70, 0xff)
- LightSlateGray =
ColorLib::RGB.new(0x77, 0x88, 0x99)
- LightSlateGrey =
LightSlateGray- LightSteelBlue =
ColorLib::RGB.new(0xb0, 0xc4, 0xde)
- LightYellow =
ColorLib::RGB.new(0xff, 0xff, 0xe0)
- Lightsalmon =
LightSalmon- LightsteelBlue =
LightSteelBlue- Lime =
ColorLib::RGB.new(0x00, 0xff, 0x00)
- LimeGreen =
ColorLib::RGB.new(0x32, 0xcd, 0x32)
- Linen =
ColorLib::RGB.new(0xfa, 0xf0, 0xe6)
- Magenta =
ColorLib::RGB.new(0xff, 0x00, 0xff)
- Maroon =
ColorLib::RGB.new(0x80, 0x00, 0x00)
- MediumAquaMarine =
ColorLib::RGB.new(0x66, 0xcd, 0xaa)
- MediumAquamarine =
MediumAquaMarine- MediumBlue =
ColorLib::RGB.new(0x00, 0x00, 0xcd)
- MediumOrchid =
ColorLib::RGB.new(0xba, 0x55, 0xd3)
- MediumPurple =
ColorLib::RGB.new(0x93, 0x70, 0xdb)
- MediumSeaGreen =
ColorLib::RGB.new(0x3c, 0xb3, 0x71)
- MediumSlateBlue =
ColorLib::RGB.new(0x7b, 0x68, 0xee)
- MediumSpringGreen =
ColorLib::RGB.new(0x00, 0xfa, 0x9a)
- MediumTurquoise =
ColorLib::RGB.new(0x48, 0xd1, 0xcc)
- MediumVioletRed =
ColorLib::RGB.new(0xc7, 0x15, 0x85)
- MidnightBlue =
ColorLib::RGB.new(0x19, 0x19, 0x70)
- MintCream =
ColorLib::RGB.new(0xf5, 0xff, 0xfa)
- MistyRose =
ColorLib::RGB.new(0xff, 0xe4, 0xe1)
- Moccasin =
ColorLib::RGB.new(0xff, 0xe4, 0xb5)
ColorLib::RGB.new(0xff, 0xde, 0xad)
ColorLib::RGB.new(0x00, 0x00, 0x80)
- OldLace =
ColorLib::RGB.new(0xfd, 0xf5, 0xe6)
- Olive =
ColorLib::RGB.new(0x80, 0x80, 0x00)
- OliveDrab =
ColorLib::RGB.new(0x6b, 0x8e, 0x23)
- Olivedrab =
OliveDrab- Orange =
ColorLib::RGB.new(0xff, 0xa5, 0x00)
- OrangeRed =
ColorLib::RGB.new(0xff, 0x45, 0x00)
- Orchid =
ColorLib::RGB.new(0xda, 0x70, 0xd6)
- PaleGoldenRod =
ColorLib::RGB.new(0xee, 0xe8, 0xaa)
- PaleGoldenrod =
PaleGoldenRod- PaleGreen =
ColorLib::RGB.new(0x98, 0xfb, 0x98)
- PaleTurquoise =
ColorLib::RGB.new(0xaf, 0xee, 0xee)
- PaleVioletRed =
ColorLib::RGB.new(0xdb, 0x70, 0x93)
- PapayaWhip =
ColorLib::RGB.new(0xff, 0xef, 0xd5)
- PeachPuff =
ColorLib::RGB.new(0xff, 0xda, 0xb9)
- Peachpuff =
PeachPuff- Peru =
ColorLib::RGB.new(0xcd, 0x85, 0x3f)
- Pink =
ColorLib::RGB.new(0xff, 0xc0, 0xcb)
- Plum =
ColorLib::RGB.new(0xdd, 0xa0, 0xdd)
- PowderBlue =
ColorLib::RGB.new(0xb0, 0xe0, 0xe6)
- Purple =
ColorLib::RGB.new(0x80, 0x00, 0x80)
- Red =
ColorLib::RGB.new(0xff, 0x00, 0x00)
- RosyBrown =
ColorLib::RGB.new(0xbc, 0x8f, 0x8f)
- RoyalBlue =
ColorLib::RGB.new(0x41, 0x69, 0xe1)
- SaddleBrown =
ColorLib::RGB.new(0x8b, 0x45, 0x13)
- Salmon =
ColorLib::RGB.new(0xfa, 0x80, 0x72)
- SandyBrown =
ColorLib::RGB.new(0xf4, 0xa4, 0x60)
- SeaGreen =
ColorLib::RGB.new(0x2e, 0x8b, 0x57)
- SeaShell =
ColorLib::RGB.new(0xff, 0xf5, 0xee)
- Seashell =
SeaShell- Sienna =
ColorLib::RGB.new(0xa0, 0x52, 0x2d)
- Silver =
ColorLib::RGB.new(0xc0, 0xc0, 0xc0)
- SkyBlue =
ColorLib::RGB.new(0x87, 0xce, 0xeb)
- SlateBlue =
ColorLib::RGB.new(0x6a, 0x5a, 0xcd)
- SlateGray =
ColorLib::RGB.new(0x70, 0x80, 0x90)
- SlateGrey =
SlateGray- Snow =
ColorLib::RGB.new(0xff, 0xfa, 0xfa)
- SpringGreen =
ColorLib::RGB.new(0x00, 0xff, 0x7f)
- SteelBlue =
ColorLib::RGB.new(0x46, 0x82, 0xb4)
- Tan =
ColorLib::RGB.new(0xd2, 0xb4, 0x8c)
- Teal =
ColorLib::RGB.new(0x00, 0x80, 0x80)
- Thistle =
ColorLib::RGB.new(0xd8, 0xbf, 0xd8)
- Tomato =
ColorLib::RGB.new(0xff, 0x63, 0x47)
- Turquoise =
ColorLib::RGB.new(0x40, 0xe0, 0xd0)
- Violet =
ColorLib::RGB.new(0xee, 0x82, 0xee)
- VioletRed =
ColorLib::RGB.new(0xd0, 0x20, 0x90)
- Wheat =
ColorLib::RGB.new(0xf5, 0xde, 0xb3)
- White =
ColorLib::RGB.new(0xff, 0xff, 0xff)
- WhiteSmoke =
ColorLib::RGB.new(0xf5, 0xf5, 0xf5)
- Yellow =
ColorLib::RGB.new(0xff, 0xff, 0x00)
- YellowGreen =
ColorLib::RGB.new(0x9a, 0xcd, 0x32)
Class Method Summary collapse
-
.from_fraction(r = 0.0, g = 0.0, b = 0.0) ⇒ Object
Creates an RGB colour object from fractional values 0..1.
-
.from_html(html_colour) ⇒ Object
Creates an RGB colour object from an HTML colour descriptor (e.g.,
"fed"or"#cabbed;". -
.from_percentage(r = 0, g = 0, b = 0) ⇒ Object
Creates an RGB colour object from percentages 0..100.
Instance Method Summary collapse
-
#+(other) ⇒ Object
Adds another colour to the current colour.
-
#-(other) ⇒ Object
Subtracts another colour to the current colour.
-
#==(other) ⇒ Object
Compares the other colour to this one.
-
#adjust_brightness(percent) ⇒ Object
Returns a new colour with the brightness adjusted by the specified percentage.
-
#adjust_hue(percent) ⇒ Object
Returns a new colour with the hue adjusted by the specified percentage.
-
#adjust_saturation(percent) ⇒ Object
Returns a new colour with the saturation adjusted by the specified percentage.
-
#b ⇒ Object
Returns the blue component of the colour as a fraction in the range 0.0 ..
-
#b=(bb) ⇒ Object
Sets the blue component of the colour as a fraction in the range 0.0 ..
-
#blue ⇒ Object
Returns the blue component of the colour in the normal 0 ..
-
#blue=(bb) ⇒ Object
Sets the blue component of the colour in the normal 0 ..
-
#blue_p ⇒ Object
Returns the blue component of the colour as a percentage.
-
#blue_p=(bb) ⇒ Object
Sets the blue component of the colour as a percentage.
-
#brightness ⇒ Object
Returns the brightness value for a colour, a number between 0..1.
-
#css_hsl ⇒ Object
Present the colour as an HSL HTML/CSS colour string (e.g., “hsl(180, 25%, 35%)”).
-
#css_hsla ⇒ Object
Present the colour as an HSLA (with alpha) HTML/CSS colour string (e.g., “hsla(180, 25%, 35%, 1)”).
-
#css_rgb ⇒ Object
Present the colour as an RGB HTML/CSS colour string (e.g., “rgb(0%, 50%, 100%)”).
-
#css_rgba ⇒ Object
Present the colour as an RGBA (with alpha) HTML/CSS colour string (e.g., “rgb(0%, 50%, 100%, 1)”).
-
#darken_by(percent) ⇒ Object
Mix the RGB hue with Black so that the RGB hue is the specified percentage of the resulting colour.
-
#g ⇒ Object
Returns the green component of the colour as a fraction in the range 0.0 ..
-
#g=(gg) ⇒ Object
Sets the green component of the colour as a fraction in the range 0.0 ..
-
#green ⇒ Object
Returns the green component of the colour in the normal 0 ..
-
#green=(gg) ⇒ Object
Sets the green component of the colour in the normal 0 ..
-
#green_p ⇒ Object
Returns the green component of the colour as a percentage.
-
#green_p=(gg) ⇒ Object
Sets the green component of the colour as a percentage.
-
#html ⇒ Object
Present the colour as an HTML/CSS colour string.
-
#initialize(r = 0, g = 0, b = 0) ⇒ RGB
constructor
Creates an RGB colour object from the standard range 0..255.
- #inspect ⇒ Object
-
#lighten_by(percent) ⇒ Object
Mix the RGB hue with White so that the RGB hue is the specified percentage of the resulting colour.
-
#max_rgb_as_grayscale ⇒ Object
(also: #max_rgb_as_greyscale)
Retrieve the maxmum RGB value from the current colour as a GrayScale colour.
-
#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).
-
#pdf_fill ⇒ Object
Present the colour as a DeviceRGB fill colour string for PDF.
-
#pdf_stroke ⇒ Object
Present the colour as a DeviceRGB stroke colour string for PDF.
-
#r ⇒ Object
Returns the red component of the colour as a fraction in the range 0.0 ..
-
#r=(rr) ⇒ Object
Sets the red component of the colour as a fraction in the range 0.0 ..
-
#red ⇒ Object
Returns the red component of the colour in the normal 0 ..
-
#red=(rr) ⇒ Object
Sets the red component of the colour in the normal 0 ..
-
#red_p ⇒ Object
Returns the red component of the colour as a percentage.
-
#red_p=(rr) ⇒ Object
Sets the red component of the colour as a percentage.
-
#to_cmyk ⇒ Object
Converts the RGB colour to CMYK.
-
#to_grayscale ⇒ Object
(also: #to_greyscale)
Convert to grayscale.
-
#to_hsl ⇒ Object
Returns the HSL colour encoding of the RGB value.
- #to_rgb(ignored = nil) ⇒ Object
-
#to_yiq ⇒ Object
Returns the YIQ (NTSC) colour encoding of the RGB value.
Constructor Details
#initialize(r = 0, g = 0, b = 0) ⇒ RGB
74 75 76 77 78 |
# File 'lib/color_lib/rgb.rb', line 74 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.
ColorLib::RGB.from_fraction(.3, .2, .1)
21 22 23 24 25 26 27 |
# File 'lib/color_lib/rgb.rb', line 21 def from_fraction(r = 0.0, g = 0.0, b = 0.0) colour = ColorLib::RGB.new colour.r = r colour.g = g colour.b = b colour end |
.from_html(html_colour) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/color_lib/rgb.rb', line 36 def from_html(html_colour) if html_colour.present? 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 ColorLib::RGB.new(*colours) else raise ArgumentError end end |
.from_percentage(r = 0, g = 0, b = 0) ⇒ Object
Creates an RGB colour object from percentages 0..100.
ColorLib::RGB.from_percentage(10, 20 30)
14 15 16 |
# File 'lib/color_lib/rgb.rb', line 14 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.
424 425 426 427 428 429 430 431 432 433 |
# File 'lib/color_lib/rgb.rb', line 424 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.
441 442 443 444 445 446 447 448 449 450 |
# File 'lib/color_lib/rgb.rb', line 441 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.
62 63 64 65 66 67 68 |
# File 'lib/color_lib/rgb.rb', line 62 def ==(other) other = other.to_rgb other.kind_of?(ColorLib::RGB) and ((@r - other.r).abs <= ColorLib::COLOR_TOLERANCE) and ((@g - other.g).abs <= ColorLib::COLOR_TOLERANCE) and ((@b - other.b).abs <= ColorLib::COLOR_TOLERANCE) end |
#adjust_brightness(percent) ⇒ Object
277 278 279 280 281 282 283 284 285 286 |
# File 'lib/color_lib/rgb.rb', line 277 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
311 312 313 314 315 316 317 318 319 320 |
# File 'lib/color_lib/rgb.rb', line 311 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
294 295 296 297 298 299 300 301 302 303 |
# File 'lib/color_lib/rgb.rb', line 294 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 |
#b ⇒ Object
Returns the blue component of the colour as a fraction in the range 0.0 .. 1.0.
398 399 400 |
# File 'lib/color_lib/rgb.rb', line 398 def b @b end |
#b=(bb) ⇒ Object
Sets the blue component of the colour as a fraction in the range 0.0 .. 1.0.
414 415 416 |
# File 'lib/color_lib/rgb.rb', line 414 def b=(bb) @b = ColorLib.normalize(bb) end |
#blue ⇒ Object
Returns the blue component of the colour in the normal 0 .. 255 range.
387 388 389 |
# File 'lib/color_lib/rgb.rb', line 387 def blue @b * 255.0 end |
#blue=(bb) ⇒ Object
Sets the blue component of the colour in the normal 0 .. 255 range.
403 404 405 |
# File 'lib/color_lib/rgb.rb', line 403 def blue=(bb) @b = ColorLib.normalize(bb / 255.0) end |
#blue_p ⇒ Object
Returns the blue component of the colour as a percentage.
392 393 394 |
# File 'lib/color_lib/rgb.rb', line 392 def blue_p @b * 100.0 end |
#blue_p=(bb) ⇒ Object
Sets the blue component of the colour as a percentage.
408 409 410 |
# File 'lib/color_lib/rgb.rb', line 408 def blue_p=(bb) @b = ColorLib.normalize(bb / 100.0) end |
#brightness ⇒ Object
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.
260 261 262 |
# File 'lib/color_lib/rgb.rb', line 260 def brightness to_yiq.y end |
#css_hsl ⇒ Object
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.
123 124 125 |
# File 'lib/color_lib/rgb.rb', line 123 def css_hsl to_hsl.css_hsl end |
#css_hsla ⇒ Object
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.
130 131 132 |
# File 'lib/color_lib/rgb.rb', line 130 def css_hsla to_hsl.css_hsla end |
#css_rgb ⇒ Object
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.
109 110 111 |
# File 'lib/color_lib/rgb.rb', line 109 def css_rgb "rgb(%3.2f%%, %3.2f%%, %3.2f%%)" % [red_p, green_p, blue_p] end |
#css_rgba ⇒ Object
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.
116 117 118 |
# File 'lib/color_lib/rgb.rb', line 116 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.
237 238 239 |
# File 'lib/color_lib/rgb.rb', line 237 def darken_by(percent) mix_with(Black, percent) end |
#g ⇒ Object
Returns the green component of the colour as a fraction in the range 0.0 .. 1.0.
366 367 368 |
# File 'lib/color_lib/rgb.rb', line 366 def g @g end |
#g=(gg) ⇒ Object
Sets the green component of the colour as a fraction in the range 0.0 .. 1.0.
382 383 384 |
# File 'lib/color_lib/rgb.rb', line 382 def g=(gg) @g = ColorLib.normalize(gg) end |
#green ⇒ Object
Returns the green component of the colour in the normal 0 .. 255 range.
355 356 357 |
# File 'lib/color_lib/rgb.rb', line 355 def green @g * 255.0 end |
#green=(gg) ⇒ Object
Sets the green component of the colour in the normal 0 .. 255 range.
371 372 373 |
# File 'lib/color_lib/rgb.rb', line 371 def green=(gg) @g = ColorLib.normalize(gg / 255.0) end |
#green_p ⇒ Object
Returns the green component of the colour as a percentage.
360 361 362 |
# File 'lib/color_lib/rgb.rb', line 360 def green_p @g * 100.0 end |
#green_p=(gg) ⇒ Object
Sets the green component of the colour as a percentage.
376 377 378 |
# File 'lib/color_lib/rgb.rb', line 376 def green_p=(gg) @g = ColorLib.normalize(gg / 100.0) end |
#html ⇒ Object
Present the colour as an HTML/CSS colour string.
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/color_lib/rgb.rb', line 93 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 |
#inspect ⇒ Object
460 461 462 |
# File 'lib/color_lib/rgb.rb', line 460 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.
230 231 232 |
# File 'lib/color_lib/rgb.rb', line 230 def lighten_by(percent) mix_with(White, percent) end |
#max_rgb_as_grayscale ⇒ Object Also known as: max_rgb_as_greyscale
Retrieve the maxmum RGB value from the current colour as a GrayScale colour
454 455 456 |
# File 'lib/color_lib/rgb.rb', line 454 def max_rgb_as_grayscale ColorLib::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).
243 244 245 246 247 248 249 250 251 252 |
# File 'lib/color_lib/rgb.rb', line 243 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 |
#pdf_fill ⇒ Object
Present the colour as a DeviceRGB fill colour string for PDF. This will be removed from the default package in color-tools 2.0.
82 83 84 |
# File 'lib/color_lib/rgb.rb', line 82 def pdf_fill PDF_FORMAT_STR % [@r, @g, @b, "rg"] end |
#pdf_stroke ⇒ Object
Present the colour as a DeviceRGB stroke colour string for PDF. This will be removed from the default package in color-tools 2.0.
88 89 90 |
# File 'lib/color_lib/rgb.rb', line 88 def pdf_stroke PDF_FORMAT_STR % [@r, @g, @b, "RG"] end |
#r ⇒ Object
Returns the red component of the colour as a fraction in the range 0.0 .. 1.0.
334 335 336 |
# File 'lib/color_lib/rgb.rb', line 334 def r @r end |
#r=(rr) ⇒ Object
Sets the red component of the colour as a fraction in the range 0.0 .. 1.0.
350 351 352 |
# File 'lib/color_lib/rgb.rb', line 350 def r=(rr) @r = ColorLib.normalize(rr) end |
#red ⇒ Object
Returns the red component of the colour in the normal 0 .. 255 range.
323 324 325 |
# File 'lib/color_lib/rgb.rb', line 323 def red @r * 255.0 end |
#red=(rr) ⇒ Object
Sets the red component of the colour in the normal 0 .. 255 range.
339 340 341 |
# File 'lib/color_lib/rgb.rb', line 339 def red=(rr) @r = ColorLib.normalize(rr / 255.0) end |
#red_p ⇒ Object
Returns the red component of the colour as a percentage.
328 329 330 |
# File 'lib/color_lib/rgb.rb', line 328 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/color_lib/rgb.rb', line 344 def red_p=(rr) @r = ColorLib.normalize(rr / 100.0) end |
#to_cmyk ⇒ Object
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:
-
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 -
Compute the minimum amount of black (K) required to smooth the colour in inks.
k = min(c, m, y) -
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.
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/color_lib/rgb.rb', line 160 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 ColorLib::CMYK.from_fraction(c, m, y, k) end |
#to_grayscale ⇒ Object Also known as: to_greyscale
Convert to grayscale.
265 266 267 |
# File 'lib/color_lib/rgb.rb', line 265 def to_grayscale ColorLib::GrayScale.from_fraction(to_hsl.l) end |
#to_hsl ⇒ Object
Returns the HSL colour encoding of the RGB value. The conversions here are based on forumlas from www.easyrgb.com/math.php and elsewhere.
191 192 193 194 195 196 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 |
# File 'lib/color_lib/rgb.rb', line 191 def to_hsl min = [@r, @g, @b].min max = [@r, @g, @b].max delta = (max - min).to_f lum = (max + min) / 2.0 if ColorLib.near_zero?(delta) # close to 0.0, so it's a grey hue = 0 sat = 0 else if ColorLib.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 # ColorLib.near_zero_or_less?(@r - max) hue = (sixth * ((@g - @b) / delta)) hue += 1.0 if @g < @b elsif @g == max # ColorLib.near_zero_or_less(@g - max) hue = (sixth * ((@b - @r) / delta)) + (1.0 / 3.0) elsif @b == max # ColorLib.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 ColorLib::HSL.from_fraction(hue, sat, lum) end |
#to_rgb(ignored = nil) ⇒ Object
176 177 178 |
# File 'lib/color_lib/rgb.rb', line 176 def to_rgb(ignored = nil) self end |
#to_yiq ⇒ Object
Returns the YIQ (NTSC) colour encoding of the RGB value.
181 182 183 184 185 186 |
# File 'lib/color_lib/rgb.rb', line 181 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) ColorLib::YIQ.from_fraction(y, i, q) end |