Class: ColorFun::HSL
- Inherits:
-
Object
- Object
- ColorFun::HSL
- Defined in:
- lib/color_fun/hsl.rb
Instance Attribute Summary collapse
-
#hue ⇒ Object
Returns the value of attribute hue.
-
#lightness ⇒ Object
Returns the value of attribute lightness.
-
#saturation ⇒ Object
Returns the value of attribute saturation.
Class Method Summary collapse
-
.from_rbg(r, g, b) ⇒ Object
Create a new HSL object based on the RGB values.
Instance Method Summary collapse
-
#initialize(h, s, l) ⇒ HSL
constructor
A new instance of HSL.
-
#name ⇒ Object
Return a name for this colour.
Constructor Details
#initialize(h, s, l) ⇒ HSL
Returns a new instance of HSL.
6 7 8 |
# File 'lib/color_fun/hsl.rb', line 6 def initialize(h, s, l) @hue, @saturation, @lightness = h, s, l end |
Instance Attribute Details
#hue ⇒ Object
Returns the value of attribute hue.
4 5 6 |
# File 'lib/color_fun/hsl.rb', line 4 def hue @hue end |
#lightness ⇒ Object
Returns the value of attribute lightness.
4 5 6 |
# File 'lib/color_fun/hsl.rb', line 4 def lightness @lightness end |
#saturation ⇒ Object
Returns the value of attribute saturation.
4 5 6 |
# File 'lib/color_fun/hsl.rb', line 4 def saturation @saturation end |
Class Method Details
.from_rbg(r, g, b) ⇒ Object
Create a new HSL object based on the RGB values
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/color_fun/hsl.rb', line 40 def self.from_rbg(r,g,b) colors = [r / 255.0, g / 255.0, b / 255.0] max = colors.max min = colors.min d = max - min v = max * 100 s = max != 0.0 ? d / max *100 : 0.0 if s == 0.0 h = 0.0 else case max when colors[0] h = (colors[1] - colors[2]) / d when colors[1] h = 2 + (colors[2] - colors[0]) / d when colors[2] h = 4 + (colors[0] - colors[1]) / d end h *= 60.0 h += 360.0 if (h < 0) end self.new(h, s, v) end |
Instance Method Details
#name ⇒ Object
Return a name for this colour
11 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/color_fun/hsl.rb', line 11 def name Array.new.tap do |s| if @saturation == 0 && @lightness == 0 s << "Black" elsif @saturation == 0 && @lightness == 100 s << "White" elsif @saturation <= 15 && @saturation > 0 s << "Light" elsif @lightness < 40 && @saturation > 0 s << "Dark" end if @saturation > 0 s << case @hue when 0..8, 347..360 then 'Red' when 9..42 then 'Orange' when 43..61 then 'Yellow' when 62..158 then 'Green' when 159..264 then 'Blue' when 265..285 then 'Purple' when 286..346 then 'Pink' end else s << "Grey" end end.join(' ') end |