colorable 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,5 +8,7 @@ module Colorable
8
8
  # When W3C color conflict with X11, a name of the color is appended with '2'.
9
9
  COLORNAMES = [["Alice Blue", [240, 248, 255]], ["Antique White", [250, 235, 215]], ["Aqua", [0, 255, 255]], ["Aquamarine", [127, 255, 212]], ["Azure", [240, 255, 255]], ["Beige", [245, 245, 220]], ["Bisque", [255, 228, 196]], ["Black", [0, 0, 0]], ["Blanched Almond", [255, 235, 205]], ["Blue", [0, 0, 255]], ["Blue Violet", [138, 43, 226]], ["Brown", [165, 42, 42]], ["Burlywood", [222, 184, 135]], ["Cadet Blue", [95, 158, 160]], ["Chartreuse", [127, 255, 0]], ["Chocolate", [210, 105, 30]], ["Coral", [255, 127, 80]], ["Cornflower", [100, 149, 237]], ["Cornsilk", [255, 248, 220]], ["Crimson", [220, 20, 60]], ["Cyan", [0, 255, 255]], ["Dark Blue", [0, 0, 139]], ["Dark Cyan", [0, 139, 139]], ["Dark Goldenrod", [184, 134, 11]], ["Dark Gray", [169, 169, 169]], ["Dark Green", [0, 100, 0]], ["Dark Khaki", [189, 183, 107]], ["Dark Magenta", [139, 0, 139]], ["Dark Olive Green", [85, 107, 47]], ["Dark Orange", [255, 140, 0]], ["Dark Orchid", [153, 50, 204]], ["Dark Red", [139, 0, 0]], ["Dark Salmon", [233, 150, 122]], ["Dark Sea Green", [143, 188, 143]], ["Dark Slate Blue", [72, 61, 139]], ["Dark Slate Gray", [47, 79, 79]], ["Dark Turquoise", [0, 206, 209]], ["Dark Violet", [148, 0, 211]], ["Deep Pink", [255, 20, 147]], ["Deep Sky Blue", [0, 191, 255]], ["Dim Gray", [105, 105, 105]], ["Dodger Blue", [30, 144, 255]], ["Firebrick", [178, 34, 34]], ["Floral White", [255, 250, 240]], ["Forest Green", [34, 139, 34]], ["Fuchsia", [255, 0, 255]], ["Gainsboro", [220, 220, 220]], ["Ghost White", [248, 248, 255]], ["Gold", [255, 215, 0]], ["Goldenrod", [218, 165, 32]], ["Gray", [190, 190, 190]], ["Gray2", [128, 128, 128]], ["Green", [0, 255, 0]], ["Green Yellow", [173, 255, 47]], ["Green2", [0, 128, 0]], ["Honeydew", [240, 255, 240]], ["Hot Pink", [255, 105, 180]], ["Indian Red", [205, 92, 92]], ["Indigo", [75, 0, 130]], ["Ivory", [255, 255, 240]], ["Khaki", [240, 230, 140]], ["Lavender", [230, 230, 250]], ["Lavender Blush", [255, 240, 245]], ["Lawn Green", [124, 252, 0]], ["Lemon Chiffon", [255, 250, 205]], ["Light Blue", [173, 216, 230]], ["Light Coral", [240, 128, 128]], ["Light Cyan", [224, 255, 255]], ["Light Goldenrod", [250, 250, 210]], ["Light Gray", [211, 211, 211]], ["Light Green", [144, 238, 144]], ["Light Pink", [255, 182, 193]], ["Light Salmon", [255, 160, 122]], ["Light Sea Green", [32, 178, 170]], ["Light Sky Blue", [135, 206, 250]], ["Light Slate Gray", [119, 136, 153]], ["Light Steel Blue", [176, 196, 222]], ["Light Yellow", [255, 255, 224]], ["Lime", [0, 255, 0]], ["Lime Green", [50, 205, 50]], ["Linen", [250, 240, 230]], ["Magenta", [255, 0, 255]], ["Maroon", [176, 48, 96]], ["Maroon2", [127, 0, 0]], ["Medium Aquamarine", [102, 205, 170]], ["Medium Blue", [0, 0, 205]], ["Medium Orchid", [186, 85, 211]], ["Medium Purple", [147, 112, 219]], ["Medium Sea Green", [60, 179, 113]], ["Medium Slate Blue", [123, 104, 238]], ["Medium Spring Green", [0, 250, 154]], ["Medium Turquoise", [72, 209, 204]], ["Medium Violet Red", [199, 21, 133]], ["Midnight Blue", [25, 25, 112]], ["Mint Cream", [245, 255, 250]], ["Misty Rose", [255, 228, 225]], ["Moccasin", [255, 228, 181]], ["Navajo White", [255, 222, 173]], ["Navy", [0, 0, 128]], ["Old Lace", [253, 245, 230]], ["Olive", [128, 128, 0]], ["Olive Drab", [107, 142, 35]], ["Orange", [255, 165, 0]], ["Orange Red", [255, 69, 0]], ["Orchid", [218, 112, 214]], ["Pale Goldenrod", [238, 232, 170]], ["Pale Green", [152, 251, 152]], ["Pale Turquoise", [175, 238, 238]], ["Pale Violet Red", [219, 112, 147]], ["Papaya Whip", [255, 239, 213]], ["Peach Puff", [255, 218, 185]], ["Peru", [205, 133, 63]], ["Pink", [255, 192, 203]], ["Plum", [221, 160, 221]], ["Powder Blue", [176, 224, 230]], ["Purple", [160, 32, 240]], ["Purple2", [127, 0, 127]], ["Red", [255, 0, 0]], ["Rosy Brown", [188, 143, 143]], ["Royal Blue", [65, 105, 225]], ["Saddle Brown", [139, 69, 19]], ["Salmon", [250, 128, 114]], ["Sandy Brown", [244, 164, 96]], ["Sea Green", [46, 139, 87]], ["Seashell", [255, 245, 238]], ["Sienna", [160, 82, 45]], ["Silver", [192, 192, 192]], ["Sky Blue", [135, 206, 235]], ["Slate Blue", [106, 90, 205]], ["Slate Gray", [112, 128, 144]], ["Snow", [255, 250, 250]], ["Spring Green", [0, 255, 127]], ["Steel Blue", [70, 130, 180]], ["Tan", [210, 180, 140]], ["Teal", [0, 128, 128]], ["Thistle", [216, 191, 216]], ["Tomato", [255, 99, 71]], ["Turquoise", [64, 224, 208]], ["Violet", [238, 130, 238]], ["Wheat", [245, 222, 179]], ["White", [255, 255, 255]], ["White Smoke", [245, 245, 245]], ["Yellow", [255, 255, 0]], ["Yellow Green", [154, 205, 50]]]
10
10
 
11
+ DARK_COLORS = ["Black", "Dark Green", "Dark Olive Green", "Dark Slate Gray", "Dim Gray", "Gray", "Indigo", "Midnight Blue", "Navy", "Navy Blue", "Blue Violet", "Brown", "Dark Slate Blue", "Dark Violet", "Firebrick", "Medium Blue", "Medium Violet Red", "Saddle Brown"]
12
+
11
13
  end
12
14
 
@@ -2,6 +2,7 @@ module Colorable
2
2
  class Color
3
3
  class ColorNameError < StandardError; end
4
4
  include Colorable::Converter
5
+ include Comparable
5
6
  attr_reader :name, :rgb
6
7
 
7
8
  def initialize(name_or_rgb)
@@ -29,6 +30,7 @@ module Colorable
29
30
  def hsb
30
31
  @hsb ||= rgb2hsb(rgb)
31
32
  end
33
+ alias :hsv :hsb
32
34
 
33
35
  %w(red green blue).each_with_index do |c, i|
34
36
  define_method(c) { rgb[i] }
@@ -38,6 +40,25 @@ module Colorable
38
40
  define_method(n) { hsb[i] }
39
41
  end
40
42
 
43
+ def <=>(other)
44
+ self.name <=> other.name
45
+ end
46
+
47
+ @@colorset = {}
48
+ def next(set=:name, n=1)
49
+ @@colorset[set] ||= Colorable::Colorset[set]
50
+ idx = @@colorset[set].find_index(self)
51
+ @@colorset[set].at(idx+n) if idx
52
+ end
53
+
54
+ def prev(set=:name, n=1)
55
+ self.next(set, -n)
56
+ end
57
+
58
+ def dark?
59
+ DARK_COLORS.detect { |d| d == self.name }
60
+ end
61
+
41
62
  private
42
63
  def varidate_name(name)
43
64
  COLORNAMES.detect do |label, _|
@@ -9,7 +9,7 @@ module Colorable
9
9
  end
10
10
 
11
11
  # +Colorset[:order]+ create a ordered colorset by passing a order key.
12
- def self.[](order, dir=:+)
12
+ def self.[](order, dir=:+, colorset=nil)
13
13
  rgb = [:red, :green, :blue]
14
14
  hsb = [:hue, :sat, :bright]
15
15
  blk =
@@ -24,9 +24,9 @@ module Colorable
24
24
 
25
25
  case dir
26
26
  when :+
27
- new.sort_by(&blk)
27
+ new(colorset).sort_by(&blk)
28
28
  when :-
29
- new.sort_by(&blk).reverse
29
+ new(colorset).sort_by(&blk).reverse
30
30
  else
31
31
  raise ArgumentError, "it must ':+' or ':-'"
32
32
  end
@@ -36,17 +36,21 @@ module Colorable
36
36
  @colorset.each(&blk)
37
37
  end
38
38
 
39
+ def size
40
+ @colorset.size
41
+ end
42
+
39
43
  def at(pos=0)
40
- @colorset[pos]
44
+ @colorset[pos%size]
41
45
  end
42
46
 
43
47
  def next(n=1)
44
- @pos += n
48
+ @pos = (@pos+n)%size
45
49
  at @pos
46
50
  end
47
51
 
48
52
  def prev(n=1)
49
- @pos -= n
53
+ @pos = (@pos-n)%size
50
54
  at @pos
51
55
  end
52
56
 
@@ -63,6 +67,10 @@ module Colorable
63
67
  @colorset.first(n)
64
68
  end
65
69
 
70
+ def find_index(color)
71
+ @colorset.find_index { |c| c == color }
72
+ end
73
+
66
74
  def sort_by(&blk)
67
75
  self.class.new @colorset.sort_by(&blk)
68
76
  end
@@ -70,5 +78,9 @@ module Colorable
70
78
  def reverse
71
79
  self.class.new @colorset.reverse
72
80
  end
81
+
82
+ def to_a
83
+ @colorset
84
+ end
73
85
  end
74
86
  end
@@ -1,3 +1,3 @@
1
1
  module Colorable
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -44,17 +44,57 @@ describe Colorable::Color do
44
44
  end
45
45
  end
46
46
 
47
- context "#hex" do
47
+ describe "#hex" do
48
48
  it { color.new("Alice Blue").hex.should eql "#F0F8FF" }
49
49
  it { color.new("Khaki").hex.should eql "#F0E68C" }
50
50
  it { color.new("Mint Cream").hex.should eql "#F5FFFA" }
51
51
  it { color.new("Thistle").hex.should eql "#D8BFD8" }
52
52
  end
53
53
 
54
- context "#hsb" do
54
+ describe "#hsb" do
55
55
  it { color.new("Alice Blue").hsb.should eql [208, 6, 100] }
56
56
  it { color.new("Khaki").hsb.should eql [55, 42, 94] }
57
57
  it { color.new("Mint Cream").hsb.should eql [150, 4, 100] }
58
58
  it { color.new("Thistle").hsb.should eql [300, 12, 85] }
59
59
  end
60
+
61
+ describe "#next" do
62
+ context "when no argument" do
63
+ it "returns next color in name order" do
64
+ @c = color.new("khaki")
65
+ @c.next.name.should eql "Lavender"
66
+ end
67
+ end
68
+
69
+ context "when :hsb passed" do
70
+ it "returns next color in hsb order" do
71
+ @c = color.new("khaki")
72
+ @c.next(:hsb).name.should eql "Dark Khaki"
73
+ end
74
+ end
75
+
76
+ context "when color is not in X11 colorset" do
77
+ it "returns nil" do
78
+ @c = color.new([100,10,10])
79
+ @c.next.should be_nil
80
+ end
81
+ end
82
+ end
83
+
84
+ describe "#prev" do
85
+ context "when no argument" do
86
+ it "returns prev color in name order" do
87
+ @c = color.new("Alice Blue")
88
+ @c.prev.name.should eql "Yellow Green"
89
+ end
90
+ end
91
+
92
+ context "when :hsb passed" do
93
+ it "returns prev color in hsb order" do
94
+ @c = color.new("Yellow")
95
+ @c.prev(:hsb).name.should eql "Olive"
96
+ end
97
+ end
98
+ end
99
+
60
100
  end
@@ -41,6 +41,12 @@ describe Colorable::Colorset do
41
41
  it { @cs.at.to_s.should eql 'rgb(0,0,0)' }
42
42
  it { @cs.at(1).to_s.should eql 'rgb(0,0,128)' }
43
43
  end
44
+
45
+ context "when argument exceed colorset size" do
46
+ it "acts like linked list" do
47
+ colorset.new.at(144).name.should eql "Alice Blue"
48
+ end
49
+ end
44
50
  end
45
51
 
46
52
  describe "#next" do
@@ -63,4 +69,10 @@ describe Colorable::Colorset do
63
69
  before(:all) { @cs = colorset.new }
64
70
  it { @cs.next(10); @cs.rewind.should eql @cs.at }
65
71
  end
72
+
73
+ describe "#size" do
74
+ it "returns size of colorset" do
75
+ colorset.new.size.should eql 144
76
+ end
77
+ end
66
78
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: colorable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-05 00:00:00.000000000 Z
12
+ date: 2012-11-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec