colormath 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,42 @@
1
+ ColorMath
2
+ =========
3
+
4
+ A simple Ruby library to perform operations on RGB and HSL colours.
5
+
6
+ Usage
7
+ -----
8
+
9
+ Instantiate an RGB (red, green, blue) colour:
10
+
11
+ orange = ColorMath::RGB.new(1.0, 0.5, 0)
12
+
13
+ Or from a hex value via a helper method:
14
+
15
+ white = ColorMath::hex_color("#fff")
16
+ blue = ColorMath::hex_color("#0000ff")
17
+
18
+ Instantiate an HSL (hue, saturation, luminance) colour:
19
+
20
+ pink = ColorMath::HSL.new(350, 1, 0.88)
21
+
22
+ Retrieve the RGB components of a colour:
23
+
24
+ pink.red # => 1.0
25
+ pink.green # => 0.76
26
+ pink.blue # => 0.8
27
+
28
+ Or the HSL components:
29
+
30
+ orange.hue # => 30.0
31
+ orange.saturation # => 1.0
32
+ orange.luminance # => 0.5
33
+
34
+ Combine two colours via an alpha blend, e.g. 30% orange on white:
35
+
36
+ combined = ColorMath::Blend.alpha(white, orange, 0.3)
37
+
38
+ Convert a colour to hexadecimal representation:
39
+
40
+ combined.hex # => "#ffd8b2"
41
+
42
+ That’s it. It only does the basics that I need for the job in hand, but it’s probably a good basis for extension.
@@ -24,5 +24,6 @@ module ColorMath
24
24
  end
25
25
 
26
26
  require "colormath/color"
27
+ require "colormath/adjust"
27
28
  require "colormath/blend"
28
29
  require "colormath/version"
@@ -0,0 +1,39 @@
1
+ module ColorMath
2
+
3
+ # Adjust parameters of a colour
4
+ #
5
+ module Adjust
6
+
7
+ # Rotate the hue by delta degrees in either direction
8
+ def hue(color, delta)
9
+ HSL.new(color.hue + delta, color.saturation, color.luminance)
10
+ end
11
+
12
+ # Adjust the saturation by delta, stopping at 0 or 1
13
+ def saturation(color, delta)
14
+ HSL.new(color.hue, color.saturation + delta, color.luminance)
15
+ end
16
+
17
+ # Adjust the luminance by delta, stopping at 0 or 1
18
+ def luminance(color, delta)
19
+ HSL.new(color.hue, color.saturation, color.luminance + delta)
20
+ end
21
+
22
+ # Adjust the red component by delta, stopping at 0 or 1
23
+ def red(color, delta)
24
+ RGB.new(color.red + delta, color.green, color.blue)
25
+ end
26
+
27
+ # Adjust the green component by delta, stopping at 0 or 1
28
+ def green(color, delta)
29
+ RGB.new(color.red, color.green + delta, color.blue)
30
+ end
31
+
32
+ # Adjust the blue component by delta, stopping at 0 or 1
33
+ def blue(color, delta)
34
+ RGB.new(color.red, color.green, color.blue + delta)
35
+ end
36
+
37
+ extend self
38
+ end
39
+ end
@@ -12,10 +12,11 @@ module ColorMath
12
12
  # 0 <= s <= 1
13
13
  # 0 <= l <= 1
14
14
  #
15
- # Values outside these ranges will be clippped.
15
+ # Saturation and luminance values outside these ranges will be clipped.
16
+ # Hue values will be mapped to a circle, so that e.g. -20 becomes 340.
16
17
  #
17
18
  def initialize(h, s, l)
18
- @hue = force_range(h, 0, 360).to_f
19
+ @hue = h % 360
19
20
  @saturation = force_range(s, 0, 1).to_f
20
21
  @luminance = force_range(l, 0, 1).to_f
21
22
  end
@@ -23,6 +23,7 @@ module ColorMath
23
23
  # The hue component of the colour in HSL representation where 0 <= h < 360
24
24
  #
25
25
  def hue
26
+ return 0 if saturation.zero?
26
27
  case max
27
28
  when red
28
29
  (60.0 * ((green - blue) / (max - min))) % 360.0
@@ -2,7 +2,7 @@ module ColorMath
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- TINY = 0
5
+ TINY = 1
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
@@ -0,0 +1,81 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
2
+ require "test/unit"
3
+ require "shoulda"
4
+ require "colormath"
5
+
6
+ class AdjustmentTest < Test::Unit::TestCase
7
+ include ColorMath
8
+ EPSILON = 1e-3
9
+
10
+ context "in the HSL space" do
11
+ should "rotate hue" do
12
+ c = HSL.new(180, 0.5, 0.5)
13
+ assert_equal 120, Adjust.hue(c, -60).hue
14
+ assert_equal 340, Adjust.hue(c, -200).hue
15
+ assert_equal 240, Adjust.hue(c, 60).hue
16
+ assert_equal 20, Adjust.hue(c, 200).hue
17
+ end
18
+
19
+ should "adjust saturation" do
20
+ c = HSL.new(180, 0.5, 0.5)
21
+ assert_in_delta 0.2, Adjust.saturation(c, -0.3).saturation, EPSILON
22
+ assert_in_delta 0.0, Adjust.saturation(c, -0.6).saturation, EPSILON
23
+ assert_in_delta 0.8, Adjust.saturation(c, 0.3).saturation, EPSILON
24
+ assert_in_delta 1.0, Adjust.saturation(c, 0.6).saturation, EPSILON
25
+ end
26
+
27
+ should "adjust luminance" do
28
+ c = HSL.new(180, 0.5, 0.5)
29
+ assert_in_delta 0.2, Adjust.luminance(c, -0.3).luminance, EPSILON
30
+ assert_in_delta 0.0, Adjust.luminance(c, -0.6).luminance, EPSILON
31
+ assert_in_delta 0.8, Adjust.luminance(c, 0.3).luminance, EPSILON
32
+ assert_in_delta 1.0, Adjust.luminance(c, 0.6).luminance, EPSILON
33
+ end
34
+
35
+ should "not affect unrelated parameters" do
36
+ c = HSL.new(180, 0.4, 0.6)
37
+ assert_in_delta 180, Adjust.saturation(c, -0.3).hue, EPSILON
38
+ assert_in_delta 180, Adjust.luminance(c, -0.3).hue, EPSILON
39
+ assert_in_delta 0.4, Adjust.hue(c, -20).saturation, EPSILON
40
+ assert_in_delta 0.4, Adjust.luminance(c, -0.3).saturation, EPSILON
41
+ assert_in_delta 0.6, Adjust.hue(c, -20).luminance, EPSILON
42
+ assert_in_delta 0.6, Adjust.saturation(c, -0.3).luminance, EPSILON
43
+ end
44
+ end
45
+
46
+ context "in the RGB space" do
47
+ should "adjust red" do
48
+ c = RGB.new(0.5, 0.3, 0.4)
49
+ assert_in_delta 0.2, Adjust.red(c, -0.3).red, EPSILON
50
+ assert_in_delta 0.0, Adjust.red(c, -0.6).red, EPSILON
51
+ assert_in_delta 0.8, Adjust.red(c, 0.3).red, EPSILON
52
+ assert_in_delta 1.0, Adjust.red(c, 0.6).red, EPSILON
53
+ end
54
+
55
+ should "adjust green" do
56
+ c = RGB.new(0.3, 0.5, 0.4)
57
+ assert_in_delta 0.2, Adjust.green(c, -0.3).green, EPSILON
58
+ assert_in_delta 0.0, Adjust.green(c, -0.6).green, EPSILON
59
+ assert_in_delta 0.8, Adjust.green(c, 0.3).green, EPSILON
60
+ assert_in_delta 1.0, Adjust.green(c, 0.6).green, EPSILON
61
+ end
62
+
63
+ should "adjust blue" do
64
+ c = RGB.new(0.3, 0.4, 0.5)
65
+ assert_in_delta 0.2, Adjust.blue(c, -0.3).blue, EPSILON
66
+ assert_in_delta 0.0, Adjust.blue(c, -0.6).blue, EPSILON
67
+ assert_in_delta 0.8, Adjust.blue(c, 0.3).blue, EPSILON
68
+ assert_in_delta 1.0, Adjust.blue(c, 0.6).blue, EPSILON
69
+ end
70
+
71
+ should "not affect unrelated parameters" do
72
+ c = RGB.new(0.3, 0.4, 0.5)
73
+ assert_in_delta 0.3, Adjust.green(c, -0.3).red, EPSILON
74
+ assert_in_delta 0.3, Adjust.blue(c, -0.3).red, EPSILON
75
+ assert_in_delta 0.4, Adjust.red(c, -0.3).green, EPSILON
76
+ assert_in_delta 0.4, Adjust.blue(c, -0.3).green, EPSILON
77
+ assert_in_delta 0.5, Adjust.red(c, -0.3).blue, EPSILON
78
+ assert_in_delta 0.5, Adjust.green(c, -0.3).blue, EPSILON
79
+ end
80
+ end
81
+ end
@@ -14,12 +14,12 @@ class HSLTest < Test::Unit::TestCase
14
14
 
15
15
  should "force hue >= 0" do
16
16
  c = ColorMath::HSL.new(-2, 0, 0)
17
- assert_equal 0, c.hue
17
+ assert_equal 358, c.hue
18
18
  end
19
19
 
20
20
  should "force hue <= 360" do
21
21
  c = ColorMath::HSL.new(361, 0, 0)
22
- assert_equal 360, c.hue
22
+ assert_equal 1, c.hue
23
23
  end
24
24
 
25
25
  should "force saturation >= 0" do
@@ -41,4 +41,9 @@ class RGBTest < Test::Unit::TestCase
41
41
  c = ColorMath::RGB.new(0, 0, 1.1)
42
42
  assert_equal 1, c.blue
43
43
  end
44
+
45
+ should "have a hue of zero if it is white" do
46
+ c = ColorMath::RGB.new(1, 1, 1)
47
+ assert_equal 0, c.hue
48
+ end
44
49
  end
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.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Battley
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-09 00:00:00 +01:00
12
+ date: 2009-12-03 00:00:00 +00:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -31,11 +31,14 @@ extensions: []
31
31
  extra_rdoc_files: []
32
32
 
33
33
  files:
34
+ - README.md
35
+ - test/adjust_test.rb
34
36
  - test/blend_test.rb
35
37
  - test/conversion_test.rb
36
38
  - test/hex_decoding_test.rb
37
39
  - test/hsl_test.rb
38
40
  - test/rgb_test.rb
41
+ - lib/colormath/adjust.rb
39
42
  - lib/colormath/blend.rb
40
43
  - lib/colormath/color/hsl.rb
41
44
  - lib/colormath/color/rgb.rb
@@ -66,7 +69,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
66
69
  requirements: []
67
70
 
68
71
  rubyforge_project:
69
- rubygems_version: 1.3.4
72
+ rubygems_version: 1.3.5
70
73
  signing_key:
71
74
  specification_version: 3
72
75
  summary: Colour manipulation library for Ruby