Class: Falu::Colors::RGB

Inherits:
Base
  • Object
show all
Defined in:
lib/falu/colors/rgb.rb

Instance Attribute Summary

Attributes inherited from Base

#color

Instance Method Summary collapse

Methods inherited from Base

#as_json

Constructor Details

#initialize(*color) ⇒ RGB

Returns a new instance of RGB.



6
7
8
9
10
# File 'lib/falu/colors/rgb.rb', line 6

def initialize(*color)
  color = color[0] if color.length == 1
  raise "Invalid RGB Color: #{color.to_s}" unless Falu::Color.rgb?(color)
  @color = color.is_a?(Array) ? color.join(',') : color
end

Instance Method Details

#absoluteObject



28
29
30
# File 'lib/falu/colors/rgb.rb', line 28

def absolute
  colors.sum
end

#blueObject



24
25
26
# File 'lib/falu/colors/rgb.rb', line 24

def blue
  colors[2]
end

#chromaObject



36
37
38
# File 'lib/falu/colors/rgb.rb', line 36

def chroma
  @chroma ||= (inverse.max - inverse.min)
end

#colorsObject



12
13
14
# File 'lib/falu/colors/rgb.rb', line 12

def colors
  @colors ||= color.to_s.split(',').map { |c| c.strip.to_i }
end

#greenObject



20
21
22
# File 'lib/falu/colors/rgb.rb', line 20

def green
  colors[1]
end

#hueObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/falu/colors/rgb.rb', line 58

def hue
  @hue ||= begin
    if inverse.max == inverse.min
      0
    else
      rd,gr,bl = *inverse

      if rd == inverse.max
        hue = ((gr - bl) / chroma % 6) * 60
      elsif gr == inverse.max
        hue = ((bl - rd) / chroma + 2) * 60
      elsif bl == inverse.max
        hue = ((rd - gr) / chroma + 4) * 60
      end
    end
  end
end

#inverseObject



32
33
34
# File 'lib/falu/colors/rgb.rb', line 32

def inverse
  @inverse ||= colors.map { |c| c / 255.0 }
end

#lightnessObject



40
41
42
# File 'lib/falu/colors/rgb.rb', line 40

def lightness
  @lightness ||= ((inverse.max + inverse.min) / 2)
end

#redObject



16
17
18
# File 'lib/falu/colors/rgb.rb', line 16

def red
  colors[0]
end

#saturationObject



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/falu/colors/rgb.rb', line 44

def saturation
  @saturation ||= begin
    if inverse.max == inverse.min
      0
    else
      if lightness < 0.5
        (chroma / (inverse.max + inverse.min))
      else
        (chroma / (2 - inverse.max - inverse.min))
      end
    end
  end
end

#to_aObject



80
81
82
# File 'lib/falu/colors/rgb.rb', line 80

def to_a
  colors
end

#to_hObject



84
85
86
# File 'lib/falu/colors/rgb.rb', line 84

def to_h
  {red: red, green: green, blue: blue}
end

#to_hexObject



92
93
94
# File 'lib/falu/colors/rgb.rb', line 92

def to_hex
  Falu::Colors::HEX.new("##{colors.map { |c| c.to_s(16).rjust(2, '0') }.join}")
end

#to_hslObject



96
97
98
# File 'lib/falu/colors/rgb.rb', line 96

def to_hsl
  Falu::Colors::HSL.new([hue, (saturation * 100), (lightness * 100)])
end

#to_rgbObject



88
89
90
# File 'lib/falu/colors/rgb.rb', line 88

def to_rgb
  self
end

#to_sObject



76
77
78
# File 'lib/falu/colors/rgb.rb', line 76

def to_s
  colors.map { |c| c.round.to_s.rjust(3, '0') }.join(',')
end