colormath 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/colormath/color.rb +11 -11
- data/lib/colormath/version.rb +1 -1
- data/test/hex_encoding_test.rb +30 -0
- data/test/hsl_test.rb +0 -8
- data/test/rgb_test.rb +0 -8
- metadata +2 -1
data/lib/colormath/color.rb
CHANGED
@@ -3,17 +3,6 @@ module ColorMath
|
|
3
3
|
# Color can be mixed into any class that responds to red, green, and blue, where 0 <= c <= 1
|
4
4
|
#
|
5
5
|
module Color
|
6
|
-
EPSILON = 1/256.0
|
7
|
-
|
8
|
-
# Returns true if the RGB components of the two colours differ by less than EPSILON,
|
9
|
-
# i.e. they are the same when represented in 8 bits.
|
10
|
-
#
|
11
|
-
def ==(other)
|
12
|
-
deltas = [ other.red - self.red,
|
13
|
-
other.green - self.green,
|
14
|
-
other.blue - self.blue ].map{ |e| e.abs }
|
15
|
-
deltas.max < EPSILON
|
16
|
-
end
|
17
6
|
|
18
7
|
# The six-digit hexadecimal representation of the colour, e.g. "#cafe66"
|
19
8
|
#
|
@@ -21,6 +10,17 @@ module ColorMath
|
|
21
10
|
"#%02x%02x%02x" % [red * 0xff, green * 0xff, blue * 0xff]
|
22
11
|
end
|
23
12
|
|
13
|
+
# The hexadecimal representation of the colour, using 3 digits if possible
|
14
|
+
#
|
15
|
+
def compact_hex
|
16
|
+
case h = hex
|
17
|
+
when /^#(.)\1(.)\2(.)\3$/
|
18
|
+
"##$1#$2#$3"
|
19
|
+
else
|
20
|
+
h
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
24
|
def inspect(*args)
|
25
25
|
"<%s r=%0.3f g=%0.3f b=%0.3f>" % [self.class.to_s, red, green, blue]
|
26
26
|
end
|
data/lib/colormath/version.rb
CHANGED
@@ -0,0 +1,30 @@
|
|
1
|
+
|
2
|
+
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
3
|
+
require "test/unit"
|
4
|
+
require "shoulda"
|
5
|
+
require "colormath"
|
6
|
+
|
7
|
+
class HexEncodingTest < Test::Unit::TestCase
|
8
|
+
|
9
|
+
should "generate hex code" do
|
10
|
+
%w[ #123456 #abcdef #000000 #ffffff #112233 #aabbcc ].each do |hex|
|
11
|
+
assert_equal hex, ColorMath::hex_color(hex).hex
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context "when generating short hex code" do
|
16
|
+
should "use three digits if possible" do
|
17
|
+
assert_equal "#000", ColorMath::hex_color("#000000").compact_hex
|
18
|
+
assert_equal "#fff", ColorMath::hex_color("#ffffff").compact_hex
|
19
|
+
assert_equal "#123", ColorMath::hex_color("#112233").compact_hex
|
20
|
+
assert_equal "#abc", ColorMath::hex_color("#aabbcc").compact_hex
|
21
|
+
end
|
22
|
+
|
23
|
+
should "use six digits if not possible" do
|
24
|
+
%w[ #123456 #abcdef ].each do |hex|
|
25
|
+
assert_equal hex, ColorMath::hex_color(hex).compact_hex
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
data/test/hsl_test.rb
CHANGED
@@ -4,14 +4,6 @@ require "shoulda"
|
|
4
4
|
require "colormath"
|
5
5
|
|
6
6
|
class HSLTest < Test::Unit::TestCase
|
7
|
-
should "be equal if initialized with same values" do
|
8
|
-
assert_equal ColorMath::HSL.new(123, 0.5, 0.7), ColorMath::HSL.new(123, 0.5, 0.7)
|
9
|
-
end
|
10
|
-
|
11
|
-
should "not be equal if initialized with different values" do
|
12
|
-
assert_not_equal ColorMath::HSL.new(124, 0.4, 0.8), ColorMath::HSL.new(123, 0.5, 0.7)
|
13
|
-
end
|
14
|
-
|
15
7
|
should "force hue >= 0" do
|
16
8
|
c = ColorMath::HSL.new(-2, 0, 0)
|
17
9
|
assert_equal 358, c.hue
|
data/test/rgb_test.rb
CHANGED
@@ -4,14 +4,6 @@ require "shoulda"
|
|
4
4
|
require "colormath"
|
5
5
|
|
6
6
|
class RGBTest < Test::Unit::TestCase
|
7
|
-
should "be equal if initialized with same values" do
|
8
|
-
assert_equal ColorMath::RGB.new(0.3, 0.5, 0.7), ColorMath::RGB.new(0.3, 0.5, 0.7)
|
9
|
-
end
|
10
|
-
|
11
|
-
should "not be equal if initialized with different values" do
|
12
|
-
assert_not_equal ColorMath::RGB.new(0.5, 0.4, 0.8), ColorMath::RGB.new(0.3, 0.5, 0.7)
|
13
|
-
end
|
14
|
-
|
15
7
|
should "force red >= 0" do
|
16
8
|
c = ColorMath::HSL.new(-2, 0, 0)
|
17
9
|
assert_equal 0, c.red
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: colormath
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paul Battley
|
@@ -36,6 +36,7 @@ files:
|
|
36
36
|
- test/blend_test.rb
|
37
37
|
- test/conversion_test.rb
|
38
38
|
- test/hex_decoding_test.rb
|
39
|
+
- test/hex_encoding_test.rb
|
39
40
|
- test/hsl_test.rb
|
40
41
|
- test/rgb_test.rb
|
41
42
|
- lib/colormath/adjust.rb
|