Class: Gauguin::Color

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(red, green, blue, percentage = 1, transparent = false) ⇒ Color

Returns a new instance of Color.



5
6
7
8
9
10
11
# File 'lib/gauguin/color.rb', line 5

def initialize(red, green, blue, percentage = 1, transparent = false)
  self.red = red
  self.green = green
  self.blue = blue
  self.percentage = percentage
  self.transparent = transparent
end

Instance Attribute Details

#blueObject

Returns the value of attribute blue.



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

def blue
  @blue
end

#greenObject

Returns the value of attribute green.



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

def green
  @green
end

#percentageObject

Returns the value of attribute percentage.



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

def percentage
  @percentage
end

#redObject

Returns the value of attribute red.



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

def red
  @red
end

#transparentObject

Returns the value of attribute transparent.



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

def transparent
  @transparent
end

Class Method Details

.from_a(array) ⇒ Object



50
51
52
53
# File 'lib/gauguin/color.rb', line 50

def self.from_a(array)
  red, green, blue, percentage, transparent = array
  Color.new(red, green, blue, percentage, transparent)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



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

def ==(other)
  self.class == other.class && self.to_key == other.to_key
end

#distance(other_color) ⇒ Object



28
29
30
# File 'lib/gauguin/color.rb', line 28

def distance(other_color)
  (self.to_lab - other_color.to_lab).r
end

#hashObject



19
20
21
# File 'lib/gauguin/color.rb', line 19

def hash
  self.to_key.hash
end

#inspectObject



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

def inspect
  msg = "#{to_s}[#{percentage}]"
  if transparent?
    msg += "[transparent]"
  end
  msg
end

#similar?(other_color) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
# File 'lib/gauguin/color.rb', line 23

def similar?(other_color)
  self.transparent == other_color.transparent &&
    self.distance(other_color) < Gauguin.configuration.color_similarity_threshold
end

#to_aObject



46
47
48
# File 'lib/gauguin/color.rb', line 46

def to_a
  to_rgb + [self.percentage, self.transparent]
end

#to_keyObject



55
56
57
# File 'lib/gauguin/color.rb', line 55

def to_key
  to_rgb + [self.transparent]
end

#to_labObject



32
33
34
35
36
# File 'lib/gauguin/color.rb', line 32

def to_lab
  rgb_vector = self.to_vector
  xyz_vector = rgb_vector.to_xyz
  xyz_vector.to_lab
end

#to_rgbObject



42
43
44
# File 'lib/gauguin/color.rb', line 42

def to_rgb
  [self.red, self.green, self.blue]
end

#to_sObject



59
60
61
# File 'lib/gauguin/color.rb', line 59

def to_s
  "rgb(#{self.red}, #{self.green}, #{self.blue})"
end

#to_vectorObject



38
39
40
# File 'lib/gauguin/color.rb', line 38

def to_vector
  ColorSpace::RgbVector[*to_rgb]
end

#transparent?Boolean

Returns:

  • (Boolean)


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

def transparent?
  self.transparent
end