color 0.1.0 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data.tar.gz.sig +0 -0
- data/History.txt +91 -0
- data/Install.txt +20 -0
- data/Licence.txt +29 -0
- data/Manifest.txt +31 -0
- data/Rakefile +104 -41
- data/Readme.txt +33 -0
- data/lib/color.rb +133 -188
- data/lib/color/cmyk.rb +281 -0
- data/lib/color/css.rb +30 -0
- data/lib/color/grayscale.rb +214 -0
- data/lib/color/hsl.rb +223 -0
- data/lib/color/palette.rb +18 -0
- data/lib/color/palette/adobecolor.rb +274 -0
- data/lib/color/palette/gimp.rb +118 -0
- data/lib/color/palette/monocontrast.rb +182 -0
- data/lib/color/rgb-colors.rb +357 -0
- data/lib/color/rgb.rb +455 -0
- data/lib/color/rgb/metallic.rb +45 -0
- data/lib/color/yiq.rb +86 -0
- data/setup.rb +1585 -0
- data/test/test_adobecolor.rb +421 -0
- data/test/test_all.rb +25 -0
- data/test/test_cmyk.rb +130 -0
- data/test/test_color.rb +123 -120
- data/test/test_css.rb +31 -0
- data/test/test_gimp.rb +103 -0
- data/test/test_grayscale.rb +123 -0
- data/test/test_hsl.rb +155 -0
- data/test/test_monocontrast.rb +144 -0
- data/test/test_rgb.rb +346 -0
- data/test/test_yiq.rb +75 -0
- metadata +92 -20
- metadata.gz.sig +3 -0
- data/CHANGELOG +0 -4
- data/LICENSE +0 -7
- data/README +0 -41
data.tar.gz.sig
ADDED
Binary file
|
data/History.txt
ADDED
@@ -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 $
|
data/Install.txt
ADDED
@@ -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 $
|
data/Licence.txt
ADDED
@@ -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 $
|
data/Manifest.txt
ADDED
@@ -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
|
-
|
2
|
-
|
3
|
-
#
|
4
|
-
|
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
|
11
|
-
PKG_VERSION
|
12
|
-
|
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
|
-
|
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
|
-
|
17
|
-
RUBY_FORGE_USER = "mly"
|
41
|
+
p.extra_deps << [ "archive-tar-minitar", "~>0.5.1" ]
|
18
42
|
|
19
|
-
|
20
|
-
task :default => [ :test ]
|
43
|
+
p.clean_globs << "coverage"
|
21
44
|
|
22
|
-
|
23
|
-
|
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
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
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
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
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
|
data/Readme.txt
ADDED
@@ -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 $
|
data/lib/color.rb
CHANGED
@@ -1,202 +1,147 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
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
|
91
|
-
|
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
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
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
|
-
#
|
101
|
-
|
102
|
-
|
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
|
-
|
127
|
-
|
128
|
-
|
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
|
-
|
132
|
-
|
133
|
-
if
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
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
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
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
|
-
|
175
|
-
|
176
|
-
|
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
|
-
|
180
|
-
def
|
181
|
-
|
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
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
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
|
-
|
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
|