Class: OoxmlParser::HSLColor

Inherits:
Object
  • Object
show all
Defined in:
lib/ooxml_parser/common_parser/common_data/colors/hsl_color.rb

Overview

HSL is one of the most common cylindrical-coordinate representations of points in an RGB color model. HSL stands for hue, saturation, and lightness, and is often also called HLS.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hue = 0, saturation = 0, lightness = 0, alpha_channel = 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”.



15
16
17
18
19
20
# File 'lib/ooxml_parser/common_parser/common_data/colors/hsl_color.rb', line 15

def initialize(hue = 0, saturation = 0, lightness = 0, alpha_channel = nil)
  @alpha_channel = alpha_channel
  @h = hue
  @s = saturation
  @l = lightness
end

Instance Attribute Details

#alpha_channelInteger

Returns alpha channel value.

Returns:

  • (Integer)

    alpha channel value



9
10
11
# File 'lib/ooxml_parser/common_parser/common_data/colors/hsl_color.rb', line 9

def alpha_channel
  @alpha_channel
end

#hObject

Returns the value of attribute h.



7
8
9
# File 'lib/ooxml_parser/common_parser/common_data/colors/hsl_color.rb', line 7

def h
  @h
end

#lObject

Returns the value of attribute l.



7
8
9
# File 'lib/ooxml_parser/common_parser/common_data/colors/hsl_color.rb', line 7

def l
  @l
end

#sObject

Returns the value of attribute s.



7
8
9
# File 'lib/ooxml_parser/common_parser/common_data/colors/hsl_color.rb', line 7

def s
  @s
end

Instance Method Details

#calculate_lum_value(tint, lum) ⇒ Integer

Get lum value of color

Parameters:

  • tint (Integer)

    tint to apply

  • lum (Integer)

    lum without tint

Returns:

  • (Integer)

    result



51
52
53
54
55
56
57
# File 'lib/ooxml_parser/common_parser/common_data/colors/hsl_color.rb', line 51

def calculate_lum_value(tint, lum)
  if tint.nil?
    lum
  else
    tint.negative? ? lum * (1.0 + tint) : (lum * (1.0 - tint)) + (255 - (255 * (1.0 - tint)))
  end
end

#calculate_rgb_with_tint(tint) ⇒ Color

Convert to rgb with applied tint

Parameters:

  • tint (Integer)

    tint to apply

Returns:

  • (Color)

    result of covert



62
63
64
65
# File 'lib/ooxml_parser/common_parser/common_data/colors/hsl_color.rb', line 62

def calculate_rgb_with_tint(tint)
  self.l = calculate_lum_value(tint, @l * 255.0) / 255.0
  to_rgb
end

#to_rgbColor

Chroma - The “colorfulness relative to the brightness of a similarly illuminated white”.

Returns:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ooxml_parser/common_parser/common_data/colors/hsl_color.rb', line 24

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.zero?
          Color.new(0, 0, 0)
        elsif @h.positive? && @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