Skip to content

Instantly share code, notes, and snippets.

@max-power
Last active January 2, 2016 06:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save max-power/8266649 to your computer and use it in GitHub Desktop.
Save max-power/8266649 to your computer and use it in GitHub Desktop.
simple color value object
class Color < Struct.new(:r, :g, :b)
def self.hex(value)
new(*value.to_s.delete('#').scan(/../).map(&:hex))
end
def hex
"%.2X%.2X%.2X" % values
end
def to_s
hex
end
def to_ary
values
end
def inverse
self.class.new(*values.map { |v| v ^ 255 })
end
{
black: [0,0,0],
white: [255,255,255],
red: [255,0,0],
yellow: [255,255,0],
green: [0,255,0],
cyan: [0,255,255],
blue: [0,0,255],
magenta: [255,0,255]
}.each do |color, values|
define_singleton_method(color) { new(*values) }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment