Module: OoxmlParser::ColorHelper

Included in:
Color
Defined in:
lib/ooxml_parser/common_parser/common_data/color/color_helper.rb

Overview

Helper methods for color

Constant Summary collapse

AUTO_STRING_VALUE =

Returns value for auto string.

Returns:

  • (String)

    value for auto string

'auto'

Instance Method Summary collapse

Instance Method Details

#parse_hex_string(hex_string) ⇒ Object

Parse string in hex

Parameters:

  • hex_string (String)

    with or without alpha-channel



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ooxml_parser/common_parser/common_data/color/color_helper.rb', line 11

def parse_hex_string(hex_string)
  return self if hex_string == AUTO_STRING_VALUE

  case hex_string.length
  when 3
    @red, @green, @blue = hex_string.chars.map(&:hex)
  when 6
    @red, @green, @blue = hex_string.unpack('A2A2A2').map(&:hex)
  when 8
    @alpha_channel, @red, @green, @blue = hex_string.unpack('A2A2A2A2').map(&:hex)
  end
  self
end

#to_hslHSLColor

Convert color to HSLColor

Returns:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ooxml_parser/common_parser/common_data/color/color_helper.rb', line 27

def to_hsl
  hls_color = HSLColor.new

  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.alpha_channel = 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