color 0.1.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
Binary file
@@ -0,0 +1,91 @@
1
+ == Color 1.4.0 / 2007.02.11
2
+ * Merged Austin Ziegler's color-tools library (previously part of the Ruby
3
+ PDF Tools project) with Matt Lyon's color library.
4
+ - The HSL implementation from the Color class has been merged into
5
+ Color::HSL. Color is a module the way it was for color-tools.
6
+ - A thin veneer has been written to allow Color::new to return a Color::HSL
7
+ instance; Color::HSL supports as many methods as possible that were
8
+ previously supported by the Color class.
9
+ - Values that were previously rounded by Color are no longer rounded;
10
+ fractional values matter.
11
+ * Converted to hoe for project management.
12
+ * Moved to the next step of deprecating Color::Name values; printing a
13
+ warning for each use (see the history for color-tools 1.3.0).
14
+ * Print a warning on the access of either VERSION or COLOR_TOOLS_VERSION; the
15
+ version constant is now COLOR_VERSION.
16
+ * Added humanized versions of accessors (e.g., CMYK colours now have both #cyan
17
+ and #c to access the cyan component of the colour; #cyan provides the value
18
+ as a percentage).
19
+ * Added CSS3 formatters for RGB, RGBA, HSL, and HSLA outputs. Note that the
20
+ Color library does not yet have a way of setting alpha opacity, so the
21
+ output for RGBA and HSLA are at full alpha opacity (1.0). The values are
22
+ output with two decimal places.
23
+ * Applied a patch to provide simple arithmetic colour addition and subtraction
24
+ to Color::GrayScale and Color::RGB. The patch was contributed by Jeremy
25
+ Hinegardner <jeremy@hinegardner.org>. This patch also provides the ability to
26
+ return the maximum RGB value as a grayscale colour.
27
+ * Fixed two problems reported by Jean Krohn <jb.krohn@free.fr> against
28
+ color-tools relating to RGB-to-HSL and HSL-to-RGB conversion. (Color and
29
+ color-tools use the same formulas, but the ordering of the calculations is
30
+ slightly different with Color and did not suffer from this problem;
31
+ color-tools was more sensitive to floating-point values and precision
32
+ errors.)
33
+ * Fixed an issue with HSL/RGB conversions reported by Adam Johnson
34
+ <adam.sven.johnson@gmail.com>.
35
+ * Added an Adobe Color swatch (Photoshop) palette reader,
36
+ Color::Palette::AdobeColor (for .aco files only).
37
+
38
+ == Color 0.1.0 / 2006.08.05
39
+ * Added HSL (degree, percent, percent) interface.
40
+ * Removed RGB instance variable; color is managed internally as HSL floating
41
+ point.
42
+ * Tests!
43
+
44
+ == color-tools 1.3.0
45
+ * Added new metallic colours suggested by Jim Freeze <jfn@freeze.org>. These
46
+ are in the namespace Color::Metallic.
47
+ * Colours that were defined in the Color namespace (e.g., Color::Red,
48
+ Color::AliceBlue) are now defined in Color::RGB (e.g., Color::RGB::Red,
49
+ Color::RGB::AliceBlue). They are added back to the Color namespace on the
50
+ first use of the old colours and a warning is printed. In version 1.4, this
51
+ warning will be printed on every use of the old colours. In version 1.5,
52
+ the backwards compatible support for colours like Color::Red will be
53
+ removed completely.
54
+ * Added the Color::CSS module, color/css or Color::CSS that provides a name
55
+ lookup of Color::RGB-namespaced constants with Color::CSS[name]. Most of
56
+ these colours (which are mirrored from the Color::RGB default colours) are
57
+ only "officially" recognised under the CSS3 colour module or SVG.
58
+ * Added the Color::HSL colour space and some helper utilities to Color::RGB
59
+ for colour manipulation using the HSL value.
60
+ * Controlled internal value replacement to be between 0 and 1 for all
61
+ colours.
62
+ * Updated Color::Palette::Gimp to more meaningfully deal with duplicate named
63
+ colours. Named colours now return an array of colours.
64
+ * Indicated the plans for some methods and constants out to color-tools 2.0.
65
+ * Added unit tests and fixed a number of hidden bugs because of them.
66
+
67
+ == color-tools 1.2.0
68
+ * Changed installer from a custom-written install.rb to setup.rb
69
+ 3.3.1-modified.
70
+ * Added Color::GreyScale (or Color::GrayScale).
71
+ * Added Color::YIQ. This colour definition is incomplete; it does not have
72
+ conversions from YIQ to other colour spaces.
73
+
74
+ == color-tools 1.1.0
75
+ * Added color/palette/gimp to support the reading and use of GIMP color
76
+ palettes.
77
+
78
+ == color-tools 1.0.0
79
+ * Initial release.
80
+
81
+ == Copyright
82
+ Color
83
+ Colour Management with Ruby
84
+ http://rubyforge.org/projects/color
85
+
86
+ Licensed under a MIT-style licence. See Licence.txt in the main
87
+ distribution for full licensing information.
88
+
89
+ Copyright (c) 2005 - 2007 Austin Ziegler and Matt Lyon
90
+
91
+ $Id: History.txt 50 2007-02-03 20:26:19Z austin $
@@ -0,0 +1,20 @@
1
+ == Installing Color
2
+
3
+ Color may be installed with:
4
+
5
+ % ruby setup.rb
6
+
7
+ Alternatively, the RubyGems version of Color may be installed from the usual
8
+ sources.
9
+
10
+ == Copyright
11
+ Color
12
+ Colour Management with Ruby
13
+ http://rubyforge.org/projects/color
14
+
15
+ Licensed under a MIT-style licence. See Licence.txt in the main
16
+ distribution for full licensing information.
17
+
18
+ Copyright (c) 2005 - 2007 Austin Ziegler and Matt Lyon
19
+
20
+ $Id: History.txt 50 2007-02-03 20:26:19Z austin $
@@ -0,0 +1,29 @@
1
+ Color
2
+ Colour management in Ruby
3
+ http://rubyforge.org/projects/color/
4
+
5
+ Copyright (c) 2005 - 2007 Austin Ziegler, Matt Lyon, and other contributors
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
8
+ this software and associated documentation files (the "Software"), to deal in
9
+ the Software without restriction, including without limitation the rights to
10
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
11
+ of the Software, and to permit persons to whom the Software is furnished to do
12
+ so, subject to the following conditions:
13
+
14
+ * The names of its contributors may not be used to endorse or promote
15
+ products derived from this software without specific prior written
16
+ permission.
17
+
18
+ The above copyright notice and this permission notice shall be included in all
19
+ copies or substantial portions of the Software.
20
+
21
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27
+ SOFTWARE.
28
+
29
+ $Id: History.txt 50 2007-02-03 20:26:19Z austin $
@@ -0,0 +1,31 @@
1
+ History.txt
2
+ Install.txt
3
+ Licence.txt
4
+ Manifest.txt
5
+ Rakefile
6
+ Readme.txt
7
+ lib/color.rb
8
+ lib/color/cmyk.rb
9
+ lib/color/css.rb
10
+ lib/color/grayscale.rb
11
+ lib/color/hsl.rb
12
+ lib/color/palette.rb
13
+ lib/color/palette/adobecolor.rb
14
+ lib/color/palette/gimp.rb
15
+ lib/color/palette/monocontrast.rb
16
+ lib/color/rgb-colors.rb
17
+ lib/color/rgb.rb
18
+ lib/color/rgb/metallic.rb
19
+ lib/color/yiq.rb
20
+ setup.rb
21
+ test/test_adobecolor.rb
22
+ test/test_all.rb
23
+ test/test_cmyk.rb
24
+ test/test_color.rb
25
+ test/test_css.rb
26
+ test/test_gimp.rb
27
+ test/test_grayscale.rb
28
+ test/test_hsl.rb
29
+ test/test_monocontrast.rb
30
+ test/test_rgb.rb
31
+ test/test_yiq.rb
data/Rakefile CHANGED
@@ -1,53 +1,116 @@
1
- require 'rake'
2
- require 'rake/gempackagetask'
3
- # require 'rake/rdoctask'
4
- require 'rake/testtask'
1
+ #! /usr/bin/env rake
2
+ #--
3
+ # Color
4
+ # Colour Management with Ruby
5
+ # http://rubyforge.org/projects/color
6
+ #
7
+ # Licensed under a MIT-style licence. See Licence.txt in the main
8
+ # distribution for full licensing information.
9
+ #
10
+ # Copyright (c) 2005 - 2007 Austin Ziegler and Matt Lyon
11
+ #
12
+ # $Id: History.txt 50 2007-02-03 20:26:19Z austin $
13
+ #++
14
+
5
15
  require 'rubygems'
16
+ require 'hoe'
17
+
18
+ $LOAD_PATH.unshift('lib')
6
19
 
7
- $:.unshift(File.dirname(__FILE__) + "/lib")
8
20
  require 'color'
9
21
 
10
- PKG_NAME = 'color'
11
- PKG_VERSION = Color::VERSION
12
- PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
22
+ PKG_NAME = 'color'
23
+ PKG_VERSION = Color::COLOR_VERSION
24
+ PKG_DIST = "#{PKG_NAME}-#{PKG_VERSION}"
25
+ PKG_TAR = "pkg/#{PKG_DIST}.tar.gz"
26
+ MANIFEST = File.read("Manifest.txt").split
27
+
28
+ Hoe.new PKG_NAME, PKG_VERSION do |p|
29
+ p.rubyforge_name = PKG_NAME
30
+ # This is a lie becasue I will continue to use Archive::Tar::Minitar.
31
+ p.need_tar = false
32
+ # need_zip - Should package create a zipfile? [default: false]
13
33
 
14
- RELEASE_NAME = "REL #{PKG_VERSION}"
34
+ p.author = [ "Austin Ziegler", "Matt Lyon" ]
35
+ p.email = %W(austin@rubyforge.org matt@postsomnia.com)
36
+ p.url = "http://color.rubyforge.org/"
37
+ p.summary = "Colour management with Ruby"
38
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
39
+ p.description = p.paragraphs_of("Readme.txt", 1..1).join("\n\n")
15
40
 
16
- RUBY_FORGE_PROJECT = "color"
17
- RUBY_FORGE_USER = "mly"
41
+ p.extra_deps << [ "archive-tar-minitar", "~>0.5.1" ]
18
42
 
19
- desc "Default Task"
20
- task :default => [ :test ]
43
+ p.clean_globs << "coverage"
21
44
 
22
- Rake::TestTask.new do |t|
23
- t.test_files = FileList['test/*_test.rb']
24
- t.warning = true
25
- t.verbose = false
45
+ p.spec_extras[:extra_rdoc_files] = MANIFEST.grep(/txt$/) -
46
+ ["Manifest.txt"]
26
47
  end
27
48
 
28
- spec = Gem::Specification.new do |s|
29
- s.platform = Gem::Platform::RUBY
30
- s.name = PKG_NAME
31
- s.summary = "A Simply Library for Color Manipulation"
32
- s.description = %q{Manipulate Colors Programatically}
33
- s.version = PKG_VERSION
34
-
35
- s.author = "Matt Lyon"
36
- s.email = "matt@postsomnia.com"
37
- s.rubyforge_project = RUBY_FORGE_PROJECT
38
- s.homepage = ""
39
-
40
- s.has_rdoc = false
41
- s.require_path = 'lib'
42
- s.autorequire = 'color'
43
-
44
- s.files = [ "Rakefile", "README", "CHANGELOG", "LICENSE" ]
45
- s.files = s.files + Dir.glob("lib/**/*").delete_if {|item| item.include?("\.svn")}
46
- s.files = s.files + Dir.glob("test/**/*").delete_if {|item| item.include?("\.svn")}
49
+ desc "Build a Color .tar.gz distribution."
50
+ task :tar => [ PKG_TAR ]
51
+ file PKG_TAR => [ :test ] do |t|
52
+ require 'archive/tar/minitar'
53
+ require 'zlib'
54
+ files = MANIFEST.map { |f|
55
+ fn = File.join(PKG_DIST, f)
56
+ tm = File.stat(f).mtime
57
+
58
+ if File.directory?(f)
59
+ { :name => fn, :mode => 0755, :dir => true, :mtime => tm }
60
+ else
61
+ mode = if f =~ %r{^bin}
62
+ 0755
63
+ else
64
+ 0644
65
+ end
66
+ data = File.read(f)
67
+ { :name => fn, :mode => mode, :data => data, :size => data.size,
68
+ :mtime => tm }
69
+ end
70
+ }
71
+
72
+ begin
73
+ unless File.directory?(File.dirname(t.name))
74
+ require 'fileutils'
75
+ File.mkdir_p File.dirname(t.name)
76
+ end
77
+ tf = File.open(t.name, 'wb')
78
+ gz = Zlib::GzipWriter.new(tf)
79
+ tw = Archive::Tar::Minitar::Writer.new(gz)
80
+
81
+ files.each do |entry|
82
+ if entry[:dir]
83
+ tw.mkdir(entry[:name], entry)
84
+ else
85
+ tw.add_file_simple(entry[:name], entry) { |os|
86
+ os.write(entry[:data])
87
+ }
88
+ end
89
+ end
90
+ ensure
91
+ tw.close if tw
92
+ gz.close if gz
93
+ end
47
94
  end
95
+ task :package => [ PKG_TAR ]
96
+
97
+ desc "Build the manifest file from the current set of files."
98
+ task :build_manifest do |t|
99
+ require 'find'
100
+
101
+ paths = []
102
+ Find.find(".") do |path|
103
+ next if File.directory?(path)
104
+ next if path =~ /\.svn/
105
+ next if path =~ /\.swp$/
106
+ next if path =~ %r{coverage/}
107
+ next if path =~ /~$/
108
+ paths << path.sub(%r{^\./}, '')
109
+ end
48
110
 
49
- Rake::GemPackageTask.new(spec) do |p|
50
- p.gem_spec = spec
51
- p.need_tar = true
52
- p.need_zip = true
53
- end
111
+ File.open("Manifest.txt", "w") do |f|
112
+ f.puts paths.sort.join("\n")
113
+ end
114
+
115
+ puts paths.sort.join("\n")
116
+ end
@@ -0,0 +1,33 @@
1
+ = Color
2
+ Color is a Ruby library to provide basic RGB, CMYK, HSL, and other colourspace
3
+ manipulation support to applications that require it. It also provides 152
4
+ named RGB colours (184 with spelling variations) that are commonly supported
5
+ in HTML, SVG, and X11 applications. A technique for generating monochromatic
6
+ contrasting palettes is also included.
7
+
8
+ The capabilities of the Color library are limited to pure mathematical
9
+ manipulation of the colours based on colour theory without reference to colour
10
+ profiles (such as sRGB or Adobe RGB). For most purposes, when working with the
11
+ RGB and HSL colours, this won't matter. However, some colour models (like CIE
12
+ L*a*b*) are not supported because Color does not yet support colour profiles,
13
+ giving no meaningful way to convert colours in absolute colour spaces (like
14
+ L*a*b*, XYZ) to non-absolute colour spaces (like RGB).
15
+
16
+ Color version 1.4 is the result of a project merge between color.rb 0.1.0 by
17
+ Matt Lyon and color-tools 1.3 by Austin Ziegler. Please see History.txt for
18
+ details on the changes this merge brings.
19
+
20
+ Copyright:: Copyright (c) 2005 - 2007 by Austin Ziegler and Matt Lyon
21
+ Version:: 1.4.0
22
+ Homepage:: http://rubyforge.org/projects/color/
23
+ Licence:: MIT-Style; see Licence.txt
24
+
25
+ Color::Palette was developed based on techniques described by Andy "Malarkey"
26
+ Clarke[1], implemented in JavaScript by Steve G. Chipman at SlayerOffice[2] and
27
+ by Patrick Fitzgerald of BarelyFitz[3] in PHP.
28
+
29
+ [1] http://www.stuffandnonsense.co.uk/archives/creating_colour_palettes.html
30
+ [2] http://slayeroffice.com/tools/color_palette/
31
+ [3] http://www.barelyfitz.com/projects/csscolor/
32
+
33
+ $Id: Readme.txt 50 2007-02-03 20:26:19Z austin $
@@ -1,202 +1,147 @@
1
- class Color
2
-
3
- VERSION = "0.1.0"
4
-
5
- # attr_accessor :rgb
6
- attr_accessor :hue
7
- attr_accessor :saturation
8
- attr_accessor :lightness
9
-
10
- def initialize(values, mode=:rgb)
11
- rgb = case mode
12
- when :hsl
13
- Color.hsl_to_rgb(values)
14
- when :rgb
15
- values = [values].flatten
16
- case values.size
17
- when 1
18
- Color.rgbhex_to_rgb(values.first)
19
- else
20
- values
21
- end
22
- when :cmyk
23
- Color.cmyk_to_rgb(values)
24
- end
25
- rgb.collect! {|n| n.coerce(0).max.coerce(255).min }
26
- @hue, @saturation, @lightness = Color.rgb_to_hsl(rgb)
27
- end
28
-
29
- def hue=(value)
30
- value = value.abs >= 1 ? (value/360.0) : value
31
- @hue = value
32
- @hue += 1 if @hue < 0 # color is _perceived_ as a wheel, not a continuum
33
- @hue -= 1 if @hue > 1
34
- end
35
-
36
- def hue
37
- (@hue * 360).round
38
- end
39
-
40
- def saturation
41
- (@saturation * 100).round
42
- end
43
-
44
- def saturation=(value)
45
- @saturation = value.abs >= 1 ? (value / 100.0) : value
46
- @saturation = @saturation.coerce(0.0).max.coerce(1.0).min
47
- end
48
-
49
- def lightness
50
- (@lightness * 100).round
51
- end
52
-
53
- def lightness=(value)
54
- @lightness = value.abs >= 1 ? (value / 100.0) : value
55
- @lightness = @lightness.coerce(0.0).max.coerce(1.0).min
56
- end
57
-
58
- def mix_with(color, percent_as_decimal=0.5)
59
- target = color.to_hsl
60
- deltas = []
61
- self.hsl.each_with_index {|val, i| deltas[i] = target[i] - val }
62
- deltas.collect! {|n| n * percent_as_decimal }
63
- new_color = []
64
- deltas.each_with_index {|val, i| new_color[i] = val + self.hsl[i] }
65
- Color.new(new_color, :hsl)
66
- end
67
-
68
- def hsl
69
- [@hue, @saturation, @lightness]
70
- end
71
-
72
- def to_hsl
73
- [(@hue * 360.0).round, (@saturation * 100.0).round, (@lightness * 100.0).round]
74
- end
75
-
76
- def to_rgb
77
- Color.hsl_to_rgb(hsl)
78
- end
79
-
80
- def to_hex
81
- Color.rgb_to_rgbhex(@rgb)
82
- end
83
-
84
- def to_cmyk
85
- Color.rgb_to_cmyk(@rgb)
86
- end
87
-
1
+ # :title: Color -- Colour Management with Ruby
2
+ # :main: Readme.txt
3
+
4
+ #--
5
+ # Color
6
+ # Colour management with Ruby
7
+ # http://rubyforge.org/projects/color
8
+ # Version 1.4.0
9
+ #
10
+ # Licensed under a MIT-style licence. See Licence.txt in the main
11
+ # distribution for full licensing information.
12
+ #
13
+ # Copyright (c) 2005 - 2007 Austin Ziegler and Matt Lyon
14
+ #
15
+ # $Id: test_all.rb 55 2007-02-03 23:29:34Z austin $
16
+ #++
17
+
18
+ # = Colour Management with Ruby
19
+ module Color
20
+ COLOR_VERSION = '1.4.0'
21
+
22
+ class RGB; end
23
+ class CMYK; end
24
+ class GrayScale; end
25
+ class YIQ; end
26
+
27
+ # The maximum "resolution" for colour math; if any value is less than or
28
+ # equal to this value, it is treated as zero.
29
+ COLOR_EPSILON = 1e-5
30
+ # The tolerance for comparing the components of two colours. In general,
31
+ # colours are considered equal if all of their components are within this
32
+ # tolerance value of each other.
33
+ COLOR_TOLERANCE = 1e-4
34
+
88
35
  class << self
89
-
90
- def rgb_to_rgbhex(rgb=[])
91
- '#' + rgb.collect {|n| "%02X" % n }.join('').downcase
36
+ # Returns +true+ if the value is less than COLOR_EPSILON.
37
+ def near_zero?(value)
38
+ (value.abs <= COLOR_EPSILON)
92
39
  end
93
-
94
- def rgbhex_to_rgb(value)
95
- rg, blue = value.sub(/#/,'').hex.divmod(256)
96
- red, green = rg.divmod(256)
97
- return red, green, blue
40
+
41
+ # Returns +true+ if the value is within COLOR_EPSILON of zero or less than
42
+ # zero.
43
+ def near_zero_or_less?(value)
44
+ (value < 0.0 or near_zero?(value))
98
45
  end
99
-
100
- # conversion formulas from http://www.easyrgb.com/math.php
101
- # They're on Wikipedia too. And elsewhere, really, but the easyrbg ones
102
- # were most easily translatable to ruby.
103
- #
104
- # What's the difference between HSL and HSV? Quite a bit.
105
- # Wikipedia knows all:
106
- # http://en.wikipedia.org/wiki/HSV_color_space
107
- # http://en.wikipedia.org/wiki/HSL_color_space
108
- #
109
- # Generally, HSL has a more decoupled notion of saturation and lightness
110
- # whereas with HSV saturation and value are coupled in such a way as to
111
- # not behave intuitively. HSV is a bit more mathematically straightforward.
112
- #
113
- # Since the author of this library is an artist and has a personal bias
114
- # towards HSL, this library reflects that bias and uses HSL internally.
115
- # HSL is also part of the CSS3 Color Module, if I haven't convinced you yet.
116
-
117
- def rgb_to_hsl(rgb=[])
118
- r, g, b, min, max, delta = rgb_to_values(rgb)
119
- lightness = (max + min) / 2.0
120
- return [0,0,lightness] if delta.zero? # gray, no chroma
121
- saturation = (lightness < 0.5) ? (delta / (max + min)) : (delta / (2.0 - max - min))
122
- hue = figure_hue_for(rgb)
123
- return hue, saturation, lightness
46
+
47
+ # Returns +true+ if the value is within COLOR_EPSILON of one.
48
+ def near_one?(value)
49
+ near_zero?(value - 1.0)
124
50
  end
125
-
126
- def rgb_to_hsl_human(rgb=[])
127
- h,s,l = Color.rgb_to_hsl(rgb)
128
- return (h * 360).round, (s * 100).round, (l * 100).round
51
+
52
+ # Returns +true+ if the value is within COLOR_EPSILON of one or more than
53
+ # one.
54
+ def near_one_or_more?(value)
55
+ (value > 1.0 or near_one?(value))
129
56
  end
130
-
131
- def hsl_to_rgb(hsl=[])
132
- h, s, l = hsl
133
- if h > 1 || s > 1 || l > 1
134
- h = h / 360.0
135
- s = s * 0.01
136
- l = l * 0.01
137
- end
138
- return [l,l,l].collect {|l| (l * 255.0).round } if s.zero? # gray, no chroma
139
- v2 = (l < 0.5) ? (l * (1 + s)) : (l + s) - (s * l)
140
- v1 = 2.0 * l - v2
141
- rgb = [h+(1/3.0), h, h-(1/3.0)]
142
- rgb.collect! do |hue|
143
- hue += 1 if hue < 0
144
- hue -= 1 if hue > 1
145
- if (6 * hue) < 1
146
- v1 + (v2 - v1) * 6 * hue
147
- elsif (2 * hue) < 1
148
- v2
149
- elsif (3 * hue) < 2
150
- v1 + (v2 - v1) * ((2 / 3.0) - hue) * 6
151
- else
152
- v1
153
- end
57
+
58
+ # Normalizes the value to the range (0.0) .. (1.0).
59
+ def normalize(value)
60
+ if near_zero_or_less? value
61
+ 0.0
62
+ elsif near_one_or_more? value
63
+ 1.0
64
+ else
65
+ value
154
66
  end
155
- return rgb.collect {|n| (n * 255.0).round }
156
67
  end
157
-
158
- # def rgb_to_hsv(rgb=[])
159
- # r, g, b, min, max, delta = rgb_to_values(rgb)
160
- # value = max
161
- # return [0.0, 0.0, value] if delta.zero? # gray, no chroma
162
- # saturation = delta / max
163
- # hue = figure_hue_for(rgb)
164
- # return hue, saturation, value
165
- # end
166
-
167
- def rgb_to_cmyk(rgb=[])
168
- cmy = rgb.collect {|n| 1 - (n / 255.0)}
169
- k = cmy.min
170
- return [0.0,0.0,0.0,1.0] if k == 1.0 # black
171
- cmy.collect {|n| (n - k) / (1 - k) } << k
68
+ alias normalize_fractional normalize
69
+
70
+ def normalize_to_range(value, range)
71
+ range = (range.end..range.begin) if (range.end < range.begin)
72
+
73
+ if value <= range.begin
74
+ range.begin
75
+ elsif value >= range.end
76
+ range.end
77
+ else
78
+ value
79
+ end
172
80
  end
173
-
174
- def cmyk_to_rgb(cmyk=[])
175
- c,m,y,k = cmyk
176
- [c,m,y].collect {|n| ((1 - (n * (1 - k) + k)) * 255.0).to_i }
81
+
82
+ # Normalize the value to the range (0) .. (255).
83
+ def normalize_byte(value)
84
+ normalize_to_range(value, 0..255).to_i
177
85
  end
86
+ alias normalize_8bit normalize_byte
178
87
 
179
- private
180
- def rgb_to_values(rgb=[])
181
- rgb.collect! {|n| n / 255.0 }
182
- rgb << rgb.min << rgb.max << rgb.max - rgb.min
88
+ # Normalize the value to the range (0) .. (65535).
89
+ def normalize_word(value)
90
+ normalize_to_range(value, 0..65535).to_i
183
91
  end
184
-
185
- def figure_hue_for(rgb=[])
186
- r, g, b, min, max, delta = rgb_to_values(rgb)
187
- delta_r, delta_g, delta_b = [r,g,b].collect {|n| (((max - n) / 6.0) + (delta / 2.0)) / delta }
188
- hue = if r.eql? max
189
- delta_b - delta_g
190
- elsif g.eql? max
191
- (1 / 3.0) + delta_r - delta_b
192
- else # blue
193
- (2 / 3.0) + delta_g - delta_r
92
+ alias normalize_16bit normalize_word
93
+ end
94
+ end
95
+
96
+ require 'color/rgb'
97
+ require 'color/cmyk'
98
+ require 'color/grayscale'
99
+ require 'color/hsl'
100
+ require 'color/yiq'
101
+ require 'color/rgb/metallic'
102
+
103
+ module Color
104
+ def self.const_missing(name) #:nodoc:
105
+ case name
106
+ when "VERSION", :VERSION, "COLOR_TOOLS_VERSION", :COLOR_TOOLS_VERSION
107
+ warn "Color::#{name} has been deprecated. Use Color::COLOR_VERSION instead."
108
+ Color::COLOR_VERSION
109
+ else
110
+ if Color::RGB.const_defined?(name)
111
+ warn "Color::#{name} has been deprecated. Use Color::RGB::#{name} instead."
112
+ Color::RGB.const_get(name)
113
+ else
114
+ super
194
115
  end
195
- hue += 1 if hue < 0
196
- hue -= 1 if hue > 1
197
- hue
198
116
  end
199
-
200
117
  end
201
-
202
- end
118
+
119
+ # Provides a thin veneer over the Color module to make it seem like this
120
+ # is Color 0.1.0 (a class) and not Color 1.4.0 (a module). This
121
+ # "constructor" will be removed in the future.
122
+ #
123
+ # mode = :hsl:: +values+ must be an array of [ hue deg, sat %, lum % ].
124
+ # A Color::HSL object will be created.
125
+ # mode = :rgb:: +values+ will either be an HTML-style colour string or
126
+ # an array of [ red, green, blue ] (range 0 .. 255). A
127
+ # Color::RGB object will be created.
128
+ # mode = :cmyk:: +values+ must be an array of [ cyan %, magenta %, yellow
129
+ # %, black % ]. A Color::CMYK object will be created.
130
+ def self.new(values, mode = :rgb)
131
+ warn "Color.new has been deprecated. Use Color::#{mode.to_s.upcase}.new instead."
132
+ color = case mode
133
+ when :hsl
134
+ Color::HSL.new(*values)
135
+ when :rgb
136
+ values = [ values ].flatten
137
+ if values.size == 1
138
+ Color::RGB.from_html(*values)
139
+ else
140
+ Color::RGB.new(*values)
141
+ end
142
+ when :cmyk
143
+ Color::CMYK.new(*values)
144
+ end
145
+ color.to_hsl
146
+ end
147
+ end