color-tools 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2 @@
1
+ == color-utils 1.0.0
2
+ * Initial release.
data/Install ADDED
@@ -0,0 +1,6 @@
1
+ Installing this package is as simple as:
2
+
3
+ % ruby install.rb
4
+
5
+ Alternatively, you can use the RubyGem version of color-tools available as
6
+ color-tools-1.0.0.gem from the usual sources.
data/README ADDED
@@ -0,0 +1,46 @@
1
+ color-tools README
2
+ ==================
3
+ color-tools is a Ruby library to provide RGB and CMYK colour support to
4
+ applications that require it. It provides 148 named RGB colours that are
5
+ commonly supported and used in HTML, colour manipulation operations, and
6
+ a monochromatic contrasting palette generator.
7
+
8
+ Copyright
9
+ =========
10
+ Copyright 2005 by Austin Ziegler
11
+
12
+ Color::RGB and Color::CMYK were originally developed for the Ruby PDF
13
+ project and PDF::Writer and represent wholly unique code.
14
+
15
+ Color::Palette was developed based on techniques described by Andy
16
+ "Malarkey" Clarke[1], implemented in JavaScript by Steve G. Chipman at
17
+ SlayerOffice[2] and by Patrick Fitzgerald of BarelyFitz[3] in PHP.
18
+
19
+ Licence
20
+ =======
21
+ Permission is hereby granted, free of charge, to any person obtaining a
22
+ copy of this software and associated documentation files (the "Soft-
23
+ ware"), to deal in the Software without restriction, including without
24
+ limitation the rights to use, copy, modify, merge, publish, distribute,
25
+ sublicense, and/or sell copies of the Software, and to permit persons to
26
+ whom the Software is furnished to do so, subject to the following
27
+ conditions:
28
+
29
+ * The names of its contributors may not be used to endorse or promote
30
+ products derived from this software without specific prior written
31
+ permission.
32
+
33
+ The above copyright notice and this permission notice shall be included
34
+ in all copies or substantial portions of the Software.
35
+
36
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
37
+ OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
38
+ ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
39
+ SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
40
+ OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
41
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
42
+ OTHER DEALINGS IN THE SOFTWARE.
43
+
44
+ [1] http://www.stuffandnonsense.co.uk/archives/creating_colour_palettes.html
45
+ [2] http://slayeroffice.com/tools/color_palette/
46
+ [3] http://www.barelyfitz.com/projects/csscolor/
@@ -0,0 +1,116 @@
1
+ #! /usr/bin/env rake
2
+ $LOAD_PATH.unshift('lib')
3
+
4
+ require 'rubygems'
5
+ require 'rake/gempackagetask'
6
+ require 'color'
7
+ require 'archive/tar/minitar'
8
+ require 'zlib'
9
+
10
+ DISTDIR = "color-tools-#{Color::COLOR_TOOLS_VERSION}"
11
+ TARDIST = "../#{DISTDIR}.tar.gz"
12
+
13
+ DATE_RE = %r<(\d{4})[./-]?(\d{2})[./-]?(\d{2})(?:[\sT]?(\d{2})[:.]?(\d{2})[:.]?(\d{2})?)?>
14
+
15
+ if ENV['RELEASE_DATE']
16
+ year, month, day, hour, minute, second = DATE_RE.match(ENV['RELEASE_DATE']).captures
17
+ year ||= 0
18
+ month ||= 0
19
+ day ||= 0
20
+ hour ||= 0
21
+ minute ||= 0
22
+ second ||= 0
23
+ ReleaseDate = Time.mktime(year, month, day, hour, minute, second)
24
+ else
25
+ ReleaseDate = nil
26
+ end
27
+
28
+ task :test do |t|
29
+ require 'test/unit/testsuite'
30
+ require 'test/unit/ui/console/testrunner'
31
+
32
+ runner = Test::Unit::UI::Console::TestRunner
33
+
34
+ $LOAD_PATH.unshift('tests')
35
+ $stderr.puts "Checking for test cases:" if t.verbose
36
+ Dir['tests/tc_*.rb'].each do |testcase|
37
+ $stderr.puts "\t#{testcase}" if t.verbose
38
+ load testcase
39
+ end
40
+
41
+ suite = Test::Unit::TestSuite.new("color-tools")
42
+
43
+ ObjectSpace.each_object(Class) do |testcase|
44
+ suite << testcase.suite if testcase < Test::Unit::TestCase
45
+ end
46
+
47
+ runner.run(suite)
48
+ end
49
+
50
+ spec = eval(File.read("color-tools.gemspec"))
51
+ spec.version = Color::COLOR_TOOLS_VERSION
52
+ desc "Build the RubyGem for color-tools"
53
+ task :gem => [ :test ]
54
+ Rake::GemPackageTask.new(spec) do |g|
55
+ g.need_tar = false
56
+ g.need_zip = false
57
+ g.package_dir = ".."
58
+ end
59
+
60
+ desc "Build a color-tools .tar.gz distribution."
61
+ task :tar => [ TARDIST ]
62
+ file TARDIST => [ :test ] do |t|
63
+ current = File.basename(Dir.pwd)
64
+ Dir.chdir("..") do
65
+ begin
66
+ files = Dir["#{current}/**/*"].select { |dd| dd !~ %r{(?:/CVS/?|~$)} }
67
+ files.map! do |dd|
68
+ ddnew = dd.gsub(/^#{current}/, DISTDIR)
69
+ mtime = ReleaseDate || File.stat(dd).mtime
70
+ if File.directory?(dd)
71
+ { :name => ddnew, :mode => 0755, :dir => true, :mtime => mtime }
72
+ else
73
+ if dd =~ %r{bin/}
74
+ mode = 0755
75
+ else
76
+ mode = 0644
77
+ end
78
+ data = File.read(dd)
79
+ { :name => ddnew, :mode => mode, :data => data, :size => data.size,
80
+ :mtime => mtime }
81
+ end
82
+ end
83
+
84
+ ff = File.open(t.name.gsub(%r{^\.\./}o, ''), "wb")
85
+ gz = Zlib::GzipWriter.new(ff)
86
+ tw = Archive::Tar::Minitar::Writer.new(gz)
87
+
88
+ files.each do |entry|
89
+ if entry[:dir]
90
+ tw.mkdir(entry[:name], entry)
91
+ else
92
+ tw.add_file_simple(entry[:name], entry) { |os| os.write(entry[:data]) }
93
+ end
94
+ end
95
+ ensure
96
+ tw.close if tw
97
+ gz.close if gz
98
+ end
99
+ end
100
+ end
101
+ task TARDIST => [ :test ]
102
+
103
+ def sign(file)
104
+ system %("C:/Apps/GnuPG/gpg.exe" -ba #{file}).gsub(%r{/}) { "\\" }
105
+ raise "Error signing with GPG." unless File.exists?("#{file}.asc")
106
+ end
107
+
108
+ task :signtar => [ :tar ] do
109
+ sign TARDIST
110
+ end
111
+ task :signgem => [ :gem ] do
112
+ sign "../#{DISTDIR}.gem"
113
+ end
114
+
115
+ desc "Build everything."
116
+ task :default => [ :signtar, :signgem ]
@@ -0,0 +1,201 @@
1
+ # = Colour Management with Ruby
2
+ #
3
+ # == Copyright
4
+ # Copyright 2005 by Austin Ziegler
5
+ #
6
+ # Color::RGB and Color::CMYK were originally developed for the Ruby PDF
7
+ # project and PDF::Writer and represent wholly unique code.
8
+ #
9
+ # Color::Palette was developed based on techniques described by Andy
10
+ # "Malarkey"[http://www.stuffandnonsense.co.uk/archives/creating_colour_palettes.html]
11
+ # Clarke, implemented in JavaScript by Steve G. Chipman at
12
+ # SlayerOffice[http://slayeroffice.com/tools/color_palette/] and by Patrick
13
+ # Fitzgerald of BarelyFitz[http://www.barelyfitz.com/projects/csscolor/] in
14
+ # PHP.
15
+ #
16
+ # == LICENCE
17
+ # Permission is hereby granted, free of charge, to any person obtaining a
18
+ # copy of this software and associated documentation files (the "Software"),
19
+ # to deal in the Software without restriction, including without limitation
20
+ # the rights to use, copy, modify, merge, publish, distribute, sublicense,
21
+ # and/or sell copies of the Software, and to permit persons to whom the
22
+ # Software is furnished to do so, subject to the following conditions:
23
+ #
24
+ # * The names of its contributors may not be used to endorse or promote
25
+ # products derived from this software without specific prior written
26
+ # permission.
27
+ #
28
+ # The above copyright notice and this permission notice shall be included in
29
+ # all copies or substantial portions of the Software.
30
+ #
31
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
34
+ # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
36
+ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
37
+ # DEALINGS IN THE SOFTWARE.
38
+
39
+ require 'color/rgb'
40
+ require 'color/cmyk'
41
+
42
+ module Color
43
+ COLOR_TOOLS_VERSION = '1.0.0'
44
+
45
+ Black = Color::RGB.new
46
+ Navy = Color::RGB.new(0x00, 0x00, 0x80)
47
+ DarkBlue = Color::RGB.new(0x00, 0x00, 0x8b)
48
+ MediumBlue = Color::RGB.new(0x00, 0x00, 0xcd)
49
+ Blue = Color::RGB.new(0x00, 0x00, 0xff)
50
+ DarkGreen = Color::RGB.new(0x00, 0x64, 0x00)
51
+ Green = Color::RGB.new(0x00, 0x80, 0x00)
52
+ Teal = Color::RGB.new(0x00, 0x80, 0x80)
53
+ DarkCyan = Color::RGB.new(0x00, 0x8b, 0x8b)
54
+ DeepSkyBlue = Color::RGB.new(0x00, 0xbf, 0xbf)
55
+ DarkTurquoise = Color::RGB.new(0x00, 0xce, 0xd1)
56
+ MediumSpringGreen = Color::RGB.new(0x00, 0xfa, 0x9a)
57
+ Lime = Color::RGB.new(0x00, 0xff, 0x00)
58
+ SpringGreen = Color::RGB.new(0x00, 0xff, 0x7f)
59
+ Aqua = Color::RGB.new(0x00, 0xff, 0xff)
60
+ Cyan = Color::RGB.new(0x00, 0xff, 0xff)
61
+ MidnightBlue = Color::RGB.new(0x19, 0x19, 0x70)
62
+ DodgerBlue = Color::RGB.new(0x1e, 0x90, 0xff)
63
+ LightSeaGreen = Color::RGB.new(0x20, 0xb2, 0xaa)
64
+ ForestGreen = Color::RGB.new(0x22, 0x8b, 0x22)
65
+ SeaGreen = Color::RGB.new(0x2e, 0x8b, 0x57)
66
+ DarkSlateGray = Color::RGB.new(0x2f, 0x4f, 0x4f)
67
+ DarkSlateGrey = DarkSlateGray
68
+ LimeGreen = Color::RGB.new(0x32, 0xcd, 0x32)
69
+ MediumSeaGreen = Color::RGB.new(0x3c, 0xb3, 0x71)
70
+ Turquoise = Color::RGB.new(0x40, 0xe0, 0xd0)
71
+ RoyalBlue = Color::RGB.new(0x41, 0x69, 0xe1)
72
+ SteelBlue = Color::RGB.new(0x46, 0x82, 0xb4)
73
+ DarkSlateBlue = Color::RGB.new(0x48, 0x3d, 0x8b)
74
+ MediumTurquoise = Color::RGB.new(0x48, 0xd1, 0xcc)
75
+ Indigo = Color::RGB.new(0x4b, 0x00, 0x82)
76
+ DarkoliveGreen = Color::RGB.new(0x55, 0x6b, 0x2f)
77
+ CadetBlue = Color::RGB.new(0x5f, 0x9e, 0xa0)
78
+ CornflowerBlue = Color::RGB.new(0x64, 0x95, 0xed)
79
+ MediumAquamarine = Color::RGB.new(0x66, 0xcd, 0xaa)
80
+ DimGray = Color::RGB.new(0x69, 0x69, 0x69)
81
+ DimGrey = DimGray
82
+ SlateBlue = Color::RGB.new(0x6a, 0x5a, 0xcd)
83
+ Olivedrab = Color::RGB.new(0x6b, 0x8e, 0x23)
84
+ SlateGray = Color::RGB.new(0x70, 0x80, 0x90)
85
+ SlateGrey = SlateGray
86
+ LightSlateGray = Color::RGB.new(0x77, 0x88, 0x99)
87
+ LightSlateGrey = LightSlateGray
88
+ MediumSlateBlue = Color::RGB.new(0x7b, 0x68, 0xee)
89
+ LawnGreen = Color::RGB.new(0x7c, 0xfc, 0x00)
90
+ Chartreuse = Color::RGB.new(0x7f, 0xff, 0x00)
91
+ Aquamarine = Color::RGB.new(0x7f, 0xff, 0xd4)
92
+ Maroon = Color::RGB.new(0x80, 0x00, 0x00)
93
+ Purple = Color::RGB.new(0x80, 0x00, 0x80)
94
+ Olive = Color::RGB.new(0x80, 0x80, 0x00)
95
+ Gray = Color::RGB.new(0x80, 0x80, 0x80)
96
+ Grey = Gray
97
+ SkyBlue = Color::RGB.new(0x87, 0xce, 0xeb)
98
+ LightSkyBlue = Color::RGB.new(0x87, 0xce, 0xfa)
99
+ BlueViolet = Color::RGB.new(0x8a, 0x2b, 0xe2)
100
+ DarkRed = Color::RGB.new(0x8b, 0x00, 0x00)
101
+ DarkMagenta = Color::RGB.new(0x8b, 0x00, 0x8b)
102
+ SaddleBrown = Color::RGB.new(0x8b, 0x45, 0x13)
103
+ DarkSeaGreen = Color::RGB.new(0x8f, 0xbc, 0x8f)
104
+ LightGreen = Color::RGB.new(0x90, 0xee, 0x90)
105
+ MediumPurple = Color::RGB.new(0x93, 0x70, 0xdb)
106
+ DarkViolet = Color::RGB.new(0x94, 0x00, 0xd3)
107
+ PaleGreen = Color::RGB.new(0x98, 0xfb, 0x98)
108
+ DarkOrchid = Color::RGB.new(0x99, 0x32, 0xcc)
109
+ YellowGreen = Color::RGB.new(0x9a, 0xcd, 0x32)
110
+ Sienna = Color::RGB.new(0xa0, 0x52, 0x2d)
111
+ Brown = Color::RGB.new(0xa5, 0x2a, 0x2a)
112
+ DarkGray = Color::RGB.new(0xa9, 0xa9, 0xa9)
113
+ DarkGrey = DarkGray
114
+ LightBlue = Color::RGB.new(0xad, 0xd8, 0xe6)
115
+ GreenYellow = Color::RGB.new(0xad, 0xff, 0x2f)
116
+ PaleTurquoise = Color::RGB.new(0xaf, 0xee, 0xee)
117
+ LightsteelBlue = Color::RGB.new(0xb0, 0xc4, 0xde)
118
+ PowderBlue = Color::RGB.new(0xb0, 0xe0, 0xe6)
119
+ Firebrick = Color::RGB.new(0xb2, 0x22, 0x22)
120
+ DarkGoldenrod = Color::RGB.new(0xb8, 0x86, 0x0b)
121
+ MediumOrchid = Color::RGB.new(0xba, 0x55, 0xd3)
122
+ RosyBrown = Color::RGB.new(0xbc, 0x8f, 0x8f)
123
+ DarkKhaki = Color::RGB.new(0xbd, 0xb7, 0x6b)
124
+ Silver = Color::RGB.new(0xc0, 0xc0, 0xc0)
125
+ MediumVioletRed = Color::RGB.new(0xc7, 0x15, 0x85)
126
+ IndianRed = Color::RGB.new(0xcd, 0x5c, 0x5c)
127
+ Peru = Color::RGB.new(0xcd, 0x85, 0x3f)
128
+ Chocolate = Color::RGB.new(0xd2, 0x69, 0x1e)
129
+ Tan = Color::RGB.new(0xd2, 0xb4, 0x8c)
130
+ LightGray = Color::RGB.new(0xd3, 0xd3, 0xd3)
131
+ LightGrey = LightGray
132
+ Thistle = Color::RGB.new(0xd8, 0xbf, 0xd8)
133
+ Orchid = Color::RGB.new(0xda, 0x70, 0xd6)
134
+ Goldenrod = Color::RGB.new(0xda, 0xa5, 0x20)
135
+ PaleVioletRed = Color::RGB.new(0xdb, 0x70, 0x93)
136
+ Crimson = Color::RGB.new(0xdc, 0x14, 0x3c)
137
+ Gainsboro = Color::RGB.new(0xdc, 0xdc, 0xdc)
138
+ Plum = Color::RGB.new(0xdd, 0xa0, 0xdd)
139
+ Burlywood = Color::RGB.new(0xde, 0xb8, 0x87)
140
+ LightCyan = Color::RGB.new(0xe0, 0xff, 0xff)
141
+ Lavender = Color::RGB.new(0xe6, 0xe6, 0xfa)
142
+ Darksalmon = Color::RGB.new(0xe9, 0x96, 0x7a)
143
+ Violet = Color::RGB.new(0xee, 0x82, 0xee)
144
+ PaleGoldenrod = Color::RGB.new(0xee, 0xe8, 0xaa)
145
+ LightCoral = Color::RGB.new(0xf0, 0x80, 0x80)
146
+ Khaki = Color::RGB.new(0xf0, 0xe6, 0x8c)
147
+ AliceBlue = Color::RGB.new(0xf0, 0xf8, 0xff)
148
+ Honeydew = Color::RGB.new(0xf0, 0xff, 0xf0)
149
+ Azure = Color::RGB.new(0xf0, 0xff, 0xff)
150
+ SandyBrown = Color::RGB.new(0xf4, 0xa4, 0x60)
151
+ Wheat = Color::RGB.new(0xf5, 0xde, 0xb3)
152
+ Beige = Color::RGB.new(0xf5, 0xf5, 0xdc)
153
+ WhiteSmoke = Color::RGB.new(0xf5, 0xf5, 0xf5)
154
+ MintCream = Color::RGB.new(0xf5, 0xff, 0xfa)
155
+ GhostWhite = Color::RGB.new(0xf8, 0xf8, 0xff)
156
+ Salmon = Color::RGB.new(0xfa, 0x80, 0x72)
157
+ AntiqueWhite = Color::RGB.new(0xfa, 0xeb, 0xd7)
158
+ Linen = Color::RGB.new(0xfa, 0xf0, 0xe6)
159
+ LightGoldenrodYellow = Color::RGB.new(0xfa, 0xfa, 0xd2)
160
+ OldLace = Color::RGB.new(0xfd, 0xf5, 0xe6)
161
+ Red = Color::RGB.new(0xff, 0x00, 0x00)
162
+ Fuchsia = Color::RGB.new(0xff, 0x00, 0xff)
163
+ Magenta = Color::RGB.new(0xff, 0x00, 0xff)
164
+ DeepPink = Color::RGB.new(0xff, 0x14, 0x93)
165
+ OrangeRed = Color::RGB.new(0xff, 0x45, 0x00)
166
+ Tomato = Color::RGB.new(0xff, 0x63, 0x47)
167
+ HotPink = Color::RGB.new(0xff, 0x69, 0xb4)
168
+ Coral = Color::RGB.new(0xff, 0x7f, 0x50)
169
+ Darkorange = Color::RGB.new(0xff, 0x8c, 0x00)
170
+ Lightsalmon = Color::RGB.new(0xff, 0xa0, 0x7a)
171
+ Orange = Color::RGB.new(0xff, 0xa5, 0x00)
172
+ LightPink = Color::RGB.new(0xff, 0xb6, 0xc1)
173
+ Pink = Color::RGB.new(0xff, 0xc0, 0xcb)
174
+ Gold = Color::RGB.new(0xff, 0xd7, 0x00)
175
+ Peachpuff = Color::RGB.new(0xff, 0xda, 0xb9)
176
+ NavajoWhite = Color::RGB.new(0xff, 0xde, 0xad)
177
+ Moccasin = Color::RGB.new(0xff, 0xe4, 0xb5)
178
+ Bisque = Color::RGB.new(0xff, 0xe4, 0xc4)
179
+ MistyRose = Color::RGB.new(0xff, 0xe4, 0xe1)
180
+ BlanchedAlmond = Color::RGB.new(0xff, 0xeb, 0xcd)
181
+ PapayaWhip = Color::RGB.new(0xff, 0xef, 0xd5)
182
+ LavenderBlush = Color::RGB.new(0xff, 0xf0, 0xf5)
183
+ Seashell = Color::RGB.new(0xff, 0xf5, 0xee)
184
+ Cornsilk = Color::RGB.new(0xff, 0xf8, 0xdc)
185
+ LemonChiffon = Color::RGB.new(0xff, 0xfa, 0xcd)
186
+ FloralWhite = Color::RGB.new(0xff, 0xfa, 0xf0)
187
+ Snow = Color::RGB.new(0xff, 0xfa, 0xfa)
188
+ Yellow = Color::RGB.new(0xff, 0xff, 0x00)
189
+ LightYellow = Color::RGB.new(0xff, 0xff, 0xe0)
190
+ Ivory = Color::RGB.new(0xff, 0xff, 0xf0)
191
+ White = Color::RGB.new(0xff, 0xff, 0xff)
192
+ Gray10 = Grey10 = Color::RGB.from_percentage(10, 10, 10)
193
+ Gray20 = Grey20 = Color::RGB.from_percentage(20, 20, 20)
194
+ Gray30 = Grey30 = Color::RGB.from_percentage(30, 30, 30)
195
+ Gray40 = Grey40 = Color::RGB.from_percentage(40, 40, 40)
196
+ Gray50 = Grey50 = Color::RGB.from_percentage(50, 50, 50)
197
+ Gray60 = Grey60 = Color::RGB.from_percentage(60, 60, 60)
198
+ Gray70 = Grey70 = Color::RGB.from_percentage(70, 70, 70)
199
+ Gray80 = Grey80 = Color::RGB.from_percentage(80, 80, 80)
200
+ Gray90 = Grey90 = Color::RGB.from_percentage(90, 90, 90)
201
+ end
@@ -0,0 +1,73 @@
1
+ #--
2
+ # Colour management with Ruby.
3
+ #
4
+ # Copyright 2005 Austin Ziegler
5
+ # http://rubyforge.org/ruby-pdf/
6
+ #++
7
+
8
+ require 'color/rgb'
9
+
10
+ module Color
11
+ # An CMYK colour object.
12
+ class CMYK
13
+ PDF_FORMAT_STR = "%.3f %.3f %.3f %.3f %s"
14
+
15
+ def ==(other)
16
+ other = other.to_cmyk if other.kind_of?(Color::RGB)
17
+ other.kind_of?(Color::CMYK) and
18
+ (@c == other.c) and
19
+ (@m == other.m) and
20
+ (@y == other.y) and
21
+ (@k == other.k)
22
+ end
23
+
24
+ # Creates a CMYK colour object from fractional values 0 .. 1.
25
+ def self.from_fraction(c = 0, m = 0, y = 0, k = 0)
26
+ colour = Color::CMYK.new
27
+ colour.instance_variable_set(:@c, c)
28
+ colour.instance_variable_set(:@m, m)
29
+ colour.instance_variable_set(:@y, y)
30
+ colour.instance_variable_set(:@k, k)
31
+ colour
32
+ end
33
+
34
+ # Creates a CMYK colour object from percentages.
35
+ def initialize(c = 0, m = 0, y = 0, k = 0)
36
+ @c = c / 100.0
37
+ @m = m / 100.0
38
+ @y = y / 100.0
39
+ @k = k / 100.0
40
+ end
41
+
42
+ # Present the colour as a fill colour string for PDF.
43
+ def pdf_fill
44
+ PDF_FORMAT_STR % [ @c, @m, @y, @k, "k" ]
45
+ end
46
+
47
+ # Present the colour as a stroke colour string for PDF.
48
+ def pdf_stroke
49
+ PDF_FORMAT_STR % [ @c, @m, @y, @k, "K" ]
50
+ end
51
+
52
+ # Present the colour as an HTML/CSS colour string.
53
+ def html
54
+ to_rgb.html
55
+ end
56
+
57
+ # Convert the CMYK colour to RGB. Note that colour experts strongly
58
+ # suggest that this is a *bad* idea, as CMYK represents percentages of
59
+ # inks, not mixed colour intensities like RGB.
60
+ def to_rgb
61
+ r = 1.0 - (@c.to_f * (1.0 - @k.to_f) + @k.to_f)
62
+ g = 1.0 - (@m.to_f * (1.0 - @k.to_f) + @k.to_f)
63
+ b = 1.0 - (@y.to_f * (1.0 - @k.to_f) + @k.to_f)
64
+ Color::RGB.from_fraction(r, g, b)
65
+ end
66
+
67
+ def to_cmyk
68
+ self.dup
69
+ end
70
+
71
+ attr_accessor :c, :m, :y, :k
72
+ end
73
+ end
@@ -0,0 +1,11 @@
1
+ #--
2
+ # Colour management with Ruby.
3
+ #
4
+ # Copyright 2005 Austin Ziegler
5
+ # http://rubyforge.org/ruby-pdf/
6
+ #++
7
+
8
+ require 'color'
9
+
10
+ module Color::Palette
11
+ end
@@ -0,0 +1,150 @@
1
+ #--
2
+ # Colour management with Ruby.
3
+ #
4
+ # Copyright 2005 Austin Ziegler
5
+ # http://rubyforge.org/ruby-pdf/
6
+ #++
7
+
8
+ require 'color/palette'
9
+
10
+ # Generates a monochromatic constrasting colour palette for background and
11
+ # foreground. What does this mean?
12
+ #
13
+ # Monochromatic: A single colour is used to generate the base palette, and
14
+ # this colour is lightened five times and darkened five times to provide
15
+ # eleven distinct colours.
16
+ #
17
+ # Contrasting: The foreground is also generated as a monochromatic colour
18
+ # palettte; however, all generated colours are tested to see that they are
19
+ # appropriately contrasting to ensure maximum readability of the
20
+ # foreground against the background.
21
+ class Color::Palette::MonoContrast
22
+ # Hash of CSS background colour values.
23
+ #
24
+ # This is always 11 values:
25
+ #
26
+ # 0:: The starting colour.
27
+ # +1..+5:: Lighter colours.
28
+ # -1..-5:: Darker colours.
29
+ attr_accessor :background
30
+ # Hash of CSS foreground colour values.
31
+ #
32
+ # This is always 11 values:
33
+ #
34
+ # 0:: The starting colour.
35
+ # +1..+5:: Lighter colours.
36
+ # -1..-5:: Darker colours.
37
+ attr_accessor :foreground
38
+
39
+ # The minimum brightness difference between the background and the
40
+ # foreground. Setting this value will regenerate the palette based on
41
+ # the base colours. The default value for this is 125 / 255.0.
42
+ attr_accessor :minimum_brightness_diff
43
+ def minimum_brightness_diff=(bd) #:nodoc:
44
+ @minimum_brightness_diff = bd
45
+ regenerate(@background[0], @foreground[0])
46
+ end
47
+
48
+ # The minimum colour difference between the background and the
49
+ # foreground. Setting this value will regenerate the palette based on
50
+ # the base colours.
51
+ attr_accessor :minimum_color_diff
52
+ def minimum_color_diff=(cd) #:noco:
53
+ @minimum_color_diff = bd
54
+ regenerate(@background[0], @foreground[0])
55
+ end
56
+
57
+ # Generate the initial palette.
58
+ def initialize(background, foreground = nil)
59
+ @minimum_brightness_diff = 125 / 255.0
60
+ @minimum_color_diff = 500 / 765.0
61
+
62
+ regenerate(background, foreground)
63
+ end
64
+
65
+ # Generate the colour palettes.
66
+ def regenerate(background, foreground = nil)
67
+ foreground ||= background
68
+ background = background.to_rgb
69
+ foreground = foreground.to_rgb
70
+
71
+ @background = {}
72
+ @foreground = {}
73
+
74
+ @background[-5] = background.darken_by(10)
75
+ @background[-4] = background.darken_by(25)
76
+ @background[-3] = background.darken_by(50)
77
+ @background[-2] = background.darken_by(75)
78
+ @background[-1] = background.darken_by(85)
79
+ @background[ 0] = background
80
+ @background[+1] = background.lighten_by(85)
81
+ @background[+2] = background.lighten_by(75)
82
+ @background[+3] = background.lighten_by(50)
83
+ @background[+4] = background.lighten_by(25)
84
+ @background[+5] = background.lighten_by(10)
85
+
86
+ @foreground[-5] = calculate_foreground(@background[-5], foreground)
87
+ @foreground[-4] = calculate_foreground(@background[-4], foreground)
88
+ @foreground[-3] = calculate_foreground(@background[-3], foreground)
89
+ @foreground[-2] = calculate_foreground(@background[-2], foreground)
90
+ @foreground[-1] = calculate_foreground(@background[-1], foreground)
91
+ @foreground[ 0] = calculate_foreground(@background[ 0], foreground)
92
+ @foreground[+1] = calculate_foreground(@background[+1], foreground)
93
+ @foreground[+2] = calculate_foreground(@background[+2], foreground)
94
+ @foreground[+3] = calculate_foreground(@background[+3], foreground)
95
+ @foreground[+4] = calculate_foreground(@background[+4], foreground)
96
+ @foreground[+5] = calculate_foreground(@background[+5], foreground)
97
+ end
98
+
99
+ # Given a background colour and a foreground colour, modifies the
100
+ # foreground colour so that it will have enough contrast to be seen
101
+ # against the background colour.
102
+ #
103
+ # Uses #mininum_brightness_diff and #minimum_color_diff.
104
+ def calculate_foreground(background, foreground)
105
+ nfg = nil
106
+ # Loop through brighter and darker versions of the foreground color.
107
+ # The numbers here represent the amount of foreground color to mix
108
+ # with black and white.
109
+ [100, 75, 50, 25, 0].each do |percent|
110
+ dfg = foreground.darken_by(percent)
111
+ lfg = foreground.lighten_by(percent)
112
+
113
+ dbd = brightness_diff(background, dfg)
114
+ lbd = brightness_diff(background, lfg)
115
+
116
+ if lbd > dbd
117
+ nfg = lfg
118
+ nbd = lbd
119
+ else
120
+ nfg = dfg
121
+ nbd = dbd
122
+ end
123
+
124
+ ncd = color_diff(background, nfg)
125
+
126
+ break if nbd >= @minimum_brightness_diff and ncd >= @minimum_color_diff
127
+ end
128
+ nfg
129
+ end
130
+
131
+ # Returns the absolute difference between the brightness levels of two
132
+ # colours. This will be a decimal value between 0 and 1. W3C
133
+ # accessibility guidelines for colour
134
+ # contrast[http://www.w3.org/TR/AERT#color-contrast] suggest that this
135
+ # value be at least approximately 0.49 (125 / 255.0) for proper contrast.
136
+ def brightness_diff(c1, c2)
137
+ (c1.brightness - c2.brightness).abs
138
+ end
139
+
140
+ # Returns the contrast between to colours, a decimal value between 0 and
141
+ # 3. W3C accessibility guidelines for colour
142
+ # contrast[http://www.w3.org/TR/AERT#color-contrast] suggest that this
143
+ # value be at least approximately 1.96 (500 / 765.0) for proper contrast.
144
+ def color_diff(c1, c2)
145
+ r = (c1.r - c2.r).abs
146
+ g = (c1.g - c2.g).abs
147
+ b = (c1.b - c2.b).abs
148
+ r + g + b
149
+ end
150
+ end
@@ -0,0 +1,155 @@
1
+ #--
2
+ # Colour management with Ruby.
3
+ #
4
+ # Copyright 2005 Austin Ziegler
5
+ # http://rubyforge.org/ruby-pdf/
6
+ #++
7
+
8
+ require 'color/cmyk'
9
+
10
+ module Color
11
+ # An RGB colour object.
12
+ class RGB
13
+ # The format required to present the colour to a PDF document.
14
+ PDF_FORMAT_STR = "%.3f %.3f %.3f %s"
15
+
16
+ class << self
17
+ # Creates an RGB colour object from percentages 0 .. 100.
18
+ def from_percentage(r = 0, g = 0, b = 0)
19
+ from_fraction(r / 100.0, g / 100.0, b / 100.0)
20
+ end
21
+
22
+ # Creates an RGB colour object from fractional values 0 .. 1.
23
+ def from_fraction(r = 0.0, g = 0.0, b = 0.0)
24
+ colour = Color::RGB.new
25
+ colour.instance_variable_set(:@r, r)
26
+ colour.instance_variable_set(:@g, g)
27
+ colour.instance_variable_set(:@b, b)
28
+ colour
29
+ end
30
+
31
+ # Creates an RGB colour object from an HTML colour descriptor (e.g.,
32
+ # <tt>"fed"</tt> or <tt>"#cabbed;"</tt>.
33
+ def from_html(html_colour)
34
+ html_colour = html_colour.gsub(%r{[#;]}, '')
35
+ case html_colour.size
36
+ when 3
37
+ colours = html_colour.scan(%r{[0-9A-Fa-f]}).map { |el| (el * 2).to_i(16) }
38
+ when 6
39
+ colours = html_colour.scan(%r<[0-9A-Fa-f]{2}>).map { |el| el.to_i(16) }
40
+ else
41
+ raise ArgumentError
42
+ end
43
+
44
+ Color::RGB.new(*colours)
45
+ end
46
+ end
47
+
48
+ def ==(other)
49
+ other = other.to_rgb
50
+ other.kind_of?(Color::RGB) and
51
+ (@r == other.r) and
52
+ (@g == other.g) and
53
+ (@b == other.b)
54
+ end
55
+
56
+ # Creates an RGB colour object from the standard range 0 .. 255.
57
+ def initialize(r = 0, g = 0, b = 0)
58
+ @r = r / 255.0
59
+ @g = g / 255.0
60
+ @b = b / 255.0
61
+ end
62
+
63
+ # Present the colour as a fill colour string for PDF.
64
+ def pdf_fill
65
+ PDF_FORMAT_STR % [ @r, @g, @b, "rg" ]
66
+ end
67
+
68
+ # Present the colour as a stroke colour string for PDF.
69
+ def pdf_stroke
70
+ PDF_FORMAT_STR % [ @r, @g, @b, "RG" ]
71
+ end
72
+
73
+ # Present the colour as an HTML/CSS colour string.
74
+ def html
75
+ r = (@r * 255).round
76
+ r = 255 if r > 255
77
+
78
+ g = (@g * 255).round
79
+ g = 255 if g > 255
80
+
81
+ b = (@b * 255).round
82
+ b = 255 if b > 255
83
+
84
+ "#%02x%02x%02x" % [ r, g, b ]
85
+ end
86
+
87
+ # Convert the RGB colour to CMYK. Note that colour experts strongly
88
+ # suggest that this is a *bad* idea, as CMYK represents percentages of
89
+ # inks, not mixed colour intensities like RGB.
90
+ def to_cmyk
91
+ c = 1.0 - @r.to_f
92
+ m = 1.0 - @g.to_f
93
+ y = 1.0 - @b.to_f
94
+
95
+ k = 1.0
96
+ k = c if c < k
97
+ k = m if m < k
98
+ k = y if y < k
99
+
100
+ c = (c.to_f - k.to_f) / (1.0 - k.to_f)
101
+ m = (m.to_f - k.to_f) / (1.0 - k.to_f)
102
+ y = (y.to_f - k.to_f) / (1.0 - k.to_f)
103
+
104
+ Color::CMYK.new(c, m, y, k)
105
+ end
106
+
107
+ def to_rgb
108
+ self.dup
109
+ end
110
+
111
+ # Lighten the RGB hue by the stated percent.
112
+ def lighten_by(percent)
113
+ mix_with(White, percent)
114
+ end
115
+
116
+ # Darken the RGB hue by the stated percent.
117
+ def darken_by(percent)
118
+ mix_with(Black, percent)
119
+ end
120
+
121
+ # Mix the mask colour (which must be an RGB object) with the current
122
+ # colour at the stated opacity percentage (0 .. 100).
123
+ def mix_with(mask, opacity)
124
+ opacity /= 100.0
125
+ rgb = self.dup
126
+
127
+ rgb.r = (@r * opacity) + (mask.r * (1 - opacity))
128
+ rgb.g = (@g * opacity) + (mask.g * (1 - opacity))
129
+ rgb.b = (@b * opacity) + (mask.b * (1 - opacity))
130
+
131
+ rgb
132
+ end
133
+
134
+ # Returns the YIQ (NTSC) colour encoding of the RGB value.
135
+ def to_yiq
136
+ {
137
+ :y => (@r * 0.299) + (@g * 0.587) + (@b * 0.114),
138
+ :i => (@r * 0.596) + (@g * -0.275) + (@b * -0.321),
139
+ :q => (@r * 0.212) + (@g * -0.523) + (@b * 0.311),
140
+ }
141
+ end
142
+
143
+ # Returns the brightness value for a colour, a number between 0 and
144
+ # 1. Based on the Y value of YIQ encoding, representing luminosity, or
145
+ # perceived brightness.
146
+ def brightness
147
+ to_yiq[:y]
148
+ end
149
+
150
+ attr_accessor :r, :g, :b
151
+
152
+ White = RGB.new(255, 255, 255)
153
+ Black = RGB.new(0, 0, 0)
154
+ end
155
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.6
3
+ specification_version: 1
4
+ name: color-tools
5
+ version: !ruby/object:Gem::Version
6
+ version: 1.0.0
7
+ date: 2005-03-06
8
+ summary: color-tools provides RGB and CMYK colour definitions and manpiulations.
9
+ require_paths:
10
+ - lib
11
+ email: austin@rubyforge.org
12
+ homepage: http://rubyforge.org/projects/ruby-pdf
13
+ rubyforge_project: ruby-pdf
14
+ description: "color-tools is a Ruby library to provide RGB and CMYK colour support to
15
+ applications that require it. It provides 148 named RGB colours that are
16
+ commonly supported and used in HTML, colour manipulation operations, and a
17
+ monochromatic contrasting palette generator."
18
+ autorequire: color
19
+ default_executable:
20
+ bindir: bin
21
+ has_rdoc: true
22
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
23
+ requirements:
24
+ -
25
+ - ">"
26
+ - !ruby/object:Gem::Version
27
+ version: 0.0.0
28
+ version:
29
+ platform: ruby
30
+ authors:
31
+ - Austin Ziegler
32
+ files:
33
+ - Changelog
34
+ - Install
35
+ - lib
36
+ - Rakefile
37
+ - README
38
+ - lib/color
39
+ - lib/color.rb
40
+ - lib/color/cmyk.rb
41
+ - lib/color/palette
42
+ - lib/color/palette.rb
43
+ - lib/color/rgb.rb
44
+ - lib/color/palette/monocontrast.rb
45
+ test_files: []
46
+ rdoc_options:
47
+ - "--title"
48
+ - color-tools
49
+ - "--main"
50
+ - README
51
+ - "--line-numbers"
52
+ extra_rdoc_files:
53
+ - README
54
+ - Install
55
+ - Changelog
56
+ executables: []
57
+ extensions: []
58
+ requirements: []
59
+ dependencies: []