Class: RGB::Color

Inherits:
Object
  • Object
show all
Defined in:
lib/redgreenblue.rb,
lib/redgreenblue/gif.rb,
lib/redgreenblue/gpl.rb,
lib/redgreenblue/hex.rb,
lib/redgreenblue/hsb.rb,
lib/redgreenblue/hsl.rb,
lib/redgreenblue/hsv.rb,
lib/redgreenblue/hwb.rb,
lib/redgreenblue/int.rb,
lib/redgreenblue/mac.rb,
lib/redgreenblue/mix.rb,
lib/redgreenblue/web.rb,
lib/redgreenblue/yuv.rb,
lib/redgreenblue/base.rb,
lib/redgreenblue/lazy.rb,
lib/redgreenblue/math.rb,
lib/redgreenblue/misc.rb,
lib/redgreenblue/name.rb,
lib/redgreenblue/view.rb,
lib/redgreenblue/24bit.rb,
lib/redgreenblue/48bit.rb,
lib/redgreenblue/gamma.rb,
lib/redgreenblue/match.rb,
lib/redgreenblue/os/mac.rb,
lib/redgreenblue/random.rb,
lib/redgreenblue/rgb565.rb,
lib/redgreenblue/inspect.rb,
lib/redgreenblue/ostwald.rb,
lib/redgreenblue/bgr24bit.rb,
lib/redgreenblue/cie_1931.rb,
lib/redgreenblue/cie_1976.rb,
lib/redgreenblue/cie_1994.rb,
lib/redgreenblue/terminal.rb,
lib/redgreenblue/hsx_shared.rb,
lib/redgreenblue/opt/philipshue.rb

Overview

This class represents an RGB color.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*a) ⇒ Color

Returns a new instance of Color.



5
6
7
# File 'lib/redgreenblue/base.rb', line 5

def initialize(*a)
  self.values = a.any? ? a : [ 0.5, 0.5, 0.5 ]
end

Class Method Details

.assemble(*a) ⇒ Object

Creates a new RGB::Color from three RGB::Colors representing the red, green, and blue components.



34
35
36
37
# File 'lib/redgreenblue/misc.rb', line 34

def self.assemble(*a)
  v = a.flatten
  RGB::Color.new(v[0].red, v[1].green, v[2].blue)
end

.at(number) ⇒ Object

Creates a new RGB::Color from a 24-bit integer in the range 0..16777215.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/redgreenblue/int.rb', line 9

def self.at(number)
  n = number.to_i
  if (0..16777215) === n
    rgb(
      ( n & 0xff0000 ) >> 16,
      ( n & 0x00ff00 ) >>  8,
      ( n & 0x0000ff )
    )
  else
    raise ArgumentError, "Argument '#{number}' not in range 0..16777215"
  end
end

.bgr24(bgr) ⇒ Object

Creates a new RGB::Color from BGR24 data (a 3-byte string).



14
15
16
17
18
# File 'lib/redgreenblue/bgr24bit.rb', line 14

def self.bgr24(bgr)
  c = self.new
  c.bgr24 = bgr
  c
end

.blackObject

Creates a black RGB::Color.



12
13
14
# File 'lib/redgreenblue/lazy.rb', line 12

def black
  new(0,0,0)
end

.blueObject

Creates a pure blue RGB::Color.



37
38
39
# File 'lib/redgreenblue/lazy.rb', line 37

def blue
  new(0,0,1)
end

.centreObject Also known as: center

Returns the centre of the RGB cube.



62
63
64
# File 'lib/redgreenblue/lazy.rb', line 62

def centre
  grey
end

.cie_xyz(*a) ⇒ Object

Creates a new RGB::Color from CIE 1931 XYZ values.

Assumes the XYZ values are relative to D65 reference white, the same as used in sRGB.



8
9
10
11
12
# File 'lib/redgreenblue/cie_1931.rb', line 8

def cie_xyz(*a)
  c = self.new
  c.cie_xyz = a
  c
end

.cornersObject

Returns the 8 corners of the RGB cube.



57
58
59
# File 'lib/redgreenblue/lazy.rb', line 57

def corners
  [ black, red, yellow, green, cyan, blue, magenta, white ]
end

.cyanObject

Creates a cyan RGB::Color.



47
48
49
# File 'lib/redgreenblue/lazy.rb', line 47

def cyan
  new(0,1,1)
end

.gpl(line) ⇒ Object

Creates a new RGB::Color from a line of gpl (Gimp color palette) input. Returns nil if not successful.

Examples:

RGB::Color.gpl "255 153 204\tpink"


9
10
11
12
13
14
15
16
17
# File 'lib/redgreenblue/gpl.rb', line 9

def gpl(line)
  if line.chomp.match( /^\s*(?<r>\d{1,3})\s+(?<g>\d{1,3})\s+(?<b>\d{1,3})(\s+(?<name>.*))?/ )
    color = RGB::Color.rgb $~[:r].to_i, $~[:g].to_i, $~[:b].to_i
    color.name = $~[:name] if $~[:name]
    color
  else
    nil
  end
end

.greenObject

Creates a pure green RGB::Color.



32
33
34
# File 'lib/redgreenblue/lazy.rb', line 32

def green
  new(0,1,0)
end

.grey(lightness = 0.5) ⇒ Object Also known as: gray

Creates a grey RGB::Color. Defaults to lightness 0.5, a middle grey. Black equals grey(0), white equals grey(1).

::gray is an alias for ::grey.



19
20
21
# File 'lib/redgreenblue/lazy.rb', line 19

def grey(lightness=0.5)
  new(lightness, lightness, lightness)
end

.hsbObject

Creates a new RGB::Color from HSB values: hue (0..360), saturation (0..1), and brightness (0..1).



8
# File 'lib/redgreenblue/hsb.rb', line 8

alias hsb hsv

.hsl(*a) ⇒ Object

Creates a new RGB::Color from HSL values: hue (0..360), saturation (0..1), and lightness (0..1).



9
10
11
# File 'lib/redgreenblue/hsl.rb', line 9

def hsl(*a)
  new hsl_to_values(*a)
end

.hsl_to_values(*a) ⇒ Object

Calculates RGB values from HSL. Given hue (0..360), saturation (0..1), and lightness (0..1), returns red, green, and blue as three values between 0 and 1.



16
17
18
# File 'lib/redgreenblue/hsl.rb', line 16

def hsl_to_values(*a)
  hsm_to_values(:hsl, a)
end

.hsv(*a) ⇒ Object

Creates a new RGB::Color from HSV values: hue (0..360), saturation (0..1), and value (0..1).



9
10
11
# File 'lib/redgreenblue/hsv.rb', line 9

def hsv(*a)
  new hsv_to_values(*a)
end

.hsv_to_values(*a) ⇒ Object

Calculates RGB values from HSV. Given hue (0..360), saturation (0..1), and value (0..1), returns red, green, and blue as three values between 0 and 1.



16
17
18
# File 'lib/redgreenblue/hsv.rb', line 16

def hsv_to_values(*a)
  hsm_to_values(:hsv, a)
end

.magentaObject

Creates a magenta RGB::Color.



52
53
54
# File 'lib/redgreenblue/lazy.rb', line 52

def magenta
  new(1,0,1)
end

.pick(default_color = RGB::Color.new) ⇒ Object

On Mac OS, shows the color picker and creates an RGB::Color with the chosen color. Not available on other platforms.

If no default color is specified, the picker defaults to a middle grey.



16
17
18
19
20
21
22
23
# File 'lib/redgreenblue/os/mac.rb', line 16

def self.pick(default_color=RGB::Color.new)
  result = RGB::Color.mac_choose(default_color)
  if result
    RGB.rrggbb result
  else
    nil
  end
end

.randObject

Creates a new RGB::Color with random red, green, and blue values.



21
22
23
# File 'lib/redgreenblue/random.rb', line 21

def self.rand
  new(Kernel::rand, Kernel::rand, Kernel::rand)
end

.redObject

Creates a pure red RGB::Color.



27
28
29
# File 'lib/redgreenblue/lazy.rb', line 27

def red
  new(1,0,0)
end

.rgb(*rgb) ⇒ Object

Creates a new RGB::Color from red, green, and blue components as integers in the range 0..255 (three 8-bit values).



48
49
50
51
52
# File 'lib/redgreenblue/24bit.rb', line 48

def self.rgb(*rgb)
  c = self.new
  c.rgb = rgb
  c
end

.rgb565(rgb565_string) ⇒ Object

Creates a new RGB::Color from 16-bit RGB565 data.



19
20
21
22
23
# File 'lib/redgreenblue/rgb565.rb', line 19

def self.rgb565(rgb565_string)
  c = self.new
  c.rgb565 = rgb565_string
  c
end

.rrggbb(*rrggbb) ⇒ Object

Creates a new RGB::Color from red, green, and blue components as integers in the range 0..65535 (three 16-bit values).



48
49
50
51
52
# File 'lib/redgreenblue/48bit.rb', line 48

def self.rrggbb(*rrggbb)
  c = self.new
  c.rrggbb = rrggbb
  c
end

.whiteObject

Creates a white RGB::Color.



7
8
9
# File 'lib/redgreenblue/lazy.rb', line 7

def white
  new(1,1,1)
end

.xyzObject

Creates a new RGB::Color from CIE 1931 XYZ values.

Assumes the XYZ values are relative to D65 reference white, the same as used in sRGB.



14
15
16
17
18
# File 'lib/redgreenblue/cie_1931.rb', line 14

def cie_xyz(*a)
  c = self.new
  c.cie_xyz = a
  c
end

.yellowObject

Creates a yellow RGB::Color.



42
43
44
# File 'lib/redgreenblue/lazy.rb', line 42

def yellow
  new(1,1,0)
end

Instance Method Details

#==(other) ⇒ Object

Returns true if this object and another object represent exactly the same color. Otherwise returns false.



67
68
69
# File 'lib/redgreenblue/base.rb', line 67

def ==(other)
  ( self.class == other.class ) && ( self.values == other.values )
end

#achromatic?Boolean

Returns true when this is an achromatic color: red, green, and blue have equal values. Otherwise false.

Returns:

  • (Boolean)


16
17
18
# File 'lib/redgreenblue/misc.rb', line 16

def achromatic?
  values.min == values.max
end

#applescriptObject

Returns the color in the format used by AppleScript (a 48-bit RGB triplet).



4
5
6
# File 'lib/redgreenblue/mac.rb', line 4

def applescript
  "{%i, %i, %i}" % rrggbb
end

#bObject

Returns the blue component as an integer in the range 0..255 (an 8-bit value).



16
17
18
# File 'lib/redgreenblue/24bit.rb', line 16

def b
  (blue  * 255).round
end

#b=(n) ⇒ Object

Sets the blue component using an integer in the range 0..255 (an 8-bit value).



31
32
33
# File 'lib/redgreenblue/24bit.rb', line 31

def b=(n)
  self.blue  = n / 255.0
end

#bbObject

Returns the blue component as an integer in the range 0..65535 (a 16-bit value).



16
17
18
# File 'lib/redgreenblue/48bit.rb', line 16

def bb
  (blue  * 65535).round
end

#bb=(n) ⇒ Object

Sets the blue component using an integer in the range 0..65535 (a 16-bit value).



31
32
33
# File 'lib/redgreenblue/48bit.rb', line 31

def bb=(n)
  self.blue  = n / 65535.0
end

#bgr24Object

Returns a 3-byte string containing the object’s color in BGR24 format.



4
5
6
# File 'lib/redgreenblue/bgr24bit.rb', line 4

def bgr24
  [b, g, r].pack('C3')
end

#bgr24=(bgr_string) ⇒ Object

Sets red, green, and blue using BGR24 data (a 3-byte string).



9
10
11
# File 'lib/redgreenblue/bgr24bit.rb', line 9

def bgr24=(bgr_string)
  self.b, self.g, self.r = bgr_string.unpack('C3')
end

#blacken(portion = 0.5, *portions) ⇒ Object

Creates one or more new RGB::Colors by mixing this object’s color with a portion of black.



34
35
36
37
38
39
40
# File 'lib/redgreenblue/mix.rb', line 34

def blacken(portion=0.5, *portions)
  if (portion.class != Array) and portions.none?
    mix(RGB.black, portion)
  else
    ( [portion].flatten + portions ).map { |p| mix(RGB.black, p) }
  end
end

#blacken!(portion = 0.5) ⇒ Object

Changes the object’s color by mixing it with a portion of black.



29
30
31
# File 'lib/redgreenblue/mix.rb', line 29

def blacken!(portion=0.5)
  mix!(RGB.black, portion)
end

#blueObject

Returns the blue component as a value between 0 and 1.



27
28
29
# File 'lib/redgreenblue/base.rb', line 27

def blue
  @blue
end

#blue=(value) ⇒ Object

Sets the blue component to a value between 0 and 1.

Values outside the range 0..1 will be clipped.



48
49
50
# File 'lib/redgreenblue/base.rb', line 48

def blue=(value)
  @blue = limit(value)
end

#cie_lab(round: true) ⇒ Object Also known as: lab

Returns CIE 1976 L*a*b* (CIELAB) values for the RGB::Color object.



4
5
6
# File 'lib/redgreenblue/cie_1976.rb', line 4

def cie_lab(round: true)
  cie_lab_luv(round: round, type: :lab)
end

#cie_lch_abObject

Returns CIE 1976 LCHab values for the RGB::Color object, derived from L*a*b* (CIELAB).

When C is 0, H is nil.



13
14
15
# File 'lib/redgreenblue/cie_1976.rb', line 13

def cie_lch_ab
  cie_lch_ab_uv(type: :lab)
end

#cie_lch_uvObject

Returns CIE 1976 LCHuv values for the RGB::Color object, derived from L*u*v* (CIELUV).

When C is 0, H is nil.



27
28
29
# File 'lib/redgreenblue/cie_1976.rb', line 27

def cie_lch_uv
  cie_lch_ab_uv(type: :luv)
end

#cie_luv(round: true) ⇒ Object Also known as: luv

Returns CIE 1976 L*u*v* (CIELUV) values for the RGB::Color object.



18
19
20
# File 'lib/redgreenblue/cie_1976.rb', line 18

def cie_luv(round: true)
  cie_lab_luv(round: round, type: :luv)
end

#cie_xy(round: true) ⇒ Object Also known as: xy

Returns CIE 1931 xy values for the RGB::Color object.



75
76
77
# File 'lib/redgreenblue/cie_1931.rb', line 75

def cie_xy(round: true)
  cie_xyy(round: round)[0..1]
end

#cie_xyy(round: true) ⇒ Object Also known as: xyy



62
63
64
65
66
67
68
69
70
# File 'lib/redgreenblue/cie_1931.rb', line 62

def cie_xyy(round: true)
  x, y, z = cie_xyz(round: false)

  [
    x / ( x + y + z ),
    y / ( x + y + z ),
    y
  ].map { |v| round ? v.round(8) : v }
end

#cie_xyz(round: true) ⇒ Object Also known as: xyz

Returns CIE 1931 XYZ values for the RGB::Color object.

Based on:

sRGB to XYZ matrix for D65 reference white calculated with Javascript by Bruce Lindbloom:



25
26
27
28
29
30
31
32
33
# File 'lib/redgreenblue/cie_1931.rb', line 25

def cie_xyz(round: true)
  r, g, b = linear_values

  [
    r * 0.4124_5643_9090 + g * 0.3575_7607_7644 + b * 0.1804_3748_3266,
    r * 0.2126_7285_1406 + g * 0.7151_5215_5288 + b * 0.0721_7499_3307,
    r * 0.0193_3389_5582 + g * 0.1191_9202_5881 + b * 0.9503_0407_8536
  ].map { |v| round ? v.round(8) : v }
end

#cie_xyz=(*a) ⇒ Object Also known as: xyz=

Sets the red, green, and blue values by converting the given CIE 1931 XYZ values to RGB.

Assumes the XYZ values are relative to D65 reference white, the same as used in sRGB.

Based on:

XYZ to sRGB matrix for D65 reference white calculated with Javascript by Bruce Lindbloom:



45
46
47
48
49
50
51
52
# File 'lib/redgreenblue/cie_1931.rb', line 45

def cie_xyz=(*a)
  x, y, z = a.flatten
  self.linear_values = [
    x *  3.2404_5416_2114 + y * -1.5371_3851_2798 + z * -0.498_5314_09556,
    x * -0.9692_6603_0505 + y *  1.8760_1084_5447 + z *  0.0415_5601_7530,
    x *  0.0556_4343_0959 + y * -0.2040_2591_3517 + z *  1.0572_2518_8223
  ]
end

#color_spaceObject

Returns the color space.

Currently always ‘sRGB’.



12
13
14
# File 'lib/redgreenblue/base.rb', line 12

def color_space
  'sRGB'
end

#componentsObject

Returns an array of three RGB::Colors, for the red, green, and blue components of this object.



29
30
31
# File 'lib/redgreenblue/misc.rb', line 29

def components
  [ RGB::Color.new(red,0,0), RGB::Color.new(0,green,0), RGB::Color.new(0,0,blue) ]
end

#css_hexObject

Returns the object’s RGB value in hexadecimal notation as used in CSS.

Shortens to 3 digits when possible.



6
7
8
# File 'lib/redgreenblue/web.rb', line 6

def css_hex
  "##{hex true}"
end

#css_namesObject

Returns the names of CSS named colors identical to this color.

Examples:

RGB.hex('f0f').css_names


13
14
15
# File 'lib/redgreenblue/web.rb', line 13

def css_names
  RGB.css(self).map &:name
end

#de94g(another) ⇒ Object

Returns the difference between this (reference) color and another color, according to the CIE 1994 delta E formula.

For use in graphic arts, under reference conditions.



44
45
46
# File 'lib/redgreenblue/cie_1994.rb', line 44

def de94g(another)
  delta_e_cie_1994(another)
end

#de94t(another) ⇒ Object

Returns the difference between this (reference) color and another color, according to the CIE 1994 delta E formula.

For use with textiles, under reference conditions.



51
52
53
# File 'lib/redgreenblue/cie_1994.rb', line 51

def de94t(another)
  delta_e_cie_1994(another, k1: 0.048, k2: 0.014, kl: 2)
end

#delta_e_cie_1976(another) ⇒ Object Also known as: de76

Returns the difference between this color and another color, according to the CIE 1976 delta E formula.

Based on:



36
37
38
39
40
41
# File 'lib/redgreenblue/cie_1976.rb', line 36

def delta_e_cie_1976(another)
  l , a , b  =         cie_lab(round: false)
  l2, a2, b2 = another.cie_lab(round: false)

  Math.sqrt( (l - l2) ** 2 + (a - a2) ** 2 + (b - b2) ** 2 ).round(8)
end

#delta_e_cie_1994(another, k1: 0.045, k2: 0.015, kl: 1, kc: 1, kh: 1) ⇒ Object Also known as: de94

Returns the difference between this (reference) color and another color, according to the CIE 1994 delta E formula.

By default uses parameters for use in graphic arts, and reference conditions. Parameters (k1, k2, kl, kc, kh) can be individually overriden for different applications and for variations in conditions.

Based on:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/redgreenblue/cie_1994.rb', line 12

def delta_e_cie_1994(another, k1: 0.045, k2: 0.015, kl: 1, kc: 1, kh: 1)

  l , a , b  =         cie_lab(round: false)
  l2, a2, b2 = another.cie_lab(round: false)

  c  = Math.hypot(a , b )
  c2 = Math.hypot(a2, b2)

  da = a - a2
  db = b - b2
  dc = c - c2

  dh2 = (da ** 2) + (db ** 2) - (dc ** 2)
  dl = l - l2

  sl = 1
  sc = 1 + k1 * c
  sh = 1 + k2 * c

  Math.sqrt(
    ( (dl  / ( kl*sl)) ** 2  ) +
    ( (dc  / ( kc*sc)) ** 2  ) +
    (  dh2 / ((kh*sh)  ** 2) )
  ).round(6)

end

#distance(another) ⇒ Object

Returns the euclidean distance between this color and another color.

When you imagine a color as a point in a 3-dimensional space, the dimensions being red, green, and blue, this is the distance between two colors.



44
45
46
47
48
49
50
# File 'lib/redgreenblue/misc.rb', line 44

def distance(another)
  (
    ( (another.red   - red  ) ** 2) +
    ( (another.green - green) ** 2) +
    ( (another.blue  - blue ) ** 2)
  ) ** (1/2.0)
end

#gObject

Returns the green component as an integer in the range 0..255 (an 8-bit value).



11
12
13
# File 'lib/redgreenblue/24bit.rb', line 11

def g
  (green * 255).round
end

#g=(n) ⇒ Object

Sets the green component using an integer in the range 0..255 (an 8-bit value).



26
27
28
# File 'lib/redgreenblue/24bit.rb', line 26

def g=(n)
  self.green = n / 255.0
end

#ggObject

Returns the green component as an integer in the range 0..65535 (a 16-bit value).



11
12
13
# File 'lib/redgreenblue/48bit.rb', line 11

def gg
  (green * 65535).round
end

#gg=(n) ⇒ Object

Sets the green component using an integer in the range 0..65535 (a 16-bit value).



26
27
28
# File 'lib/redgreenblue/48bit.rb', line 26

def gg=(n)
  self.green = n / 65535.0
end

#gif_pixelObject

Returns a 1-pixel GIF image set to the color.

With help from:



7
8
9
10
11
# File 'lib/redgreenblue/gif.rb', line 7

def gif_pixel
  "GIF89a\1\0\1\0\x90\0\0".b +
  rgb.pack('C3') +
  "\0\0\0,\0\0\0\0\1\0\1\0\0\x02\x02\x04\1\0;".b
end

#gif_pixel_write(file_path) ⇒ Object

Writes a 1-pixel GIF image to a file.



14
15
16
# File 'lib/redgreenblue/gif.rb', line 14

def gif_pixel_write(file_path)
  File.binwrite(file_path, gif_pixel)
end

#gpl(gpl_name = name) ⇒ Object

Returns the color in the format used in .gpl files (Gimp color palettes), including its name (if present).

You can optionally supply a name as argument.



25
26
27
28
# File 'lib/redgreenblue/gpl.rb', line 25

def gpl(gpl_name=name)
  ( "%3d %3d %3d" % rgb ) +
  ( gpl_name.to_s.size != 0 ? "\t#{gpl_name}" : '' )
end

#greenObject

Returns the green component as a value between 0 and 1.



22
23
24
# File 'lib/redgreenblue/base.rb', line 22

def green
  @green
end

#green=(value) ⇒ Object

Sets the green component to a value between 0 and 1.

Values outside the range 0..1 will be clipped.



41
42
43
# File 'lib/redgreenblue/base.rb', line 41

def green=(value)
  @green = limit(value)
end

#hex(shorthand = false) ⇒ Object

Returns a 6-digit hexadecimal string representing the object’s red, green, and blue components as 8-bit values.

If shorthand is true, returns 3-digit shorthand if possible.



6
7
8
9
10
11
12
# File 'lib/redgreenblue/hex.rb', line 6

def hex(shorthand=false)
  if shorthand
    RGB.hex_shorthand hex6
  else
    hex6
  end
end

#hex=(hex_string) ⇒ Object

Sets red, green, and blue using a 6- or 3-digit hexadecimal string.

The string may include a ‘#’ prefix.



17
18
19
20
21
22
23
# File 'lib/redgreenblue/hex.rb', line 17

def hex=(hex_string)
  if rgb = RGB.hex_to_rgb(hex_string)
    self.rgb = rgb
  else
    raise ArgumentError, "'#{hex_string}' is not a usable hexadecimal string"
  end
end

#hsbObject

Returns color as HSB: hue (0..360), saturation (0..1), brightness (0..1). When saturation is 0, hue is nil.



15
# File 'lib/redgreenblue/hsb.rb', line 15

alias hsb         hsv

#hsb=Object

Sets red, green, and blue using HSB values: hue (0..360), saturation (0..1), and brightness (0..1).



27
# File 'lib/redgreenblue/hsb.rb', line 27

alias hsb=        hsv=

#hsb_bObject

Returns the object’s HSB-brightness (0..1).



24
# File 'lib/redgreenblue/hsb.rb', line 24

alias hsb_b       hsv_v

#hsb_b=Object

Sets HSB-brightness to a number between 0 and 1.

Adjusts red, green, and blue, leaving HSB-hue and -saturation unchanged. When brightness is 0, hue will be nil, and saturation will be 0.



45
# File 'lib/redgreenblue/hsb.rb', line 45

alias hsb_b=      hsv_v=

#hsb_hObject

Returns the object’s HSB-hue (0..360).



18
# File 'lib/redgreenblue/hsb.rb', line 18

alias hsb_h       hsv_h

#hsb_h=Object

Sets HSB-hue to a number of degrees (0..360) or nil.

Adjusts red, green, and blue, leaving HSB-saturation and -brightness unchanged. When hue is nil, saturation will be 0.



33
# File 'lib/redgreenblue/hsb.rb', line 33

alias hsb_h=      hsv_h=

#hsb_rotateObject

Creates one or more new RGB::Color objects by rotating this object’s HSB-hue a number of degrees.



51
# File 'lib/redgreenblue/hsb.rb', line 51

alias hsb_rotate  hsv_rotate

#hsb_rotate!Object

Sets red, green, and blue by rotating the object’s HSB-hue a number of degrees.



48
# File 'lib/redgreenblue/hsb.rb', line 48

alias hsb_rotate! hsv_rotate!

#hsb_sObject

Returns the object’s HSB-saturation (0..1).



21
# File 'lib/redgreenblue/hsb.rb', line 21

alias hsb_s       hsv_s

#hsb_s=Object

Sets HSB-saturation to a number between 0 and 1.

Adjusts red, green, and blue, leaving HSB-hue and -brightness unchanged. When saturation is 0, hue will be nil.



39
# File 'lib/redgreenblue/hsb.rb', line 39

alias hsb_s=      hsv_s=

#hslObject

Returns color as HSL: hue (0..360), saturation (0..1), lightness (0..1). When saturation is 0, hue is nil.



25
26
27
# File 'lib/redgreenblue/hsl.rb', line 25

def hsl
  hsl_hsv_c[0]
end

#hsl=(*a) ⇒ Object

Sets red, green, and blue using HSL values: hue (0..360), saturation (0..1), and lightness (0..1).



45
46
47
# File 'lib/redgreenblue/hsl.rb', line 45

def hsl=(*a)
  self.values = RGB::Color.hsl_to_values(*a)
end

#hsl_hObject

Returns the object’s HSL-hue (0..360).



30
31
32
# File 'lib/redgreenblue/hsl.rb', line 30

def hsl_h
  hsl[0]
end

#hsl_h=(degrees) ⇒ Object

Sets HSL-hue to a number of degrees (0..360) or nil.

Adjusts red, green, and blue, leaving saturation and lightness unchanged. When hue is nil, saturation will be 0.



53
54
55
# File 'lib/redgreenblue/hsl.rb', line 53

def hsl_h=(degrees)
  self.hsl = hsl.fill(degrees,0,1)
end

#hsl_lObject

Returns the object’s HSL-lightness (0..1).



40
41
42
# File 'lib/redgreenblue/hsl.rb', line 40

def hsl_l
  hsl[2]
end

#hsl_l=(value) ⇒ Object

Sets HSL-lightness to a value between 0 and 1.

Adjusts red, green, and blue, leaving hue and saturation unchanged. When lightness is 0 or 1, hue will be nil, and saturation will be 0.



69
70
71
# File 'lib/redgreenblue/hsl.rb', line 69

def hsl_l=(value)
  self.hsl = hsl.fill(value  ,2,1)
end

#hsl_rotate(a_degrees, *b_degrees) ⇒ Object

Creates one or more new RGB::Color objects by rotating this object’s HSL-hue a number of degrees.



80
81
82
83
84
85
86
# File 'lib/redgreenblue/hsl.rb', line 80

def hsl_rotate(a_degrees, *b_degrees)
  if a_degrees.class != Array and b_degrees.none?
    RGB.hsl zip_add(hsl, [a_degrees, 0, 0])
  else
    ( [a_degrees] + b_degrees ).flatten.map { |degrees| RGB.hsl zip_add(hsl, [degrees, 0, 0]) }
  end
end

#hsl_rotate!(degrees) ⇒ Object

Sets red, green, and blue by rotating the object’s HSL-hue a number of degrees.



74
75
76
77
# File 'lib/redgreenblue/hsl.rb', line 74

def hsl_rotate!(degrees)
  self.hsl = zip_add(hsl, [degrees, 0, 0])
  self
end

#hsl_sObject

Returns the object’s HSL-saturation (0..1).



35
36
37
# File 'lib/redgreenblue/hsl.rb', line 35

def hsl_s
  hsl[1]
end

#hsl_s=(value) ⇒ Object

Sets HSL-saturation to a value between 0 and 1.

Adjusts red, green, and blue, leaving hue and lightness unchanged. When saturation is 0, hue will be nil.



61
62
63
# File 'lib/redgreenblue/hsl.rb', line 61

def hsl_s=(value)
  self.hsl = hsl.fill(value  ,1,1)
end

#hsvObject

Returns color as HSV: hue (0..360), saturation (0..1), value (0..1). When saturation is 0, hue is nil.



25
26
27
# File 'lib/redgreenblue/hsv.rb', line 25

def hsv
  hsl_hsv_c[1]
end

#hsv=(*a) ⇒ Object

Sets red, green, and blue using HSV values: hue (0..360), saturation (0..1), and value (0..1).



45
46
47
# File 'lib/redgreenblue/hsv.rb', line 45

def hsv=(*a)
  self.values = RGB::Color.hsv_to_values(*a)
end

#hsv_hObject

Returns the object’s HSV-hue (0..360).



30
31
32
# File 'lib/redgreenblue/hsv.rb', line 30

def hsv_h
  hsv[0]
end

#hsv_h=(degrees) ⇒ Object

Sets HSV-hue to a number of degrees (0..360) or nil.

Adjusts red, green, and blue, leaving HSV-saturation and -value unchanged. When hue is nil, saturation will be 0.



53
54
55
# File 'lib/redgreenblue/hsv.rb', line 53

def hsv_h=(degrees)
  self.hsv = hsv.fill(degrees,0,1)
end

#hsv_rotate(a_degrees, *b_degrees) ⇒ Object

Creates one or more new RGB::Color objects by rotating this object’s HSV-hue a number of degrees.



80
81
82
83
84
85
86
# File 'lib/redgreenblue/hsv.rb', line 80

def hsv_rotate(a_degrees, *b_degrees)
  if a_degrees.class != Array and b_degrees.none?
    RGB.hsv zip_add(hsv, [a_degrees, 0, 0])
  else
    ( [a_degrees] + b_degrees ).flatten.map { |degrees| RGB.hsv zip_add(hsv, [degrees, 0, 0]) }
  end
end

#hsv_rotate!(degrees) ⇒ Object

Sets red, green, and blue by rotating the object’s HSV-hue a number of degrees.



74
75
76
77
# File 'lib/redgreenblue/hsv.rb', line 74

def hsv_rotate!(degrees)
  self.hsv = zip_add(hsv, [degrees, 0, 0])
  self
end

#hsv_sObject

Returns the object’s HSV-saturation (0..1).



35
36
37
# File 'lib/redgreenblue/hsv.rb', line 35

def hsv_s
  hsv[1]
end

#hsv_s=(value) ⇒ Object

Sets HSV-saturation to a number between 0 and 1.

Adjusts red, green, and blue, leaving HSV-hue and -value unchanged. When saturation is 0, hue will be nil.



61
62
63
# File 'lib/redgreenblue/hsv.rb', line 61

def hsv_s=(value)
  self.hsv = hsv.fill(value  ,1,1)
end

#hsv_vObject

Returns the object’s HSV-value (0..1).



40
41
42
# File 'lib/redgreenblue/hsv.rb', line 40

def hsv_v
  hsv[2]
end

#hsv_v=(value) ⇒ Object

Sets HSV-value to a number between 0 and 1.

Adjusts red, green, and blue, leaving HSV-hue and -saturation unchanged. When value is 0, hue will be nil, and saturation will be 0.



69
70
71
# File 'lib/redgreenblue/hsv.rb', line 69

def hsv_v=(value)
  self.hsv = hsv.fill(value  ,2,1)
end

#hwbObject

Returns the color’s hue (0..360), whiteness (0..1), and blackness (0..1), as defined by the HWB color model.

For achromatic colors, hue is nil.

Based on:



10
11
12
# File 'lib/redgreenblue/hwb.rb', line 10

def hwb
  [ hsv_h, cwk[1,2] ].flatten
end

#inspectObject

Returns a programmer-friendly representation of the object.

You can choose among several inspect styles. See the styles, style, and style= class methods.



45
46
47
# File 'lib/redgreenblue/inspect.rb', line 45

def inspect
  send "_inspect_#{RGB.style}"
end

#invertObject

Creates a new RGB::Color with the inverted color of this RGB object.



10
11
12
# File 'lib/redgreenblue/misc.rb', line 10

def invert
  dup.invert!
end

#invert!Object

Inverts the object’s color.



4
5
6
7
# File 'lib/redgreenblue/misc.rb', line 4

def invert!
  self.values = values.map { |v| 1 - v }
  self
end

#linear_valuesObject

Returns gamma-expanded (inverse-companded) RGB values for the object (three values between 0 and 1).

Based on:



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/redgreenblue/gamma.rb', line 9

def linear_values
  if color_space == 'sRGB'
    values.map { |v|
      if v <= 0.04045
        v / 12.92
      else
        ( ( v + 0.055 ) / 1.055 ) ** 2.4
      end
    }
  else
    raise "can not compute gamma for color space '#{color_space}'"
  end
end

#linear_values=(*a) ⇒ Object

Sets the object’s RGB values using three linear RGB values, each between 0 and 1. Linear values will be converted to the object’s gamma (gamma-companded).

Based on:



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/redgreenblue/gamma.rb', line 30

def linear_values=(*a)
  if color_space == 'sRGB'
    self.values = a.flatten.map { |v|
      if v <= 0.0031308
        v * 12.92
      else
        1.055 * ( v ** ( 1/2.4 ) ) - 0.055
      end
    }.map { |v| v.round(9) }
  else
    raise "can not compute gamma for color space '#{color_space}'"
  end
end

#match_de76(others) ⇒ Object

Matches this color to a set of colors using the CIE 1976 delta E formula.

Returns the given set of colors with their difference from this color, sorted by difference (nearest color first).

Examples:

Find the 3 nearest CSS named colors

RGB.hex('f9c').match_de76(RGB.css).first(3)


17
18
19
# File 'lib/redgreenblue/match.rb', line 17

def match_de76(others)
  others.map { |c| [ c, de76(c) ] }.sort_by { |a| a[1] }
end

#match_de94(others, k1: 0.045, k2: 0.015, kl: 1, kc: 1, kh: 1) ⇒ Object

Matches this (reference) color to a set of colors using the CIE 1994 delta E formula.

Returns the given set of colors with their difference from this color, sorted by difference (nearest color first).

By default uses parameters for use in graphic arts, and reference conditions. Parameters (k1, k2, kl, kc, kh) can be individually overriden for different applications and for variations in conditions.



27
28
29
# File 'lib/redgreenblue/match.rb', line 27

def match_de94(others, k1: 0.045, k2: 0.015, kl: 1, kc: 1, kh: 1)
  others.map { |c| [ c, de94(c, k1: k1, k2: k2, kl: kl, kc: kc, kh: kh) ] }.sort_by { |a| a[1] }
end

#match_de94g(others) ⇒ Object

Matches this (reference) color to a set of colors using the CIE 1994 delta E formula.

For use in graphic arts, under reference conditions.

Returns the given set of colors with their difference from this color, sorted by difference (nearest color first).



36
37
38
# File 'lib/redgreenblue/match.rb', line 36

def match_de94g(others)
  others.map { |c| [ c, de94g(c) ] }.sort_by { |a| a[1] }
end

#match_de94t(others) ⇒ Object

Matches this (reference) color to a set of colors using the CIE 1994 delta E formula.

For use with textiles, under reference conditions.

Returns the given set of colors with their difference from this color, sorted by difference (nearest color first).



45
46
47
# File 'lib/redgreenblue/match.rb', line 45

def match_de94t(others)
  others.map { |c| [ c, de94t(c) ] }.sort_by { |a| a[1] }
end

#match_distance(others) ⇒ Object

Matches this color to a set of colors by calculating their euclidean distance.

Returns the given set of colors with their distance from this color, sorted by distance (nearest color first).

Examples:

What is nearer: red, grey or white?

RGB.hex('f9c').match_distance [RGB.red, RGB.grey, RGB.white]


8
9
10
# File 'lib/redgreenblue/match.rb', line 8

def match_distance(others)
  others.map { |c| [ c, distance(c) ] }.sort_by { |a| a[1] }
end

#mix(another, portion = 0.5) ⇒ Object

Creates a new RGB::Color by mixing this object’s color with a portion of another RGB::Color.



10
11
12
# File 'lib/redgreenblue/mix.rb', line 10

def mix(another,portion=0.5)
  RGB::Color.new mix_values(another.values, portion)
end

#mix!(another, portion = 0.5) ⇒ Object

Changes the object’s color by mixing it with a portion of another RGB::Color.



4
5
6
7
# File 'lib/redgreenblue/mix.rb', line 4

def mix!(another,portion=0.5)
  self.values = mix_values(another.values, portion)
  self
end

#nameObject

Returns the name.



4
5
6
# File 'lib/redgreenblue/name.rb', line 4

def name
  defined?(@name) ? @name : nil
end

#name=(text) ⇒ Object

Sets the name (a string or nil).



9
10
11
# File 'lib/redgreenblue/name.rb', line 9

def name=(text)
  @name = ( text.to_s.size != 0 ) ? text.to_s : nil
end

#ostwald_colorObject

Returns a new RGB::Color with this color’s Ostwald full-color, or nil for achromatic colors (white, greys, and black).

The resulting color contains no white or black, i.e. it has at least one RGB component set to 0, and at least one set to maximum.

This is identical (barring very small rounding errors) to setting HSL-saturation to 1, and -lightness to 0.5

Based on:



15
16
17
18
19
20
21
22
23
24
# File 'lib/redgreenblue/ostwald.rb', line 15

def ostwald_color
  white_portion = values.min
  color_portion = values.max - white_portion

  if color_portion == 0
    nil
  else
    RGB::Color.new( values.map { |v| ( ( v - white_portion ) / color_portion ).round(6) } )
  end
end

#ostwald_cwk(round: true) ⇒ Object Also known as: cwk

Returns the portions of Ostwald full-color, white, and black, which constitute this color.

The sum of these three numbers equals 1.

#cwk is an alias for #ostwald_cwk.

Based on:



35
36
37
38
39
40
41
# File 'lib/redgreenblue/ostwald.rb', line 35

def ostwald_cwk(round: true)
  [
    color_portion = values.max - values.min,
    white_portion = values.min,
    1 - color_portion - white_portion
  ].map { |v| round ? v.round(8) : v }
end

#permutationObject

Returns an array of RGB::Colors for all possible ways in which the red, green, and blue values of this object can be exchanged.

Example: RGB.red.permutation returns [ RGB.red, RGB.green, RGB.blue ]. See also: shuffle.



24
25
26
# File 'lib/redgreenblue/misc.rb', line 24

def permutation
  values.permutation.to_a.uniq.map { |v| RGB::Color.new v }
end

#pickObject

On Mac OS, shows the color picker to choose a color for this object. Not available on other platforms.



5
6
7
8
9
10
# File 'lib/redgreenblue/os/mac.rb', line 5

def pick
  result = RGB::Color.mac_choose(self)
  if result
    self.rrggbb = result
  end
end

#rObject

Returns the red component as an integer in the range 0..255 (an 8-bit value).



6
7
8
# File 'lib/redgreenblue/24bit.rb', line 6

def r
  (red   * 255).round
end

#r=(n) ⇒ Object

Sets the red component using an integer in the range 0..255 (an 8-bit value).



21
22
23
# File 'lib/redgreenblue/24bit.rb', line 21

def r=(n)
  self.red   = n / 255.0
end

#randomize!Object

Assigns random values to red, green, and blue.



15
16
17
18
# File 'lib/redgreenblue/random.rb', line 15

def randomize!
  self.values = Kernel::rand, Kernel::rand, Kernel::rand
  self
end

#redObject

Returns the red component as a value between 0 and 1.



17
18
19
# File 'lib/redgreenblue/base.rb', line 17

def red
  @red
end

#red=(value) ⇒ Object

Sets the red component to a value between 0 and 1.

Values outside the range 0..1 will be clipped.



34
35
36
# File 'lib/redgreenblue/base.rb', line 34

def red=(value)
  @red = limit(value)
end

#rgbObject

Returns the red, green, and blue components as integers in the range 0..255 (three 8-bit values).



38
39
40
# File 'lib/redgreenblue/24bit.rb', line 38

def rgb
  [r,g,b]
end

#rgb565Object

Returns the color in 16-bit RGB565 format.



4
5
6
# File 'lib/redgreenblue/rgb565.rb', line 4

def rgb565
  [((r >> 3) << 11) + ((g >> 2) << 5) + (b >> 3)].pack 'S<'
end

#rgb565=(rgb565_string) ⇒ Object

Sets the color from 16-bit RGB565 data. With help from:



11
12
13
14
15
16
# File 'lib/redgreenblue/rgb565.rb', line 11

def rgb565=(rgb565_string)
  v = ( rgb565_string.unpack "S<" )[0]
  self.r = ( ( v & 0xf800 ) >> 11 ) << 3
  self.g = ( ( v & 0x07e0 ) >>  5 ) << 2
  self.b = ( ( v & 0x001f )       ) << 3
end

#rgb=(*rgb) ⇒ Object

Sets the red, green, and blue components using three integers in the range 0..255 (three 8-bit values).



43
44
45
# File 'lib/redgreenblue/24bit.rb', line 43

def rgb=(*rgb)
  self.r, self.g, self.b = rgb.flatten
end

#rrObject

Returns the red component as an integer in the range 0..65535 (a 16-bit value).



6
7
8
# File 'lib/redgreenblue/48bit.rb', line 6

def rr
  (red   * 65535).round
end

#rr=(n) ⇒ Object

Sets the red component using an integer in the range 0..65535 (a 16-bit value).



21
22
23
# File 'lib/redgreenblue/48bit.rb', line 21

def rr=(n)
  self.red   = n / 65535.0
end

#rrggbbObject

Returns the red, green, and blue components as integers in the range 0..65535 (three 16-bit values).



38
39
40
# File 'lib/redgreenblue/48bit.rb', line 38

def rrggbb
  [rr,gg,bb]
end

#rrggbb=(*rrggbb) ⇒ Object

Sets the red, green, and blue components using three integers in the range 0..65535 (three 16-bit values).



43
44
45
# File 'lib/redgreenblue/48bit.rb', line 43

def rrggbb=(*rrggbb)
  self.rr, self.gg, self.bb = rrggbb.flatten
end

#shuffleObject

Creates a new RGB::Color with this object’s red, green, and blue values shuffled.



10
11
12
# File 'lib/redgreenblue/random.rb', line 10

def shuffle
  RGB::Color.new values.shuffle
end

#shuffle!Object

Shuffles the object’s red, green, and blue values.



4
5
6
7
# File 'lib/redgreenblue/random.rb', line 4

def shuffle!
  self.values = values.shuffle
  self
end

#snapObject

Creates a new RGB::Color containing the nearest 24-bit color.



61
62
63
# File 'lib/redgreenblue/24bit.rb', line 61

def snap
  RGB.rgb rgb
end

#snap!Object

Sets the red, green, and blue values to those of the nearest 24-bit color.



55
56
57
58
# File 'lib/redgreenblue/24bit.rb', line 55

def snap!
  self.rgb = rgb
  self
end

#steps(another, step_count = 1, include_begin = false) ⇒ Object

Returns a number of colors, gradually changing from this color to another color.

The resulting colors are spaced evenly in the RGB color space.



45
46
47
48
49
50
51
52
# File 'lib/redgreenblue/mix.rb', line 45

def steps(another,step_count=1,include_begin=false)
  # origin (self, optional)
  ( include_begin ? [self.dup] : [] ) +
  # ...plus intermediate colors
  (1..step_count-1).map { |c| mix(another, c.to_f/step_count) } +
  # ...plus destination color
  [another.dup]
end

#steps_hsl(another, step_count = 1, include_begin = false) ⇒ Object

Returns a number of colors, gradually changing from this color to another color.

The resulting colors are spaced evenly by their HSL values (hue, saturation, and lightness).



57
58
59
# File 'lib/redgreenblue/mix.rb', line 57

def steps_hsl(another,step_count=1,include_begin=false)
  steps_hsx(another,step_count,include_begin, :hsl)
end

#steps_hsv(another, step_count = 1, include_begin = false) ⇒ Object Also known as: steps_hsb

Returns a number of colors, gradually changing from this color to another color.

The resulting colors are spaced evenly by their HSV values (hue, saturation, and value).



64
65
66
# File 'lib/redgreenblue/mix.rb', line 64

def steps_hsv(another,step_count=1,include_begin=false)
  steps_hsx(another,step_count,include_begin, :hsv)
end

#terminal_backgroundObject

Returns the ANSI escape sequence required to set the background color on a terminal to this color. Only works on terminals that support 24-bit colors, so-called “true color”.



15
16
17
# File 'lib/redgreenblue/terminal.rb', line 15

def terminal_background
  "\e[48;2;%d;%d;%dm" % rgb
end

#terminal_foregroundObject

Returns the ANSI escape sequence required to set the foreground color on a terminal to this color. Only works on terminals that support 24-bit colors, so-called “true color”.



9
10
11
# File 'lib/redgreenblue/terminal.rb', line 9

def terminal_foreground
  "\e[38;2;%d;%d;%dm" % rgb
end

#to_hObject

Returns a sorted hash of 3 key/value pairs for red, green, and blue, sorted in order of decreasing value.



73
74
75
76
77
# File 'lib/redgreenblue/base.rb', line 73

def to_h
  ([:red, :green, :blue].zip values).sort_by {
    |k,v| [-v,[:red, :green, :blue].index(k)]
  }.to_h
end

#to_iObject

Returns the color as a 24-bit integer in the range 0..16777215.



4
5
6
# File 'lib/redgreenblue/int.rb', line 4

def to_i
  ( r << 16 ) + ( g << 8 ) + b
end

#to_philips_hue_api_hsb_arguments(black_off = true) ⇒ Hash

Only available when optional support for Philips Hue lights is loaded.

Returns a hash with the arguments required by the Philips Hue API, to set a light to this RGB::Color’s hue, saturation and brightness.

Formatted as JSON, this hash can be sent to a bridge to set a light’s state.

See (regrettably requires registration):

Examples:

Load

require 'redgreenblue/opt/philipshue'

Use

RGB.magenta.to_philips_hue_api_hsb_arguments
=> {"on"=>true, "bri"=>254, "hue"=>54613, "sat"=>254}
RGB.black.to_philips_hue_api_hsb_arguments
=> {"on"=>false}

Returns:

  • (Hash)

    API arguments



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/redgreenblue/opt/philipshue.rb', line 28

def to_philips_hue_api_hsb_arguments(black_off=true)
  my_hsb = hsb

  # Black means 'off'
  if black_off and ( my_hsb[2] == 0 )
    { 'on'  => false }

  else
    {

      # On/Off state of the light
      'on'  => true,

      # Brightness 1..254
      # Note: a brightness of 1 will not switch the light off
      'bri' => (  my_hsb[2]        * 253 + 1     ).round,

      # Hue 0..65535
      'hue' => (( my_hsb[0] || 0 ) * 65535 / 360 ).round,

      # Saturation 0..254
      'sat' => (  my_hsb[1]        * 254         ).round

    }

  end
end

#to_sObject

Returns a string representation of the object.



50
51
52
# File 'lib/redgreenblue/inspect.rb', line 50

def to_s
  _inspect_default
end

#valuesObject Also known as: to_a

Returns the red, green, and blue components as three values between 0 and 1.



53
54
55
# File 'lib/redgreenblue/base.rb', line 53

def values
  [ red, green, blue ]
end

#values=(*a) ⇒ Object

Sets the red, green, and blue components using three values between 0 and 1.

Values outside the range 0..1 will be clipped.



62
63
64
# File 'lib/redgreenblue/base.rb', line 62

def values=(*a)
  self.red, self.green, self.blue = a.flatten
end

#view(other = nil) ⇒ Object Also known as: v

Prints a color swatch and details for the RGB::Color object, using multiple lines.

You can optionally supply a second color to be shown inside the swatch, for comparison.



6
7
8
# File 'lib/redgreenblue/view.rb', line 6

def view(other=nil)
  puts _inspect_view other
end

#wcag20_contrast_ratio(another, round: true) ⇒ Object

Returns the contrast ratio for this color and another color, as defined by the Web Content Accessibility Guidelines (WCAG) 2.0.

Based on:



43
44
45
46
47
48
49
50
51
52
# File 'lib/redgreenblue/web.rb', line 43

def wcag20_contrast_ratio(another, round: true)
  luminances = [
    wcag20_luminance(round: false),
    another.wcag20_luminance(round: false)
  ].sort.reverse

  contrast_ratio = ( luminances[0] + 0.05 ) / ( luminances[1] + 0.05 )

  round ? contrast_ratio.round(8) : contrast_ratio
end

#wcag20_luminance(round: true) ⇒ Object

Returns the color’s relative luminance, as defined by the Web Content Accessibility Guidelines (WCAG) 2.0.

This is different from the Y component of CIE 1931 XYZ.

Based on:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/redgreenblue/web.rb', line 23

def wcag20_luminance(round: true)
  if color_space == 'sRGB'
    values.map { |v|
      if v <= 0.03928
        v / 12.92
      else
        ( ( v + 0.055 ) / 1.055 ) ** 2.4
      end
    }.zip( [0.2126, 0.7152, 0.0722] ).map { |c,f|
      c * f
    }.inject(:+).instance_eval { |v| round ? v.round(8) : v }
  else
    raise "can not calculate luminance for color space '#{color_space}'"
  end
end

#web_safe?Boolean

Returns true if this is one of the 216 so-called “web safe” colors, otherwise false.

Returns:

  • (Boolean)


55
56
57
# File 'lib/redgreenblue/web.rb', line 55

def web_safe?
  ( values - [0.0, 0.2, 0.4, 0.6, 0.8, 1.0] ).empty?
end

#whiten(portion = 0.5, *portions) ⇒ Object

Creates one or more new RGB::Colors by mixing this object’s color with a portion of white.



20
21
22
23
24
25
26
# File 'lib/redgreenblue/mix.rb', line 20

def whiten(portion=0.5, *portions)
  if (portion.class != Array) and portions.none?
    mix(RGB.white, portion)
  else
    ( [portion].flatten + portions ).map { |p| mix(RGB.white, p) }
  end
end

#whiten!(portion = 0.5) ⇒ Object

Changes the object’s color by mixing it with a portion of white.



15
16
17
# File 'lib/redgreenblue/mix.rb', line 15

def whiten!(portion=0.5)
  mix!(RGB.white, portion)
end

#yuv(round: true) ⇒ Object

Returns the color’s YUV values.

Based on:



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/redgreenblue/yuv.rb', line 10

def yuv(round: true)
  [
    red *  0.299 + green *  0.587 + blue *  0.114,
    red * -0.147 + green * -0.289 + blue *  0.436,
    red *  0.615 + green * -0.515 + blue * -0.100
  ].map { |v| round ? v.round(6) : v }

  # Wikipedia (2024-Aug-7)
  # suggests slightly different factors for U and V:
  #
  # U = red * -0.14713 + green * -0.28886 + blue *  0.436
  # V = red *  0.615   + green * -0.51499 + blue * -0.10001
end