Class: ASE::Color::RGB

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(r = 0, g = 0, b = 0) ⇒ RGB

Returns a new instance of RGB.



23
24
25
26
27
# File 'lib/ase/color_modes/rgb.rb', line 23

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

Instance Attribute Details

#bObject

Returns the value of attribute b.



6
7
8
# File 'lib/ase/color_modes/rgb.rb', line 6

def b
  @b
end

#gObject

Returns the value of attribute g.



6
7
8
# File 'lib/ase/color_modes/rgb.rb', line 6

def g
  @g
end

#rObject

Returns the value of attribute r.



6
7
8
# File 'lib/ase/color_modes/rgb.rb', line 6

def r
  @r
end

Class Method Details

.from_hex(hex) ⇒ Object



15
16
17
18
19
20
# File 'lib/ase/color_modes/rgb.rb', line 15

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

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



9
10
11
12
# File 'lib/ase/color_modes/rgb.rb', line 9

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

Instance Method Details

#[](i) ⇒ Object



52
53
54
# File 'lib/ase/color_modes/rgb.rb', line 52

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

#read!(file) ⇒ Object



29
30
31
32
33
# File 'lib/ase/color_modes/rgb.rb', line 29

def read!(file)
  @r, @g, @b = 3.times.map do |c|
    (file.read(4).unpack('g')[0] * 255).to_i
  end
end

#to_aObject



44
45
46
# File 'lib/ase/color_modes/rgb.rb', line 44

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

#to_cssObject



48
49
50
# File 'lib/ase/color_modes/rgb.rb', line 48

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

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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ase/color_modes/rgb.rb', line 56

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

  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



40
41
42
# File 'lib/ase/color_modes/rgb.rb', line 40

def to_rgb
  self
end

#write!(file) ⇒ Object



35
36
37
38
# File 'lib/ase/color_modes/rgb.rb', line 35

def write!(file)
  file.write 'RGB '
  to_a.each { |c| file.write [c.to_f / 255].pack('g') }
end