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.



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

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.



104
105
106
# File 'lib/cura/color.rb', line 104

def lab
  @lab
end

Class Method Details

.blackObject



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

def black
  new
end

.blueObject



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

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.



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

def default
  super
end

.greenObject



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

def green
  new(0, 255, 0)
end

.redObject



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

def red
  new(255, 0, 0)
end

.whiteObject



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

def white
  new(255, 255, 255)
end

Instance Method Details

#-(other) ⇒ Object



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

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

#<=>(other) ⇒ Object



110
111
112
# File 'lib/cura/color.rb', line 110

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

#==(other) ⇒ Boolean

Determing if this color is equivalent to another object.

Parameters:

  • other (Object)

Returns:

  • (Boolean)


118
119
120
# File 'lib/cura/color.rb', line 118

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 89

#alpha=(value) ⇒ Integer

Set the alpha channel of this color.

Parameters:

  • value (#to_i)

Returns:

  • (Integer)


100
101
102
# File 'lib/cura/color.rb', line 100

[: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 78

#greenInteger

Get the green channel of this color.

Returns:

  • (Integer)


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

#green=(value) ⇒ Integer

Set the green channel of this color.

Parameters:

  • value (#to_i)

Returns:

  • (Integer)


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

#hexObject



134
135
136
# File 'lib/cura/color.rb', line 134

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

#hslObject



122
123
124
# File 'lib/cura/color.rb', line 122

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

#redInteger

Get the red channel of this color.

Returns:

  • (Integer)


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

#red=(value) ⇒ Integer

Set the red channel of this color.

Parameters:

  • value (#to_i)

Returns:

  • (Integer)


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

#to_aObject



130
131
132
# File 'lib/cura/color.rb', line 130

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

#yiqObject



126
127
128
# File 'lib/cura/color.rb', line 126

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