Class: ColorMath::RGB

Inherits:
Object
  • Object
show all
Includes:
Color
Defined in:
lib/colormath/color/rgb.rb

Overview

A colour represented and stored as red, green and blue components

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Color

#compact_hex, #hex, #inspect

Constructor Details

#initialize(r, g, b) ⇒ RGB

Initialize an RGB colour where:

0 <= r <= 1
0 <= g <= 1
0 <= b <= 1

Values outside these ranges will be clippped.



17
18
19
20
21
# File 'lib/colormath/color/rgb.rb', line 17

def initialize(r, g, b)
  @red   = force_range(r, 0, 1).to_f
  @green = force_range(g, 0, 1).to_f
  @blue  = force_range(b, 0, 1).to_f
end

Instance Attribute Details

#blueObject (readonly)

Returns the value of attribute blue.



8
9
10
# File 'lib/colormath/color/rgb.rb', line 8

def blue
  @blue
end

#greenObject (readonly)

Returns the value of attribute green.



8
9
10
# File 'lib/colormath/color/rgb.rb', line 8

def green
  @green
end

#redObject (readonly)

Returns the value of attribute red.



8
9
10
# File 'lib/colormath/color/rgb.rb', line 8

def red
  @red
end

Instance Method Details

#hueObject

The hue component of the colour in HSL representation where 0 <= h < 360



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/colormath/color/rgb.rb', line 25

def hue
  return 0 if saturation.zero?
  case max
  when red
    (60.0 * ((green - blue) / (max - min))) % 360.0
  when green
    60.0 * ((blue - red) / (max - min)) + 120.0
  when blue
    60.0 * ((red - green) / (max - min)) + 240.0
  end
end

#luminanceObject

The luminance component of the colour in HSL representation where 0 <= l <= 1



51
52
53
# File 'lib/colormath/color/rgb.rb', line 51

def luminance
  0.5 * (max + min)
end

#saturationObject

The saturation component of the colour in HSL representation where 0 <= s <= 1



39
40
41
42
43
44
45
46
47
# File 'lib/colormath/color/rgb.rb', line 39

def saturation
  if max == min
    0
  elsif luminance <= 0.5
    (max - min) / (2.0 * luminance)
  else
    (max - min) / (2.0 - 2.0 * luminance)
  end
end