Class: ColorMath::RGB
- Inherits:
-
Object
- Object
- ColorMath::RGB
- Includes:
- Color
- Defined in:
- lib/colormath/color/rgb.rb
Overview
A colour represented and stored as red, green and blue components
Constant Summary
Constants included from Color
Instance Attribute Summary collapse
-
#blue ⇒ Object
readonly
Returns the value of attribute blue.
-
#green ⇒ Object
readonly
Returns the value of attribute green.
-
#red ⇒ Object
readonly
Returns the value of attribute red.
Instance Method Summary collapse
-
#hue ⇒ Object
The hue component of the colour in HSL representation where 0 <= h < 360.
-
#initialize(r, g, b) ⇒ RGB
constructor
Initialize an RGB colour where: 0 <= r <= 1 0 <= g <= 1 0 <= b <= 1.
-
#luminance ⇒ Object
The luminance component of the colour in HSL representation where 0 <= l <= 1.
-
#saturation ⇒ Object
The saturation component of the colour in HSL representation where 0 <= s <= 1.
Methods included from Color
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
#blue ⇒ Object (readonly)
Returns the value of attribute blue.
8 9 10 |
# File 'lib/colormath/color/rgb.rb', line 8 def blue @blue end |
#green ⇒ Object (readonly)
Returns the value of attribute green.
8 9 10 |
# File 'lib/colormath/color/rgb.rb', line 8 def green @green end |
#red ⇒ Object (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
#hue ⇒ Object
The hue component of the colour in HSL representation where 0 <= h < 360
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/colormath/color/rgb.rb', line 25 def hue 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 |
#luminance ⇒ Object
The luminance component of the colour in HSL representation where 0 <= l <= 1
50 51 52 |
# File 'lib/colormath/color/rgb.rb', line 50 def luminance 0.5 * (max + min) end |
#saturation ⇒ Object
The saturation component of the colour in HSL representation where 0 <= s <= 1
38 39 40 41 42 43 44 45 46 |
# File 'lib/colormath/color/rgb.rb', line 38 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 |