Class: Trestle::Color

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(r, g, b) ⇒ Color

Returns a new instance of Color.



5
6
7
# File 'lib/trestle/color.rb', line 5

def initialize(r, g, b)
  @r, @g, @b = r, g, b
end

Instance Attribute Details

#bObject (readonly)

Returns the value of attribute b.



3
4
5
# File 'lib/trestle/color.rb', line 3

def b
  @b
end

#gObject (readonly)

Returns the value of attribute g.



3
4
5
# File 'lib/trestle/color.rb', line 3

def g
  @g
end

#rObject (readonly)

Returns the value of attribute r.



3
4
5
# File 'lib/trestle/color.rb', line 3

def r
  @r
end

Class Method Details

.hue_to_rgb(p, q, t) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/trestle/color.rb', line 98

def self.hue_to_rgb(p, q, t)
  t += 1 if t < 0
  t -= 1 if t > 1

  if t < 1 / 6.0
    p + (q - p) * 6 * t
  elsif t < 1 / 2.0
    q
  elsif t < 2 / 3.0
    p + (q - p) * (2 / 3.0 - t) * 6
  else
    p
  end
end

.parse(str) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/trestle/color.rb', line 49

def self.parse(str)
  if str.starts_with?("#") && str.length.in?([4, 7])
    parse_hex(str)
  elsif str.starts_with?("rgb")
    parse_rgb(str)
  elsif str.starts_with?("hsl")
    parse_hsl(str)
  else
    raise ArgumentError, "Could not parse color code: #{str}"
  end
end

.parse_hex(str) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/trestle/color.rb', line 61

def self.parse_hex(str)
  str = str.sub(/^#/, "")

  if str.length == 3
    str = str[0] * 2 + str[1] * 2 + str[2] * 2
  end

  new(str[0, 2].hex, str[2, 2].hex, str[4, 2].hex)
end

.parse_hsl(str) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/trestle/color.rb', line 76

def self.parse_hsl(str)
  match = str.match(/^hsl\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/)
  h, s, l = match[1].to_f, match[2].to_f, match[3].to_f

  h /= 360
  s /= 100
  l /= 100

  if s == 0
    r = g = b = l.to_f
  else
    q = l < 0.5 ? l * (1 + s) : l + s - l * s
    p = 2 * l - q

    r = hue_to_rgb(p, q, h + 1/3.0)
    g = hue_to_rgb(p, q, h)
    b = hue_to_rgb(p, q, h - 1/3.0)
  end

  new((r * 255).round, (g * 255).round, (b * 255).round)
end

.parse_rgb(str) ⇒ Object



71
72
73
74
# File 'lib/trestle/color.rb', line 71

def self.parse_rgb(str)
  match = str.match(/^rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/)
  new(match[1].to_i, match[2].to_i, match[3].to_i)
end

Instance Method Details

#hexObject



9
10
11
# File 'lib/trestle/color.rb', line 9

def hex
  "##{[r, g, b].map { |i| i < 16 ? "0#{i.to_s(16)}" : i.to_s(16) }.join}"
end

#hslObject



17
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
47
# File 'lib/trestle/color.rb', line 17

def hsl
  r = @r / 255.0
  g = @g / 255.0
  b = @b / 255.0

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

  h = s = l = (max + min) / 2.0

  if max == min
    h = 0
    s = 0
  else
    d = max - min
    s = l >= 0.5 ? d / (2 - max - min) : d / (max + min)

    case max
    when r
      h = (g - b) / d + (g < b ? 6 : 0)
    when g
      h = (b - r) / d + 2
    when b
      h = (r - g) / d + 4
    end

    h /= 6.0
  end

  [(h*360).round, (s*100).round, (l*100).round]
end

#rgbObject



13
14
15
# File 'lib/trestle/color.rb', line 13

def rgb
  [r, g, b]
end