Class: Cura::Color

Inherits:
Object
  • Object
show all
Includes:
Attributes::HasAttributes, Attributes::HasInitialize
Defined in:
lib/cura/color.rb

Overview

Colors.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Attributes::HasAttributes

included, #update_attributes

Constructor Details

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

Returns a new instance of Color.



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cura/color.rb', line 45

def initialize(r=0, g=0, b=0, a=255)
  if r.respond_to?(:to_h)
    super(r.to_h)
  else
    @red   = r
    @green = g
    @blue  = b
    @alpha = a
  end
  
  @lab = rgb_to_lab([@red, @green, @blue]) # TODO: Update on rgb setters?
end

Instance Attribute Details

#labObject (readonly)

Returns the value of attribute lab.



106
107
108
# File 'lib/cura/color.rb', line 106

def lab
  @lab
end

Class Method Details

.blackObject



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

def black
  new
end

.blueObject



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

def blue
  new(0, 0, 255)
end

.defaultObject

The default color to be overidden by adapters. Usually, for TUI’s to use the terminal theme’s colors. TODO: Remove.



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

def default
  super
end

.greenObject



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

def green
  new(0, 255, 0)
end

.redObject



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

def red
  new(255, 0, 0)
end

.whiteObject



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

def white
  new(255, 255, 255)
end

Instance Method Details

#-(other) ⇒ Object



108
109
110
# File 'lib/cura/color.rb', line 108

def -(other)
  delta_e_2000(@lab, other.lab)
end

#<=>(other) ⇒ Object



112
113
114
# File 'lib/cura/color.rb', line 112

def <=>(other)
  self.hsl[0] <=> other.hsl[0]
end

#==(other) ⇒ Boolean

Determing if this color is equivalent to another object.

Parameters:

  • other (Object)

Returns:

  • (Boolean)


120
121
122
# File 'lib/cura/color.rb', line 120

def ==(other)
  other.is_a?(Color) ? matches_color?(other) : super
end

#alphaInteger

Get the alpha channel of this color.

Returns:

  • (Integer)


# File 'lib/cura/color.rb', line 91

#alpha=(value) ⇒ Integer

Set the alpha channel of this color.

Parameters:

  • value (#to_i)

Returns:

  • (Integer)


102
103
104
# File 'lib/cura/color.rb', line 102

[:red, :green, :blue, :alpha].each do |channel|
  attribute(channel) { |value| convert_and_constrain_value(value) }
end

#blue=(value) ⇒ Integer

Set the blue channel of this color.

Parameters:

  • value (#to_i)

Returns:

  • (Integer)


# File 'lib/cura/color.rb', line 80

#greenInteger

Get the green channel of this color.

Returns:

  • (Integer)


# File 'lib/cura/color.rb', line 69

#green=(value) ⇒ Integer

Set the green channel of this color.

Parameters:

  • value (#to_i)

Returns:

  • (Integer)


# File 'lib/cura/color.rb', line 74

#hexObject



136
137
138
# File 'lib/cura/color.rb', line 136

def hex
  to_a.each_with_object("") { |part, memo| memo << "%02x" % part }
end

#hslObject



124
125
126
# File 'lib/cura/color.rb', line 124

def hsl
  @hsl ||= rgb_to_hsl(@rgb)
end

#redInteger

Get the red channel of this color.

Returns:

  • (Integer)


# File 'lib/cura/color.rb', line 58

#red=(value) ⇒ Integer

Set the red channel of this color.

Parameters:

  • value (#to_i)

Returns:

  • (Integer)


# File 'lib/cura/color.rb', line 63

#to_aObject



132
133
134
# File 'lib/cura/color.rb', line 132

def to_a
  [@red, @green, @blue, @alpha]
end

#yiqObject



128
129
130
# File 'lib/cura/color.rb', line 128

def yiq
  @yiq ||= rgb_to_yiq(@rgb)
end