Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #1 from floomoon/master

Added X11 colors support and some tests
  • Loading branch information...
commit c2a8dcc06d5ac9bbfc48ca86ae58ee7bef5de927 2 parents 88a14d1 + 55f4798
Michael Bleigh authored
View
6 Rakefile
@@ -0,0 +1,6 @@
+require 'bundler/gem_tasks'
+require 'rspec/core/rake_task'
+
+RSpec::Core::RakeTask.new(:spec)
+
+task :default => :spec
View
2  colorist.gemspec
@@ -14,6 +14,8 @@ Gem::Specification.new do |s|
"lib/colorist.rb",
"lib/colorist/color.rb",
"lib/colorist/core_extensions.rb" ]
+ s.add_development_dependency 'rake'
+ s.add_development_dependency 'rspec', '~> 2.8'
s.rdoc_options = ["--main", "README.rdoc"]
s.extra_rdoc_files = ["MIT_LICENSE.rdoc", "README.rdoc"]
end
View
182 lib/colorist/color.rb
@@ -5,7 +5,7 @@ module Colorist
class Color
attr_accessor :r, :g, :b
- CSS_COLOR_NAMES = { "maroon" => 0x800000,
+ W3C_COLOR_NAMES = { "maroon" => 0x800000,
"red" => 0xff0000,
"orange" => 0xffa500,
"yellow" => 0xffff00,
@@ -22,7 +22,146 @@ class Color
"black" => 0x000000,
"silver" => 0xc0c0c0,
"gray" => 0x808080 }
-
+
+ X11_COLOR_NAMES = { "alice_blue" => 0xf0f8ff,
+ "anthique_white" => 0xfaebd7,
+ "aqua" => 0x00ffff,
+ "auquamarine" => 0x7fffd4,
+ "azure" => 0xf0ffff,
+ "beige" => 0xf5f5dc,
+ "bisque" => 0xffe4c4,
+ "black" => 0x000000,
+ "blanched_almond" => 0xffebcd,
+ "blue" => 0x0000ff,
+ "blue_violet" => 0x8a2be2,
+ "brown" => 0xa52a2a,
+ "burly_wood" => 0xdeb887,
+ "cadet blue" => 0x5f9ea0,
+ "chartreuse" => 0x7fff00,
+ "chocolate" => 0xd2691e,
+ "coral" => 0xff7f50,
+ "cornflower" => 0x6495ed,
+ "cornslik" => 0xfff8dc,
+ "crimson" => 0xdc143c,
+ "cyan" => 0x00ffff,
+ "dark_blue" => 0x00008b,
+ "dark_cyan" => 0x008b8b,
+ "dark_goldenrod" => 0xb8860b,
+ "dark_gray" => 0xa9a9a9,
+ "dark_green" => 0x006400,
+ "dark_khaki" => 0xbdb76b,
+ "dark_magenta" => 0x8b008b,
+ "dark_olive_green" => 0x556b2f,
+ "dark_orange" => 0xff8c00,
+ "dark_orchid" => 0x9932cc,
+ "dark_red" => 0x8b0000,
+ "dark_salmon" => 0xe9967a,
+ "dark_sea_green" => 0x8fbc8f,
+ "dark_slate_blue" => 0x483d8b,
+ "dark_slate_gray" => 0x2f4f4f,
+ "dark_turquoise" => 0x00ced1,
+ "dark_violet" => 0x9400d3,
+ "deep_pink" => 0xff1493,
+ "deep_sky_blue" => 0x00bfff,
+ "dim_gray" => 0x696969,
+ "dodger_blue" => 0x1e90ff,
+ "firebrick" => 0xb22222,
+ "floral_white" => 0xfffaf0,
+ "forest_green" => 0x228b22,
+ "fuchsia" => 0xff00ff,
+ "gainsboro" => 0xdcdcdc,
+ "ghost_white" => 0xf8f8ff,
+ "gold" => 0xffd700,
+ "goldenrod" => 0xdaa520,
+ "gray" => 0xbebebe,
+ "green" => 0x008000,
+ "green_yellow" => 0xadff2f,
+ "honeydew" => 0xf0fff0,
+ "hot_pink" => 0xff69b4,
+ "indian_red" => 0xcd5c5c,
+ "indigo" => 0x4b0082,
+ "ivory" => 0xfffff0,
+ "khaki" => 0xf0e68c,
+ "lavender" => 0xe6e6fa,
+ "lavender_blush" => 0xfff0f5,
+ "lawn_green" => 0x7cfc00,
+ "lemon_chiffon" => 0xfffacd,
+ "light_blue" => 0xadd8e6,
+ "light_coral" => 0xf08080,
+ "light_cyan" => 0xe0ffff,
+ "light_goldenrod" => 0xfafad2,
+ "light_gray" => 0xd3d3d3,
+ "light_green" => 0x90ee90,
+ "light_pink" => 0xffb6c1,
+ "light_salmon" => 0xffa07a,
+ "light_sea_green" => 0x20b2aa,
+ "light_sky_blue" => 0x87cefa,
+ "light_slate_gray" => 0x778899,
+ "light_steel_blue" => 0xb0c4de,
+ "light_yellow" => 0xffffe0,
+ "lime" => 0x00ff00,
+ "lime_green" => 0x32cd32,
+ "linen" => 0xfaf0e6,
+ "magenta" => 0xff00ff,
+ "maroon" => 0xb03060,
+ "medium_aquamarine" => 0x66cdaa,
+ "medium_blue" => 0x0000cd,
+ "medium_orchid" => 0xba55d3,
+ "medium_purple" => 0x9370db,
+ "medium_sea_green" => 0x3cb371,
+ "medium_slate_blue" => 0x7b68ee,
+ "medium_spring_green" => 0x00fa9a,
+ "medium_turquoise" => 0x48d1cc,
+ "medium_violet_red" => 0xc71585,
+ "midnight_blue" => 0x191970,
+ "mint_cream" => 0xf5fffa,
+ "misty_rose" => 0xffe4e1,
+ "moccasin" => 0xffe4b5,
+ "navajo_white" => 0xffdead,
+ "navy" => 0x000080,
+ "old_lace" => 0xfdf5e6,
+ "olive" => 0x808000,
+ "olive_drab" => 0x6b8e23,
+ "orange" => 0xffa500,
+ "orange_red" => 0xff4500,
+ "orchid" => 0xda70d6,
+ "pale_goldenrod" => 0xeee8aa,
+ "pale_green" => 0x98fb98,
+ "pale_turquoise" => 0xafeeee,
+ "pale_violet_red" => 0xdb7093,
+ "papaya_whip" => 0xffefd5,
+ "peach_puff" => 0xffdab9,
+ "peru" => 0xcd853f,
+ "pink" => 0xffc0cb,
+ "plum" => 0xdda0dd,
+ "powder_blue" => 0xb0e0e6,
+ "purple" => 0xa020f0,
+ "red" => 0xff0000,
+ "rosy_brown" => 0xbc8f8f,
+ "royal_blue" => 0x4169e1,
+ "saddle_brown" => 0x8b4513,
+ "salmon" => 0xfa8072,
+ "sandy_brown" => 0xf4a460,
+ "sea_green" => 0x2e8b57,
+ "seashell" => 0xfff5ee,
+ "sienna" => 0xa0522d,
+ "sky_blue" => 0x87ceeb,
+ "slate_blue" => 0x6a5acd,
+ "slate_gray" => 0x708090,
+ "snow" => 0xfffafa,
+ "spring_green" => 0x00ff7f,
+ "steel_blue" => 0x4682b4,
+ "tan" => 0xd2b48c,
+ "teal" => 0x008080,
+ "thistle" => 0xd8bfd8,
+ "tomato" => 0xff6347,
+ "turquoise" => 0x40e0d0,
+ "violet" => 0xee82ee,
+ "wheat" => 0xf5deb3,
+ "white" => 0xffffff,
+ "white_smoke" => 0xf5f5f5,
+ "yellow" => 0xffff00 }
+
# Creates a new color with the hex color provided as a number (i.e. 0x112233)
def initialize(color=0x000000)
string = "%.6x" % color
@@ -69,10 +208,27 @@ def self.from_hsv(hue, saturation, value)
end
end
- # Converts a CSS hex string into a color. Works both with the
+ # Converts a W3C hex string into a color. Works both with the
# full form (i.e. <tt>#ffffff</tt>) and the abbreviated form (<tt>#fff</tt>). Can
- # also take any of the 16 named CSS colors.
- def self.from_string(some_string)
+ # also take any of the 16 named W3C colors.
+ # Specify a standard if you enter the key word as defined by the standard you use, the
+ # available standards are :
+ #
+ # * <tt>:w3c</tt> - Used by default, represent W3C colors specified in W3C_COLOR_NAMES constant
+ # * <tt>:x11</tt> - represent X11 standard colors specified in X11_COLOR_NAMES constant
+ #
+ # Examples :
+ #
+ # # Here we can ommit standard parameter (:w3c by default)
+ # color = Colorist::Color.from_string "gray" # => <Color #808080>
+ #
+ # # For X11 standard (gray hex value is different)
+ # color = Colorist::Color.from_string "gray", :x11 # => <Color #BEBEBE>
+ #
+ # # Other X11 color (case insensitive)
+ # color = Colorist::Color.from_string "Dark Olive Green", :x11 # => <Color #556B2F>
+ def self.from_string(some_string, standard=:w3c)
+ some_string = some_string.downcase.sub(/ /, "_")
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)
@@ -80,10 +236,20 @@ def self.from_string(some_string)
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])
+ elsif standard == :w3c
+ if W3C_COLOR_NAMES.key?(some_string)
+ color = Colorist::Color.new(W3C_COLOR_NAMES[some_string])
+ else
+ raise ArgumentError, "#{some_string} is not a valid W3C color.", caller
+ end
+ elsif standard == :x11
+ if X11_COLOR_NAMES.key?(some_string)
+ color = Colorist::Color.new(X11_COLOR_NAMES[some_string])
+ else
+ raise ArgumentError, "#{some_string} is not a valid X11 color.", caller
+ end
else
- raise ArgumentError, "Must provide a valid CSS hex color or color name.", caller
+ raise ArgumentError, "Must provide a valid W3C or X11 hex color or color name.", caller
end
color
end
View
104 spec/colorist/color_spec.rb
@@ -0,0 +1,104 @@
+require 'spec_helper'
+require 'colorist'
+
+describe Colorist::Color do
+
+ before :each do
+ @color = Colorist::Color.new 0x808080
+ end
+
+ describe "#1 Color" do
+
+ it "should initialize default color (#000000)" do
+ Colorist::Color.new.to_s.should == "#000000"
+ end
+
+ it "should initialize the approriate color" do
+ hex_color = 0x112233
+ Colorist::Color.new(hex_color).to_s.should == "#112233"
+ end
+
+ it "should normalize rgb value on modification" do
+ color = Colorist::Color.from("#ff2233")
+ color.r, color.g, color.b = 22, 256, -3
+ color.to_s.should == "#16ff00"
+ end
+
+ it "should convert ruby object to Color instance" do
+ "#808080".to_color.should == @color
+ end
+
+ it "should duplicate the color (rgb values)" do
+ color = @color.dup
+ [color.r, color.g, color.b].should === [@color.r, @color.g, @color.b]
+ end
+
+ end
+
+ describe "#2 From entity" do
+
+ it "should return the color from css string (#121212)" do
+ Colorist::Color.from_string("#121212").to_s.should == "#121212"
+ end
+
+ it "should return the W3C gray color from word (#bebebe)" do
+ Colorist::Color.from_string("gray", :w3c).to_s.should == "#808080"
+ end
+
+ it "should return the X11 gray color from word (#808080)" do
+ Colorist::Color.from_string("gray", :x11).to_s.should == "#bebebe"
+ end
+
+ it "should return color from rgb" do
+ Colorist::Color.from_rgb(24, 230, 36).to_s.should == "#18e624"
+ end
+
+ it "should return color from rgb with percent values" do
+ Colorist::Color.from_rgb(0.15, 1, 0.2, :percent => true).to_s.should == "#26ff33"
+ end
+
+ it "should return color from hsv" do
+ Colorist::Color.from_hsv(0, 0.0, 0.5019607843137255).to_s.should == @color.to_s
+ end
+
+ end
+
+ describe "#3 Operations" do
+
+ it "should be equal to the other color" do
+ @color.should == Colorist::Color.from_string("gray")
+ end
+
+ it "should add color correctly" do
+ color = Colorist::Color.new(0x223344) + Colorist::Color.new(0x111111)
+ color.should == Colorist::Color.new(0x334455)
+ end
+
+ it "should substract color correctly" do
+ color = Colorist::Color.new(0x223344) - Colorist::Color.new(0x111111)
+ color.should == Colorist::Color.new(0x112233)
+ end
+
+ end
+
+ describe "#4 Format" do
+
+ it "should convert to css string" do
+ @color.to_s(:css).should == "#808080"
+ end
+
+ it "should convert to rgb in css style" do
+ @color.to_s(:css_rgb).should == "rgb(128.00,128.00,128.00)"
+ end
+
+ it "should convert to rgb triplet" do
+ @color.to_s(:rgb).should == "0.000, 0.000, 0.000"
+ end
+
+ it "should convert to hsv" do
+ @color.to_hsv.should == [0, 0.0, 0.5019607843137255]
+ end
+
+ end
+
+end
View
5 spec/spec_helper.rb
@@ -0,0 +1,5 @@
+require 'bundler/setup'
+require 'rspec'
+
+RSpec.configure do |config|
+end
Please sign in to comment.
Something went wrong with that request. Please try again.