Class: ASE::Color

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(r, g, b, a = 255) ⇒ Color

We always internally store the color as RGBA.



20
21
22
23
24
25
# File 'lib/ase/color.rb', line 20

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

Instance Attribute Details

#aObject

Returns the value of attribute a.



17
18
19
# File 'lib/ase/color.rb', line 17

def a
  @a
end

#bObject

Returns the value of attribute b.



17
18
19
# File 'lib/ase/color.rb', line 17

def b
  @b
end

#gObject

Returns the value of attribute g.



17
18
19
# File 'lib/ase/color.rb', line 17

def g
  @g
end

#rObject

Returns the value of attribute r.



17
18
19
# File 'lib/ase/color.rb', line 17

def r
  @r
end

Class Method Details

.from_hex(hex) ⇒ Object



12
13
14
# File 'lib/ase/color.rb', line 12

def from_hex(hex)
  self.new *hex.gsub('#', '').scan(/../).map { |c| c.to_i(16) }
end

.from_rgba(*args) ⇒ Object Also known as: from_rgb



6
7
8
9
# File 'lib/ase/color.rb', line 6

def from_rgba(*args)
  args = args.first if args.length == 1
  self.new *args
end

Instance Method Details

#[](i) ⇒ Object



39
40
41
# File 'lib/ase/color.rb', line 39

def [](i)
  [@r, @g, @b, @a][i]
end

#to_cssObject



35
36
37
# File 'lib/ase/color.rb', line 35

def to_css
  "rgba(#{r}, #{g}, #{b}, #{a})"
end

#to_hex(incl_hash = true, incl_alpha = false) ⇒ Object Also known as: to_s



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ase/color.rb', line 43

def to_hex(incl_hash=true, incl_alpha=false)
  hex = incl_hash ? '#' : ''
  colors = [@r, @g, @b]
  colors << @a if incl_alpha

  colors.each do |c| 
    color = c.to_s(16)
    if c < 16
      hex << "0#{color}"
    else
      hex << color
    end
  end

  return hex
end

#to_rgbObject



31
32
33
# File 'lib/ase/color.rb', line 31

def to_rgb
  [@r, @g, @b]
end

#to_rgbaObject



27
28
29
# File 'lib/ase/color.rb', line 27

def to_rgba
  [@r, @g, @b, @a]
end