Class: Colorist::Color

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

Overview

Color is the general class for storing and manipulating a color with the Colorist gem. It provides methods to add, subtract, and calculate aspects of the color based on W3C and other standards.

Constant Summary collapse

CSS_COLOR_NAMES =
{  "maroon"  => 0x800000,
"red"     => 0xff0000,
"orange"  => 0xffa500,
"yellow"  => 0xffff00,
"olive"   => 0x808000,
"purple"  => 0x800080,
"fuchsia" => 0xff00ff,
"white"   => 0xffffff,
"lime"    => 0x00ff00,
"green"   => 0x008000,
"navy"    => 0x000080,
"blue"    => 0x0000ff,
"aqua"    => 0x00ffff,
"teal"    => 0x008080,
"black"   => 0x000000,
"silver"  => 0xc0c0c0,
"gray"    => 0x808080  }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(color = 0x000000) ⇒ Color

Creates a new color with the hex color provided as a number (i.e. 0x112233)



27
28
29
30
31
32
# File 'lib/colorist/color.rb', line 27

def initialize(color=0x000000)
  string = "%.6x" % color
  @r = string[0..1].hex
  @g = string[2..3].hex
  @b = string[4..5].hex
end

Instance Attribute Details

#bObject

Returns the value of attribute b.



6
7
8
# File 'lib/colorist/color.rb', line 6

def b
  @b
end

#gObject

Returns the value of attribute g.



6
7
8
# File 'lib/colorist/color.rb', line 6

def g
  @g
end

#rObject

Returns the value of attribute r.



6
7
8
# File 'lib/colorist/color.rb', line 6

def r
  @r
end

Class Method Details

.from(some_entity) ⇒ Object

Create a new color from the provided object. Duplicates Color objects and attempts to call to_color on other objects. Will raise an ArgumentError if it is unable to coerce the color.



69
70
71
72
73
74
75
76
77
# File 'lib/colorist/color.rb', line 69

def self.from(some_entity)
  case some_entity
    when Colorist::Color
      some_entity.dup
    else
      raise ArgumentError, "#{some_entity.class.to_s} cannot be coerced into a color.", caller unless some_entity.respond_to?(:to_color)
      some_entity.to_color
  end
end

.from_rgb(r, g, b, options = {}) ⇒ Object

Initialize a color based on RGB values. By default, the values should be between 0 and 255. If you use the option :percent => true, the values should then be between 0.0 and 1.0.



37
38
39
40
41
42
43
44
45
# File 'lib/colorist/color.rb', line 37

def self.from_rgb(r,g,b,options={})
  color = Colorist::Color.new
  # convert from 0.0 to 1.0 to 0 to 255 if the :percent option is used
  if options[:percent]
    color.r, color.g, color.b = r * 255, g * 255, b * 255
  end
  color.r, color.g, color.b = r, g, b
  color
end

.from_string(some_string) ⇒ Object

Converts a CSS hex string into a color. Works both with the full form (i.e. #ffffff) and the abbreviated form (#fff). Can also take any of the 16 named CSS colors.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/colorist/color.rb', line 50

def self.from_string(some_string)
  if matched = some_string.match(/\A#([0-9a-f]{3})\z/i)
    color = Colorist::Color.from_rgb(*matched[1].split(//).collect{|v| "#{v}#{v}".hex })
  elsif matched = some_string.match(/\A#([0-9a-f]{6})\z/i)
    color = Colorist::Color.new
    color.r = matched[1][0..1].hex
    color.g = matched[1][2..3].hex
    color.b = matched[1][4..5].hex
  elsif CSS_COLOR_NAMES.key?(some_string)
    color = Colorist::Color.new(CSS_COLOR_NAMES[some_string])
  else
    raise ArgumentError, "Must provide a valid CSS hex color or color name.", caller
  end
  color
end

Instance Method Details

#+(other_color) ⇒ Object

Add the individual RGB values of two colors together. You may also use an equivalent numeric or string color representation.

Examples:

gray = Colorist::Color.new(0x333333)
gray + "#300"   # => <Color #663333>
gray + 0x000000 # => <Color #333333>
white = "white".to_color
gray + white    # => <Color #ffffff>


94
95
96
97
98
99
100
101
# File 'lib/colorist/color.rb', line 94

def +(other_color)
  other_color = Colorist::Color.from(other_color)
  color = self.dup
  color.r += other_color.r
  color.g += other_color.g
  color.b += other_color.b
  color
end

#-(other_color) ⇒ Object

Subtract the individual RGB values of the two colors together. You may also use an equivalent numeric or string color representation.



105
106
107
108
109
110
111
112
# File 'lib/colorist/color.rb', line 105

def -(other_color)
  other_color = Colorist::Color.from(other_color)
  color = self.dup
  color.r -= other_color.r
  color.g -= other_color.g
  color.b -= other_color.b
  color
end

#<(other_color) ⇒ Object

Compares colors based on brightness.



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

def < (other_color)
  other_color = Colorist::Color.from(other_color)
  brightness < other_color.brightness
end

#<=>(other_color) ⇒ Object

Compares colors based on brightness.



115
116
117
118
# File 'lib/colorist/color.rb', line 115

def <=>(other_color)
  other_color = Colorist::Color.from(other_color)
  brightness <=> other_color.brightness
end

#==(other_color) ⇒ Object

Equal if the red, green, and blue values are identical.



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

def ==(other_color)
  other_color = Colorist::Color.from(other_color)
  other_color.r == self.r && other_color.g == self.g && other_color.b == self.b
end

#===(other_color) ⇒ Object

Equal if the brightnesses of the two colors are identical.



139
140
141
142
# File 'lib/colorist/color.rb', line 139

def ===(other_color)
  other_color = Colorist::Color.from(other_color)
  other_color.brightness == brightness
end

#>(other_color) ⇒ Object

Compares colors based on brightness.



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

def > (other_color)
  other_color = Colorist::Color.from(other_color)
  brightness > other_color.brightness
end

#brightness(formula = :w3c) ⇒ Object

Returns the perceived brightness of the provided color on a scale of 0.0 to 1.0 based on the formula provided. The formulas available are:

  • :w3c - ((r * 299 + g * 587 + b * 114) / 1000 / 255

  • :standard - sqrt(0.241 * r^2 + 0.691 * g^2 + 0.068 * b^2) / 255



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

def brightness(formula=:w3c)
  case formula
    when :standard
      Math.sqrt(0.241 * r**2 + 0.691 * g**2 + 0.068 * b**2) / 255
    when :w3c
      ((r * 299 + g * 587 + b * 114) / 255000.0)
  end
end

#contrast_with(other_color, formula = :w3c) ⇒ Object

Contrast this color with another color using the provided formula. The available formulas are:

  • :w3c - (max(r1 r2) - min(r1 r2)) + (max(g1 g2) - min(g1 g2)) + (max(b1 b2) - min(b1 b2))



191
192
193
194
195
196
197
198
199
# File 'lib/colorist/color.rb', line 191

def contrast_with(other_color, formula=:w3c)
  other_color = Color.from(other_color)
  case formula
    when :w3c
      (([self.r, other_color.r].max - [self.r, other_color.r].min) +
      ([self.g, other_color.g].max - [self.g, other_color.g].min) +
      ([self.b, other_color.b].max - [self.b, other_color.b].min)) / 765.0
  end
end

#dupObject

Create a duplicate of this color.



80
81
82
# File 'lib/colorist/color.rb', line 80

def dup
  Colorist::Color.from_rgb(@r,@g,@b)
end

#inspectObject



168
169
170
# File 'lib/colorist/color.rb', line 168

def inspect
  "#<Color #{to_s(:css)}>"
end

#invertObject

Returns the opposite of the current color.



202
203
204
# File 'lib/colorist/color.rb', line 202

def invert
  Color.from_rgb(255 - r, 255 - g, 255 - b)
end

#text_color(threshold = 0.6, formula = :standard) ⇒ Object

Returns an appropriate text color (either black or white) based on the brightness of this color. The threshold specifies the brightness cutoff point.



217
218
219
# File 'lib/colorist/color.rb', line 217

def text_color(threshold=0.6, formula=:standard)
  brightness(formula) > threshold ? Colorist::Color.new(0x000000) : Colorist::Color.new(0xffffff)
end

#to_grayscale(formula = :w3c) ⇒ Object

Converts the current color to grayscale using the brightness formula provided. See #brightness for a description of the available formulas.



209
210
211
212
# File 'lib/colorist/color.rb', line 209

def to_grayscale(formula=:w3c)
  b = brightness(formula)
  Color.from_rgb(255 * b, 255 * b, 255 * b)
end

#to_s(format = :css) ⇒ Object

Outputs a string representation of the color in the desired format. The available formats are:

  • :css - As a CSS hex string (i.e. #ffffff) (default)

  • :css_rgb - As a CSS RGB value string (i.e. rgb(255,255,255))

  • :rgb - As an RGB triplet (i.e. 1.0, 1.0, 1.0)



157
158
159
160
161
162
163
164
165
166
# File 'lib/colorist/color.rb', line 157

def to_s(format=:css)
  case format
    when :css
      "#%.2x%.2x%.2x" % [r, g, b]
    when :css_rgb
      "rgb(%.2f,%.2f,%.2f)" % [r, g, b]
    when :rgb
      "%.3f, %.3f, %.3f" % [r / 255, g / 255, b / 255]
  end
end