Class: OoxmlParser::HSLColor
- Inherits:
-
Object
- Object
- OoxmlParser::HSLColor
- Defined in:
- lib/ooxml_parser/common_parser/common_data/colors/hsl_color.rb
Instance Attribute Summary collapse
-
#a ⇒ Object
Returns the value of attribute a.
-
#h ⇒ Object
Returns the value of attribute h.
-
#l ⇒ Object
Returns the value of attribute l.
-
#s ⇒ Object
Returns the value of attribute s.
Class Method Summary collapse
Instance Method Summary collapse
- #calculate_lum_value(tint, lum) ⇒ Object
- #calculate_rgb_with_tint(tint) ⇒ Object
-
#initialize(hue = 0, saturation = 0, lightness = 0, a = nil) ⇒ HSLColor
constructor
Hue - The “attribute of a visual sensation according to which an area appears to be similar to one of the perceived colors: red, yellow, green, and blue, or to a combination of two of them”.
- #set_color(t1, t2, t3) ⇒ Object
-
#to_rgb ⇒ Object
Chroma - The “colorfulness relative to the brightness of a similarly illuminated white”.
Constructor Details
#initialize(hue = 0, saturation = 0, lightness = 0, a = nil) ⇒ HSLColor
Hue - The “attribute of a visual sensation according to which an area appears to be similar to one of the perceived colors: red, yellow, green, and blue, or to a combination of two of them”. Saturation - The “colorfulness of a stimulus relative to its own brightness”. Lightness - The “brightness relative to the brightness of a similarly illuminated white”.
11 12 13 14 15 16 |
# File 'lib/ooxml_parser/common_parser/common_data/colors/hsl_color.rb', line 11 def initialize(hue = 0, saturation = 0, lightness = 0, a = nil) @a = a @h = hue @s = saturation @l = lightness end |
Instance Attribute Details
#a ⇒ Object
Returns the value of attribute a.
5 6 7 |
# File 'lib/ooxml_parser/common_parser/common_data/colors/hsl_color.rb', line 5 def a @a end |
#h ⇒ Object
Returns the value of attribute h.
5 6 7 |
# File 'lib/ooxml_parser/common_parser/common_data/colors/hsl_color.rb', line 5 def h @h end |
#l ⇒ Object
Returns the value of attribute l.
5 6 7 |
# File 'lib/ooxml_parser/common_parser/common_data/colors/hsl_color.rb', line 5 def l @l end |
#s ⇒ Object
Returns the value of attribute s.
5 6 7 |
# File 'lib/ooxml_parser/common_parser/common_data/colors/hsl_color.rb', line 5 def s @s end |
Class Method Details
.calculate_with_luminance(color, lum_mod, lum_off = nil) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/ooxml_parser/common_parser/common_data/colors/hsl_color.rb', line 94 def self.calculate_with_luminance(color, lum_mod, lum_off = nil) hls_color = color.is_a?(HSLColor) ? color : HSLColor.rgb_to_hsl(color) # for hsl color which have h == 0 need another values of lumOff lumMod - 0.04(+-0.005) if lum_mod if hls_color.h == 0 hls_color.l *= case lum_mod when 0.2 0.169 when 0.4 0.369 when 0.6 0.55 when 0.75 0.705 when 0.5 0.469 else lum_mod end else hls_color.l *= lum_mod end end current_rgb = hls_color.to_rgb return current_rgb if current_rgb == Color.new unless lum_off.nil? hls_color = HSLColor.rgb_to_hsl(current_rgb) # for hsl color which have h == 0 need another values of lumOff - 0.04(+-0.01) if hls_color.h == 0 hls_color.l += case lum_off when 0.8 0.76 when 0.6 0.55 when 0.4 0.36 else lum_off end else hls_color.l += lum_off end current_rgb = hls_color.to_rgb end current_rgb end |
.rgb_to_hsl(rgb_color) ⇒ Object
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 |
# File 'lib/ooxml_parser/common_parser/common_data/colors/hsl_color.rb', line 18 def self.rgb_to_hsl(rgb_color) hls_color = HSLColor.new red = rgb_color.red.to_f # / 255.0 green = rgb_color.green.to_f # / 255.0 blue = rgb_color.blue.to_f # / 255.0 min = [red, green, blue].min.to_f max = [red, green, blue].max.to_f delta = (max - min).to_f hls_color.l = (min + max) / 255.0 / 2.0 hls_color.a = rgb_color.alpha_channel.to_f / 255.0 unless max == min hls_color.s = delta / (255.0 - (255.0 - max - min).abs) hls_color.h = if max == red && green >= blue 60.0 * (green - blue) / delta elsif max == red && green < blue 60.0 * (green - blue) / delta + 360.0 elsif max == green 60.0 * (blue - red) / delta + 120.0 else 60.0 * (red - green) / delta + 240.0 end end hls_color end |
Instance Method Details
#calculate_lum_value(tint, lum) ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/ooxml_parser/common_parser/common_data/colors/hsl_color.rb', line 86 def calculate_lum_value(tint, lum) if tint.nil? lum else tint < 0 ? lum * (1.0 + tint) : lum * (1.0 - tint) + (255 - 255 * (1.0 - tint)) end end |
#calculate_rgb_with_tint(tint) ⇒ Object
142 143 144 145 |
# File 'lib/ooxml_parser/common_parser/common_data/colors/hsl_color.rb', line 142 def calculate_rgb_with_tint(tint) self.l = calculate_lum_value(tint, @l * 255.0) / 255.0 to_rgb end |
#set_color(t1, t2, t3) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/ooxml_parser/common_parser/common_data/colors/hsl_color.rb', line 72 def set_color(t1, t2, t3) t3 += 1.0 if t3 < 0 t3 -= 1.0 if t3 > 1 if 6.0 * t3 < 1 t2 + (t1 - t2) * 6.0 * t3 elsif 2.0 * t3 < 1 t1 elsif 3.0 * t3 < 2 t2 + (t1 - t2) * ((2.0 / 3.0) - t3) * 6.0 else t2 end end |
#to_rgb ⇒ Object
Chroma - The “colorfulness relative to the brightness of a similarly illuminated white”.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/ooxml_parser/common_parser/common_data/colors/hsl_color.rb', line 49 def to_rgb chroma = (1 - (2 * @l - 1).abs) * @s x = chroma * (1 - ((@h / 60.0) % 2.0 - 1).abs) m = @l - chroma / 2.0 rgb = if @h == 0 Color.new(0, 0, 0) elsif @h > 0 && @h < 60 Color.new(chroma, x, 0) elsif @h > 60 && @h < 120 Color.new(x, chroma, 0) elsif @h > 120 && @h < 180 Color.new(0, chroma, x) elsif @h > 180 && @h < 240 Color.new(0, x, chroma) elsif @h > 240 && @h < 300 Color.new(x, 0, chroma) else Color.new(chroma, 0, x) end Color.new(((rgb.red + m) * 255.0).round, ((rgb.green + m) * 255.0).round, ((rgb.blue + m) * 255.0).round) end |