Class: RubyXL::ColorConvenienceClasses::RgbColor

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyXL/convenience_methods/color.rb

Overview

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#aObject

Returns the value of attribute a.



20
21
22
# File 'lib/rubyXL/convenience_methods/color.rb', line 20

def a
  @a
end

#bObject

Returns the value of attribute b.



20
21
22
# File 'lib/rubyXL/convenience_methods/color.rb', line 20

def b
  @b
end

#gObject

Returns the value of attribute g.



20
21
22
# File 'lib/rubyXL/convenience_methods/color.rb', line 20

def g
  @g
end

#rObject

Returns the value of attribute r.



20
21
22
# File 'lib/rubyXL/convenience_methods/color.rb', line 20

def r
  @r
end

Class Method Details

.parse(str) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rubyXL/convenience_methods/color.rb', line 60

def self.parse(str)
  r, g, b, a = str.unpack('A2A2A2A2')

  rgb_color = RgbColor.new
  rgb_color.r = r && r.to_i(16)
  rgb_color.g = g && g.to_i(16)
  rgb_color.b = b && b.to_i(16)
  rgb_color.a = a && a.to_i(16)

  rgb_color
end

Instance Method Details

#to_hlsObject



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
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rubyXL/convenience_methods/color.rb', line 22

def to_hls
  hls_color = HlsColor.new

  # Note that we are overriding accessors with local vars here:
  r = self.r / 255.0
  g = self.g / 255.0
  b = self.b / 255.0

  hls_color.a = (self.a || 0) / 255.0

  min = [r, g, b].min
  max = [r, g, b].max
  delta = max - min

  if (max == min) then
    hls_color.h = hls_color.s = 0
    hls_color.l = max
    return hls_color
  end

  hls_color.l = (min + max) / 2

  if (hls_color.l < 0.5) then
    hls_color.s = delta / (max + min);
  else
    hls_color.s = delta / (2.0 - max - min);
  end

  hls_color.h = (g - b) / delta       if (r == max)
  hls_color.h = 2.0 + ((b - r) / delta) if (g == max)
  hls_color.h = 4.0 + ((r - g) / delta) if (b == max)

  hls_color.h *= 60;
  hls_color.h += 360 if hls_color.h < 0

  hls_color
end

#to_sObject



72
73
74
75
76
77
78
# File 'lib/rubyXL/convenience_methods/color.rb', line 72

def to_s
  if a && a != 0 then
    format('%02x%02x%02x%02x', r, g, b, a)
  else
    format('%02x%02x%02x', r, g, b)
  end
end