Class: Color

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

Overview

Implements a color (r,g,b + a) with conversion to/from web format (eg #aabbcc), and with a number of utilities to lighten, darken and blend values.

Constant Summary collapse

HEXVAL =

Table for conversion to hex

(('0'..'9').to_a).concat(('A'..'F').to_a).freeze
BRIGHTNESS_DEFAULT =

Default value for #darken, #lighten etc.

0.2
SOLARIZED =
{
  :base03 =>     '#002b36',
  :base02 =>     '#073642',
  :base01 =>     '#586e75',
  :base00 =>     '#657b83',
  :base0 =>      '#839496',
  :base1 =>      '#93a1a1',
  :base2 =>      '#eee8d5',
  :base3 =>      '#fdf6e3',
  :yellow =>     '#b58900',
  :orange =>     '#cb4b16',
  :red =>        '#dc322f',
  :magenta =>    '#d33682',
  :violet =>     '#6c71c4',
  :blue =>       '#268bd2',
  :cyan =>       '#2aa198',
  :green =>      '#859900',
}
WHITE =

Some constants for general use

Color.new(255,255,255).freeze
BLACK =
Color.new(0,0,0).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Color

Constructor. Inits to white (#FFFFFF) by default, or accepts any params supported by #parse.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rbbt/util/color.rb', line 54

def initialize(*args)
  @r = 255
  @g = 255
  @b = 255
  @a = 255

  if args.size.between?(3,4)
    self.r = args[0]
    self.g = args[1]
    self.b = args[2]
    self.a = args[3] if args[3]
  else
    set(*args)
  end
end

Instance Attribute Details

#aObject

Returns the value of attribute a.



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

def a
  @a
end

#bObject

Returns the value of attribute b.



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

def b
  @b
end

#gObject

Returns the value of attribute g.



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

def g
  @g
end

#rObject

Returns the value of attribute r.



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

def r
  @r
end

Class Method Details

.blend(col1, col2, amt) ⇒ Object

Class-level version for explicit blends of two values, useful with constants



227
228
229
230
231
# File 'lib/rbbt/util/color.rb', line 227

def self.blend(col1, col2, amt)
  col1 = Color.parse(col1)
  col2 = Color.parse(col2)
  col1.blend(col2, amt)
end

.parse(*args) ⇒ Object

Attempt to read in a string and parse it into values



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/rbbt/util/color.rb', line 96

def self.parse(*args)
  case args.size

  when 0 then
    return nil

  when 1 then
    val = args[0]

    # Trivial parse... :-)
    return val if val.is_a?(Color)

    # Single value, assume grayscale
    return Color.new(val, val, val) if val.is_a?(Numeric)

    # Assume string
    str = val.to_s.upcase
    str = str[/[0-9A-F]{3,8}/] || ''
    case str.size
    when 3, 4 then
      r, g, b, a = str.scan(/[0-9A-F]/)
    when 6,8 then
      r, g, b, a = str.scan(/[0-9A-F]{2}/)
    else
      return nil
    end

    return Color.new(r,g,b,a || 255)

  when 3,4 then
    return Color.new(*args)

  end
  nil
end

Instance Method Details

#==(val) ⇒ Object

Test for equality, accepts string vals as well, eg Color.new(‘aaa’) == ‘#AAAAAA’ => true



83
84
85
86
87
# File 'lib/rbbt/util/color.rb', line 83

def ==(val)
  val = Color.parse(val)
  return false if val.nil?
  return r == val.r && g == val.g && b == val.b && a == val.a
end

#blend(other, amt) ⇒ Object

Blend to a color amt % towards another color value, eg red.blend(blue, 0.5) will be purple, white.blend(black, 0.5) will be gray, etc.



209
210
211
212
213
214
215
216
217
218
# File 'lib/rbbt/util/color.rb', line 209

def blend(other, amt)
  other = Color.parse(other)
  return Color.new(self) if amt <= 0 || other.nil?
  return Color.new(other) if amt >= 1.0
  val = Color.new(self)
  val.r += ((other.r - val.r)*amt).to_i
  val.g += ((other.g - val.g)*amt).to_i
  val.b += ((other.b - val.b)*amt).to_i
  val
end

#blend!(other, amt) ⇒ Object

In place version of #blend



221
222
223
224
# File 'lib/rbbt/util/color.rb', line 221

def blend!(other, amt)
  set(blend(other, amt))
  self
end

#darken(amt = BRIGHTNESS_DEFAULT) ⇒ Object

Darken a color towards full black. 0.0 is a no-op, 1.0 will return #000000



178
179
180
181
182
183
184
185
186
# File 'lib/rbbt/util/color.rb', line 178

def darken(amt = BRIGHTNESS_DEFAULT)
  return self if amt <= 0
  return BLACK if amt >= 1.0
  val = Color.new(self)
  val.r -= (val.r * amt).to_i
  val.g -= (val.g * amt).to_i
  val.b -= (val.b * amt).to_i
  val
end

#darken!(amt = BRIGHTNESS_DEFAULT) ⇒ Object

In place version of #darken



189
190
191
192
# File 'lib/rbbt/util/color.rb', line 189

def darken!(amt = BRIGHTNESS_DEFAULT)
  set(darken(amt))
  self
end

#grayscaleObject

Convert to grayscale, using perception-based weighting



195
196
197
198
199
# File 'lib/rbbt/util/color.rb', line 195

def grayscale
  val = Color.new(self)
  val.r = val.g = val.b = (0.2126 * val.r + 0.7152 * val.g + 0.0722 * val.b)
  val
end

#grayscale!Object

In place version of #grayscale



202
203
204
205
# File 'lib/rbbt/util/color.rb', line 202

def grayscale!
  set(grayscale)
  self
end

#grayscale?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/rbbt/util/color.rb', line 156

def grayscale?
  @r == @g && @g == @b
end

#inspectObject



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

def inspect
  to_s(true)
end

#lighten(amt = BRIGHTNESS_DEFAULT) ⇒ Object

Lighten color towards white. 0.0 is a no-op, 1.0 will return #FFFFFF



161
162
163
164
165
166
167
168
169
# File 'lib/rbbt/util/color.rb', line 161

def lighten(amt = BRIGHTNESS_DEFAULT)
  return self if amt <= 0
  return WHITE if amt >= 1.0
  val = Color.new(self)
  val.r += ((255-val.r) * amt).to_i
  val.g += ((255-val.g) * amt).to_i
  val.b += ((255-val.b) * amt).to_i
  val
end

#lighten!(amt = BRIGHTNESS_DEFAULT) ⇒ Object

In place version of #lighten



172
173
174
175
# File 'lib/rbbt/util/color.rb', line 172

def lighten!(amt = BRIGHTNESS_DEFAULT)
  set(lighten(amt))
  self
end

#opaque?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/rbbt/util/color.rb', line 148

def opaque?
  @a == 255
end

#set(*args) ⇒ Object

All-purpose setter - pass in another Color, ‘#000000’, rgb vals… whatever



71
72
73
74
75
76
77
78
79
80
# File 'lib/rbbt/util/color.rb', line 71

def set(*args)
  val = Color.parse(*args)
  unless val.nil?
    self.r = val.r
    self.g = val.g
    self.b = val.b
    self.a = val.a
  end
  self
end

#to_rgb(add_hash = true) ⇒ Object



140
141
142
# File 'lib/rbbt/util/color.rb', line 140

def to_rgb(add_hash = true)
  (add_hash ? '#' : '') + to_hex(r) + to_hex(g) + to_hex(b)
end

#to_rgba(add_hash = true) ⇒ Object



144
145
146
# File 'lib/rbbt/util/color.rb', line 144

def to_rgba(add_hash = true)
  to_rgb(add_hash) + to_hex(a)
end

#to_s(add_hash = true) ⇒ Object



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

def to_s(add_hash = true)
  trans? ? to_rgba(add_hash) : to_rgb(add_hash)
end

#trans?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/rbbt/util/color.rb', line 152

def trans?
  @a != 255
end