Class: Compass::Colors::HSL
- Inherits:
-
Object
- Object
- Compass::Colors::HSL
- Defined in:
- lib/compass-colors/hsl.rb
Instance Attribute Summary collapse
-
#h ⇒ Object
Stored in degrees [0, 360).
-
#l ⇒ Object
Stored as a number from [0,1].
-
#s ⇒ Object
Stored as a number from [0,1].
Class Method Summary collapse
- .from_color(color) ⇒ Object
- .from_fractions(hue, saturation, lightness) ⇒ Object
- .from_rgb(r, g, b) ⇒ Object
Instance Method Summary collapse
-
#initialize(h, s, l) ⇒ HSL
constructor
A new instance of HSL.
- #to_color ⇒ Object
Constructor Details
#initialize(h, s, l) ⇒ HSL
Returns a new instance of HSL.
53 54 55 56 57 |
# File 'lib/compass-colors/hsl.rb', line 53 def initialize(h, s, l) self.h = h self.s = s self.l = l end |
Instance Attribute Details
#h ⇒ Object
Stored in degrees [0, 360)
6 7 8 |
# File 'lib/compass-colors/hsl.rb', line 6 def h @h end |
#l ⇒ Object
Stored as a number from [0,1]
8 9 10 |
# File 'lib/compass-colors/hsl.rb', line 8 def l @l end |
#s ⇒ Object
Stored as a number from [0,1]
8 9 10 |
# File 'lib/compass-colors/hsl.rb', line 8 def s @s end |
Class Method Details
.from_color(color) ⇒ Object
10 11 12 13 |
# File 'lib/compass-colors/hsl.rb', line 10 def self.from_color(color) rgb = color.respond_to?(:rgb) ? color.rgb : color.value from_rgb(*rgb) end |
.from_fractions(hue, saturation, lightness) ⇒ Object
49 50 51 |
# File 'lib/compass-colors/hsl.rb', line 49 def self.from_fractions(hue, saturation, lightness) HSL.new(360 * hue, saturation, lightness) end |
.from_rgb(r, g, b) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/compass-colors/hsl.rb', line 15 def self.from_rgb(r, g, b) rgb = [r,g,b] rgb.map!{|c| c / 255.0} min_rgb = rgb.min max_rgb = rgb.max delta = max_rgb - min_rgb lightness = (max_rgb + min_rgb) / 2.0 if delta < 1e-5 hue = 0 saturation = 0 else saturation = if ( lightness < 0.5 ) delta / ( max_rgb + min_rgb ) else delta / ( 2 - max_rgb - min_rgb ) end deltas = rgb.map{|c| (((max_rgb - c) / 6.0) + (delta / 2.0)) / delta} hue = if (rgb[0] - max_rgb).abs < 1e-5 deltas[2] - deltas[1] elsif (rgb[1] - max_rgb).abs < 1e-5 ( 1.0 / 3.0 ) + deltas[0] - deltas[2] else ( 2.0 / 3.0 ) + deltas[1] - deltas[0] end hue += 1 if hue < 0 hue -= 1 if hue > 1 end from_fractions(hue, saturation, lightness) end |
Instance Method Details
#to_color ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/compass-colors/hsl.rb', line 59 def to_color m2 = l <= 0.5 ? l * (s + 1) : l + s - l * s m1 = l * 2 - m2 Sass::Script::Color.new([hue_to_rgb(m1, m2, hp + 1.0/3), hue_to_rgb(m1, m2, hp), hue_to_rgb(m1, m2, hp - 1.0/3)].map { |c| (c * 0xff).round }) end |