color_lib 1.4.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/.travis.yml +7 -0
- data/CHANGELOG +96 -0
- data/Gemfile +2 -0
- data/LICENSE.txt +21 -0
- data/README.md +25 -0
- data/Rakefile +11 -0
- data/color_lib.gemspec +24 -0
- data/lib/color_lib.rb +139 -0
- data/lib/color_lib/cmyk.rb +280 -0
- data/lib/color_lib/css.rb +15 -0
- data/lib/color_lib/grayscale.rb +205 -0
- data/lib/color_lib/hsl.rb +221 -0
- data/lib/color_lib/palette.rb +4 -0
- data/lib/color_lib/palette/adobecolor.rb +265 -0
- data/lib/color_lib/palette/gimp.rb +103 -0
- data/lib/color_lib/palette/monocontrast.rb +169 -0
- data/lib/color_lib/pantone.rb +1499 -0
- data/lib/color_lib/rgb-colors.rb +343 -0
- data/lib/color_lib/rgb.rb +461 -0
- data/lib/color_lib/rgb/metallic.rb +31 -0
- data/lib/color_lib/version.rb +3 -0
- data/lib/color_lib/yiq.rb +79 -0
- data/lib/test/unit/assertions.rb +332 -0
- data/setup.rb +1588 -0
- data/test/test_adobecolor.rb +404 -0
- data/test/test_cmyk.rb +117 -0
- data/test/test_color.rb +129 -0
- data/test/test_css.rb +18 -0
- data/test/test_gimp.rb +87 -0
- data/test/test_grayscale.rb +110 -0
- data/test/test_hsl.rb +140 -0
- data/test/test_monocontrast.rb +131 -0
- data/test/test_rgb.rb +333 -0
- data/test/test_yiq.rb +62 -0
- metadata +132 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a1f04b55358a9cdc8a5c4fa32df79b152fd6b91e
|
4
|
+
data.tar.gz: 5972b46bd97e6f4a4d61951dee4c49726d2d7aa9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ef2a082378edd5344546018019c90f1787a51b8c64d2242ca7758e06db1ab50d1252eff2dbd4450093e30deb914f6ce82aa2f01612c0585fc740f321df3e0168
|
7
|
+
data.tar.gz: e687268a86384904554f54bfb377db5dddd07b9b8a1d7a7b7f1ac778c71fa05b43a9061ac984e5de49c4b1d273747534cf06f94873f4c00a30c1ca1994868619
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CHANGELOG
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
== ColorLib 1.4.2 / 2010.02.03
|
2
|
+
* Added 7000 range to Pantone
|
3
|
+
|
4
|
+
== ColorLib 1.4.1 / 2010.02.03
|
5
|
+
* Imported to github.
|
6
|
+
* Converted to Hoe 2.5 spec format.
|
7
|
+
|
8
|
+
== ColorLib 1.4.0 / 2007.02.11
|
9
|
+
* Merged Austin Ziegler's color-tools library (previously part of the Ruby
|
10
|
+
PDF Tools project) with Matt Lyon's color library.
|
11
|
+
- The HSL implementation from the ColorLib class has been merged into
|
12
|
+
ColorLib::HSL. ColorLib is a module the way it was for color-tools.
|
13
|
+
- A thin veneer has been written to allow ColorLib::new to return a ColorLib::HSL
|
14
|
+
instance; ColorLib::HSL supports as many methods as possible that were
|
15
|
+
previously supported by the ColorLib class.
|
16
|
+
- Values that were previously rounded by ColorLib are no longer rounded;
|
17
|
+
fractional values matter.
|
18
|
+
* Converted to hoe for project management.
|
19
|
+
* Moved to the next step of deprecating ColorLib::Name values; printing a
|
20
|
+
warning for each use (see the history for color-tools 1.3.0).
|
21
|
+
* Print a warning on the access of either VERSION or COLOR_TOOLS_VERSION; the
|
22
|
+
version constant is now COLOR_VERSION.
|
23
|
+
* Added humanized versions of accessors (e.g., CMYK colours now have both #cyan
|
24
|
+
and #c to access the cyan component of the colour; #cyan provides the value
|
25
|
+
as a percentage).
|
26
|
+
* Added CSS3 formatters for RGB, RGBA, HSL, and HSLA outputs. Note that the
|
27
|
+
ColorLib library does not yet have a way of setting alpha opacity, so the
|
28
|
+
output for RGBA and HSLA are at full alpha opacity (1.0). The values are
|
29
|
+
output with two decimal places.
|
30
|
+
* Applied a patch to provide simple arithmetic colour addition and subtraction
|
31
|
+
to ColorLib::GrayScale and ColorLib::RGB. The patch was contributed by Jeremy
|
32
|
+
Hinegardner <jeremy@hinegardner.org>. This patch also provides the ability to
|
33
|
+
return the maximum RGB value as a grayscale colour.
|
34
|
+
* Fixed two problems reported by Jean Krohn <jb.krohn@free.fr> against
|
35
|
+
color-tools relating to RGB-to-HSL and HSL-to-RGB conversion. (ColorLib and
|
36
|
+
color-tools use the same formulas, but the ordering of the calculations is
|
37
|
+
slightly different with ColorLib and did not suffer from this problem;
|
38
|
+
color-tools was more sensitive to floating-point values and precision
|
39
|
+
errors.)
|
40
|
+
* Fixed an issue with HSL/RGB conversions reported by Adam Johnson
|
41
|
+
<adam.sven.johnson@gmail.com>.
|
42
|
+
* Added an Adobe ColorLib swatch (Photoshop) palette reader,
|
43
|
+
ColorLib::Palette::AdobeColorLib (for .aco files only).
|
44
|
+
|
45
|
+
== ColorLib 0.1.0 / 2006.08.05
|
46
|
+
* Added HSL (degree, percent, percent) interface.
|
47
|
+
* Removed RGB instance variable; color is managed internally as HSL floating
|
48
|
+
point.
|
49
|
+
* Tests!
|
50
|
+
|
51
|
+
== color-tools 1.3.0
|
52
|
+
* Added new metallic colours suggested by Jim Freeze <jfn@freeze.org>. These
|
53
|
+
are in the namespace ColorLib::Metallic.
|
54
|
+
* Colours that were defined in the ColorLib namespace (e.g., ColorLib::Red,
|
55
|
+
ColorLib::AliceBlue) are now defined in ColorLib::RGB (e.g., ColorLib::RGB::Red,
|
56
|
+
ColorLib::RGB::AliceBlue). They are added back to the ColorLib namespace on the
|
57
|
+
first use of the old colours and a warning is printed. In version 1.4, this
|
58
|
+
warning will be printed on every use of the old colours. In version 1.5,
|
59
|
+
the backwards compatible support for colours like ColorLib::Red will be
|
60
|
+
removed completely.
|
61
|
+
* Added the ColorLib::CSS module, color_lib/css or ColorLib::CSS that provides a name
|
62
|
+
lookup of ColorLib::RGB-namespaced constants with ColorLib::CSS[name]. Most of
|
63
|
+
these colours (which are mirrored from the ColorLib::RGB default colours) are
|
64
|
+
only "officially" recognised under the CSS3 colour module or SVG.
|
65
|
+
* Added the ColorLib::HSL colour space and some helper utilities to ColorLib::RGB
|
66
|
+
for colour manipulation using the HSL value.
|
67
|
+
* Controlled internal value replacement to be between 0 and 1 for all
|
68
|
+
colours.
|
69
|
+
* Updated ColorLib::Palette::Gimp to more meaningfully deal with duplicate named
|
70
|
+
colours. Named colours now return an array of colours.
|
71
|
+
* Indicated the plans for some methods and constants out to color-tools 2.0.
|
72
|
+
* Added unit tests and fixed a number of hidden bugs because of them.
|
73
|
+
|
74
|
+
== color-tools 1.2.0
|
75
|
+
* Changed installer from a custom-written install.rb to setup.rb
|
76
|
+
3.3.1-modified.
|
77
|
+
* Added ColorLib::GreyScale (or ColorLib::GrayScale).
|
78
|
+
* Added ColorLib::YIQ. This colour definition is incomplete; it does not have
|
79
|
+
conversions from YIQ to other colour spaces.
|
80
|
+
|
81
|
+
== color-tools 1.1.0
|
82
|
+
* Added color_lib/palette/gimp to support the reading and use of GIMP color
|
83
|
+
palettes.
|
84
|
+
|
85
|
+
== color-tools 1.0.0
|
86
|
+
* Initial release.
|
87
|
+
|
88
|
+
== Copyright
|
89
|
+
ColorLib
|
90
|
+
Colour Management with Ruby
|
91
|
+
http://rubyforge.org/projects/color
|
92
|
+
|
93
|
+
Licensed under a MIT-style licence. See Licence.txt in the main
|
94
|
+
distribution for full licensing information.
|
95
|
+
|
96
|
+
Copyright (c) 2005 - 2010 Austin Ziegler and Matt Lyon
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2014 Brandfolder, Inc.
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# ColorLib [](https://travis-ci.org/brandfolder/color_lib)
|
2
|
+
|
3
|
+
A lib for colors.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'color_lib'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install color_lib## Contributing
|
20
|
+
|
21
|
+
1. Fork it ( https://github.com/brandfolder/color_lib/fork )
|
22
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
23
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
24
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
25
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/color_lib.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'color_lib/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "color_lib"
|
8
|
+
spec.version = ColorLib::VERSION
|
9
|
+
spec.authors = ["Brandfolder, Inc."]
|
10
|
+
spec.email = ["developers@brandfolder.com"]
|
11
|
+
spec.summary = %q{A lib for colors}
|
12
|
+
spec.description = %q{A lib for working with colors in ruby}
|
13
|
+
spec.homepage = "https://github.com/brandfolder/color_lib"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "minitest", "~> 5.5"
|
24
|
+
end
|
data/lib/color_lib.rb
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
require "color_lib/version"
|
2
|
+
|
3
|
+
module ColorLib
|
4
|
+
COLOR_VERSION = VERSION
|
5
|
+
|
6
|
+
class RGB;
|
7
|
+
end
|
8
|
+
class CMYK;
|
9
|
+
end
|
10
|
+
class GrayScale;
|
11
|
+
end
|
12
|
+
class YIQ;
|
13
|
+
end
|
14
|
+
class Pantone;
|
15
|
+
end
|
16
|
+
|
17
|
+
# The maximum "resolution" for colour math; if any value is less than or
|
18
|
+
# equal to this value, it is treated as zero.
|
19
|
+
COLOR_EPSILON = 1e-5
|
20
|
+
# The tolerance for comparing the components of two colours. In general,
|
21
|
+
# colours are considered equal if all of their components are within this
|
22
|
+
# tolerance value of each other.
|
23
|
+
COLOR_TOLERANCE = 1e-4
|
24
|
+
|
25
|
+
class << self
|
26
|
+
# Returns +true+ if the value is less than COLOR_EPSILON.
|
27
|
+
def near_zero?(value)
|
28
|
+
(value.abs <= COLOR_EPSILON)
|
29
|
+
end
|
30
|
+
|
31
|
+
# Returns +true+ if the value is within COLOR_EPSILON of zero or less than
|
32
|
+
# zero.
|
33
|
+
def near_zero_or_less?(value)
|
34
|
+
(value < 0.0 or near_zero?(value))
|
35
|
+
end
|
36
|
+
|
37
|
+
# Returns +true+ if the value is within COLOR_EPSILON of one.
|
38
|
+
def near_one?(value)
|
39
|
+
near_zero?(value - 1.0)
|
40
|
+
end
|
41
|
+
|
42
|
+
# Returns +true+ if the value is within COLOR_EPSILON of one or more than
|
43
|
+
# one.
|
44
|
+
def near_one_or_more?(value)
|
45
|
+
(value > 1.0 or near_one?(value))
|
46
|
+
end
|
47
|
+
|
48
|
+
# Normalizes the value to the range (0.0) .. (1.0).
|
49
|
+
def normalize(value)
|
50
|
+
if near_zero_or_less? value
|
51
|
+
0.0
|
52
|
+
elsif near_one_or_more? value
|
53
|
+
1.0
|
54
|
+
else
|
55
|
+
value
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
alias normalize_fractional normalize
|
60
|
+
|
61
|
+
def normalize_to_range(value, range)
|
62
|
+
range = (range.end..range.begin) if (range.end < range.begin)
|
63
|
+
|
64
|
+
if value <= range.begin
|
65
|
+
range.begin
|
66
|
+
elsif value >= range.end
|
67
|
+
range.end
|
68
|
+
else
|
69
|
+
value
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
# Normalize the value to the range (0) .. (255).
|
74
|
+
def normalize_byte(value)
|
75
|
+
normalize_to_range(value, 0..255).to_i
|
76
|
+
end
|
77
|
+
|
78
|
+
alias normalize_8bit normalize_byte
|
79
|
+
|
80
|
+
# Normalize the value to the range (0) .. (65535).
|
81
|
+
def normalize_word(value)
|
82
|
+
normalize_to_range(value, 0..65535).to_i
|
83
|
+
end
|
84
|
+
|
85
|
+
alias normalize_16bit normalize_word
|
86
|
+
|
87
|
+
def const_missing(name) #:nodoc:
|
88
|
+
case name
|
89
|
+
when "VERSION", :VERSION, "COLOR_TOOLS_VERSION", :COLOR_TOOLS_VERSION
|
90
|
+
warn "ColorLib::#{name} has been deprecated. Use ColorLib::COLOR_VERSION instead."
|
91
|
+
ColorLib::COLOR_VERSION
|
92
|
+
else
|
93
|
+
if ColorLib::RGB.const_defined?(name)
|
94
|
+
warn "ColorLib::#{name} has been deprecated. Use ColorLib::RGB::#{name} instead."
|
95
|
+
ColorLib::RGB.const_get(name)
|
96
|
+
else
|
97
|
+
super
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
# Provides a thin veneer over the ColorLib module to make it seem like this
|
103
|
+
# is ColorLib 0.1.0 (a class) and not ColorLib 1.4.1 (a module). This
|
104
|
+
# "constructor" will be removed in the future.
|
105
|
+
#
|
106
|
+
# mode = :hsl:: +values+ must be an array of [ hue deg, sat %, lum % ].
|
107
|
+
# A ColorLib::HSL object will be created.
|
108
|
+
# mode = :rgb:: +values+ will either be an HTML-style colour string or
|
109
|
+
# an array of [ red, green, blue ] (range 0 .. 255). A
|
110
|
+
# ColorLib::RGB object will be created.
|
111
|
+
# mode = :cmyk:: +values+ must be an array of [ cyan %, magenta %, yellow
|
112
|
+
# %, black % ]. A ColorLib::CMYK object will be created.
|
113
|
+
def new(values, mode = :rgb)
|
114
|
+
warn "ColorLib.new has been deprecated. Use ColorLib::#{mode.to_s.upcase}.new instead."
|
115
|
+
color = case mode
|
116
|
+
when :hsl
|
117
|
+
ColorLib::HSL.new(*values)
|
118
|
+
when :rgb
|
119
|
+
values = [values].flatten
|
120
|
+
if values.size == 1
|
121
|
+
ColorLib::RGB.from_html(*values)
|
122
|
+
else
|
123
|
+
ColorLib::RGB.new(*values)
|
124
|
+
end
|
125
|
+
when :cmyk
|
126
|
+
ColorLib::CMYK.new(*values)
|
127
|
+
end
|
128
|
+
color.to_hsl
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
require 'color_lib/rgb'
|
134
|
+
require 'color_lib/cmyk'
|
135
|
+
require 'color_lib/grayscale'
|
136
|
+
require 'color_lib/hsl'
|
137
|
+
require 'color_lib/yiq'
|
138
|
+
require 'color_lib/rgb/metallic'
|
139
|
+
require 'color_lib/pantone'
|
@@ -0,0 +1,280 @@
|
|
1
|
+
# An CMYK colour object. CMYK (cyan, magenta, yellow, and black) colours are
|
2
|
+
# based on additive percentages of ink. A CMYK colour of (0.3, 0, 0.8, 0.3)
|
3
|
+
# would be mixed from 30% cyan, 0% magenta, 80% yellow, and 30% black.
|
4
|
+
# Primarily used in four-colour printing processes.
|
5
|
+
class ColorLib::CMYK
|
6
|
+
# The format of a DeviceCMYK colour for PDF. In color-tools 2.0 this will
|
7
|
+
# be removed from this package and added back as a modification by the
|
8
|
+
# PDF::Writer package.
|
9
|
+
PDF_FORMAT_STR = "%.3f %.3f %.3f %.3f %s"
|
10
|
+
|
11
|
+
# Compares the other colour to this one. The other colour will be
|
12
|
+
# converted to CMYK before comparison, so the comparison between a CMYK
|
13
|
+
# colour and a non-CMYK colour will be approximate and based on the other
|
14
|
+
# colour's #to_cmyk conversion. If there is no #to_cmyk conversion, this
|
15
|
+
# will raise an exception. This will report that two CMYK colours are
|
16
|
+
# equivalent if all component values are within COLOR_TOLERANCE of each
|
17
|
+
# other.
|
18
|
+
def ==(other)
|
19
|
+
other = other.to_cmyk
|
20
|
+
other.kind_of?(ColorLib::CMYK) and
|
21
|
+
((@c - other.c).abs <= ColorLib::COLOR_TOLERANCE) and
|
22
|
+
((@m - other.m).abs <= ColorLib::COLOR_TOLERANCE) and
|
23
|
+
((@y - other.y).abs <= ColorLib::COLOR_TOLERANCE) and
|
24
|
+
((@k - other.k).abs <= ColorLib::COLOR_TOLERANCE)
|
25
|
+
end
|
26
|
+
|
27
|
+
# Creates a CMYK colour object from fractional values 0..1.
|
28
|
+
#
|
29
|
+
# ColorLib::CMYK.from_fraction(0.3, 0, 0.8, 0.3)
|
30
|
+
def self.from_fraction(c = 0, m = 0, y = 0, k = 0)
|
31
|
+
colour = ColorLib::CMYK.new
|
32
|
+
colour.c = c
|
33
|
+
colour.m = m
|
34
|
+
colour.y = y
|
35
|
+
colour.k = k
|
36
|
+
colour
|
37
|
+
end
|
38
|
+
|
39
|
+
# Creates a CMYK colour object from percentages. Internally, the colour is
|
40
|
+
# managed as fractional values 0..1.
|
41
|
+
#
|
42
|
+
# ColorLib::CMYK.new(30, 0, 80, 30)
|
43
|
+
def self.from_percent(c = 0, m = 0, y = 0, k = 0)
|
44
|
+
ColorLib::CMYK.new(c, m, y, k)
|
45
|
+
end
|
46
|
+
|
47
|
+
# Creates a CMYK colour object from percentages. Internally, the colour is
|
48
|
+
# managed as fractional values 0..1.
|
49
|
+
#
|
50
|
+
# ColorLib::CMYK.new(30, 0, 80, 30)
|
51
|
+
def initialize(c = 0, m = 0, y = 0, k = 0)
|
52
|
+
@c = c / 100.0
|
53
|
+
@m = m / 100.0
|
54
|
+
@y = y / 100.0
|
55
|
+
@k = k / 100.0
|
56
|
+
end
|
57
|
+
|
58
|
+
# Present the colour as a DeviceCMYK fill colour string for PDF. This will
|
59
|
+
# be removed from the default package in color-tools 2.0.
|
60
|
+
def pdf_fill
|
61
|
+
PDF_FORMAT_STR % [@c, @m, @y, @k, "k"]
|
62
|
+
end
|
63
|
+
|
64
|
+
# Present the colour as a DeviceCMYK stroke colour string for PDF. This
|
65
|
+
# will be removed from the default package in color-tools 2.0.
|
66
|
+
def pdf_stroke
|
67
|
+
PDF_FORMAT_STR % [@c, @m, @y, @k, "K"]
|
68
|
+
end
|
69
|
+
|
70
|
+
# Present the colour as an RGB HTML/CSS colour string (e.g., "#aabbcc").
|
71
|
+
# Note that this will perform a #to_rgb operation using the default
|
72
|
+
# conversion formula.
|
73
|
+
def html
|
74
|
+
to_rgb.html
|
75
|
+
end
|
76
|
+
|
77
|
+
# Present the colour as an RGB HTML/CSS colour string (e.g., "rgb(0%, 50%,
|
78
|
+
# 100%)"). Note that this will perform a #to_rgb operation using the
|
79
|
+
# default conversion formula.
|
80
|
+
def css_rgb
|
81
|
+
to_rgb.css_rgb
|
82
|
+
end
|
83
|
+
|
84
|
+
# Present the colour as an RGBA (with alpha) HTML/CSS colour string (e.g.,
|
85
|
+
# "rgb(0%, 50%, 100%, 1)"). Note that this will perform a #to_rgb
|
86
|
+
# operation using the default conversion formula.
|
87
|
+
def css_rgba
|
88
|
+
to_rgb.css_rgba
|
89
|
+
end
|
90
|
+
|
91
|
+
# Present the colour as an HSL HTML/CSS colour string (e.g., "hsl(180,
|
92
|
+
# 25%, 35%)"). Note that this will perform a #to_hsl operation using the
|
93
|
+
# default conversion formula.
|
94
|
+
def css_hsl
|
95
|
+
to_hsl.css_hsl
|
96
|
+
end
|
97
|
+
|
98
|
+
# Present the colour as an HSLA (with alpha) HTML/CSS colour string (e.g.,
|
99
|
+
# "hsla(180, 25%, 35%, 1)"). Note that this will perform a #to_hsl
|
100
|
+
# operation using the default conversion formula.
|
101
|
+
def css_hsla
|
102
|
+
to_hsl.css_hsla
|
103
|
+
end
|
104
|
+
|
105
|
+
# Converts the CMYK colour to RGB. Most colour experts strongly suggest
|
106
|
+
# that this is not a good idea (some even suggesting that it's a very bad
|
107
|
+
# idea). CMYK represents additive percentages of inks on white paper,
|
108
|
+
# whereas RGB represents mixed colour intensities on a black screen.
|
109
|
+
#
|
110
|
+
# However, the colour conversion can be done, and there are two different
|
111
|
+
# methods for the conversion that provide slightly different results.
|
112
|
+
# Adobe PDF conversions are done with the first form.
|
113
|
+
#
|
114
|
+
# # Adobe PDF Display Formula
|
115
|
+
# r = 1.0 - min(1.0, c + k)
|
116
|
+
# g = 1.0 - min(1.0, m + k)
|
117
|
+
# b = 1.0 - min(1.0, y + k)
|
118
|
+
#
|
119
|
+
# # Other
|
120
|
+
# r = 1.0 - (c * (1.0 - k) + k)
|
121
|
+
# g = 1.0 - (m * (1.0 - k) + k)
|
122
|
+
# b = 1.0 - (y * (1.0 - k) + k)
|
123
|
+
#
|
124
|
+
# If we have a CMYK colour of [33% 66% 83% 25%], the first method will
|
125
|
+
# give an approximate RGB colour of (107, 23, 0) or #6b1700. The second
|
126
|
+
# method will give an approximate RGB colour of (128, 65, 33) or #804121.
|
127
|
+
# Which is correct? Although the colours may seem to be drastically
|
128
|
+
# different in the RGB colour space, they are very similar colours,
|
129
|
+
# differing mostly in intensity. The first is a darker, slightly redder
|
130
|
+
# brown; the second is a lighter brown.
|
131
|
+
#
|
132
|
+
# Because of this subtlety, both methods are now offered for conversion.
|
133
|
+
# The Adobe method is not used by default; to enable it, pass +true+ to
|
134
|
+
# #to_rgb.
|
135
|
+
#
|
136
|
+
# Future versions of ColorLib may offer other conversion mechanisms that
|
137
|
+
# offer greater colour fidelity, including recognition of ICC colour
|
138
|
+
# profiles.
|
139
|
+
def to_rgb(use_adobe_method = false)
|
140
|
+
if use_adobe_method
|
141
|
+
r = 1.0 - [1.0, @c + @k].min
|
142
|
+
g = 1.0 - [1.0, @m + @k].min
|
143
|
+
b = 1.0 - [1.0, @y + @k].min
|
144
|
+
else
|
145
|
+
r = 1.0 - (@c.to_f * (1.0 - @k.to_f) + @k.to_f)
|
146
|
+
g = 1.0 - (@m.to_f * (1.0 - @k.to_f) + @k.to_f)
|
147
|
+
b = 1.0 - (@y.to_f * (1.0 - @k.to_f) + @k.to_f)
|
148
|
+
end
|
149
|
+
ColorLib::RGB.from_fraction(r, g, b)
|
150
|
+
end
|
151
|
+
|
152
|
+
# Converts the CMYK colour to a single greyscale value. There are
|
153
|
+
# undoubtedly multiple methods for this conversion, but only a minor
|
154
|
+
# variant of the Adobe conversion method will be used:
|
155
|
+
#
|
156
|
+
# g = 1.0 - min(1.0, 0.299 * c + 0.587 * m + 0.114 * y + k)
|
157
|
+
#
|
158
|
+
# This treats the CMY values similarly to YIQ (NTSC) values and then adds
|
159
|
+
# the level of black. This is a variant of the Adobe version because it
|
160
|
+
# uses the more precise YIQ (NTSC) conversion values for Y (intensity)
|
161
|
+
# rather than the approximates provided by Adobe (0.3, 0.59, and 0.11).
|
162
|
+
def to_grayscale
|
163
|
+
c = 0.299 * @c.to_f
|
164
|
+
m = 0.587 * @m.to_f
|
165
|
+
y = 0.114 * @y.to_f
|
166
|
+
g = 1.0 - [1.0, c + m + y + @k].min
|
167
|
+
ColorLib::GrayScale.from_fraction(g)
|
168
|
+
end
|
169
|
+
|
170
|
+
alias to_greyscale to_grayscale
|
171
|
+
|
172
|
+
def to_cmyk
|
173
|
+
self
|
174
|
+
end
|
175
|
+
|
176
|
+
def inspect
|
177
|
+
"CMYK [%.2f%%, %.2f%%, %.2f%%, %.2f%%]" % [cyan, magenta, yellow, black]
|
178
|
+
end
|
179
|
+
|
180
|
+
# Converts to RGB then YIQ.
|
181
|
+
def to_yiq
|
182
|
+
to_rgb.to_yiq
|
183
|
+
end
|
184
|
+
|
185
|
+
# Converts to RGB then HSL.
|
186
|
+
def to_hsl
|
187
|
+
to_rgb.to_hsl
|
188
|
+
end
|
189
|
+
|
190
|
+
# Returns the cyan (C) component of the CMYK colour as a percentage value.
|
191
|
+
def cyan
|
192
|
+
@c * 100.0
|
193
|
+
end
|
194
|
+
|
195
|
+
# Returns the cyan (C) component of the CMYK colour as a value in the
|
196
|
+
# range 0.0 .. 1.0.
|
197
|
+
def c
|
198
|
+
@c
|
199
|
+
end
|
200
|
+
|
201
|
+
# Sets the cyan (C) component of the CMYK colour as a percentage value.
|
202
|
+
def cyan=(cc)
|
203
|
+
@c = ColorLib.normalize(cc / 100.0)
|
204
|
+
end
|
205
|
+
|
206
|
+
# Sets the cyan (C) component of the CMYK colour as a value in the range
|
207
|
+
# 0.0 .. 1.0.
|
208
|
+
def c=(cc)
|
209
|
+
@c = ColorLib.normalize(cc)
|
210
|
+
end
|
211
|
+
|
212
|
+
# Returns the magenta (M) component of the CMYK colour as a percentage
|
213
|
+
# value.
|
214
|
+
def magenta
|
215
|
+
@m * 100.0
|
216
|
+
end
|
217
|
+
|
218
|
+
# Returns the magenta (M) component of the CMYK colour as a value in the
|
219
|
+
# range 0.0 .. 1.0.
|
220
|
+
def m
|
221
|
+
@m
|
222
|
+
end
|
223
|
+
|
224
|
+
# Sets the magenta (M) component of the CMYK colour as a percentage value.
|
225
|
+
def magenta=(mm)
|
226
|
+
@m = ColorLib.normalize(mm / 100.0)
|
227
|
+
end
|
228
|
+
|
229
|
+
# Sets the magenta (M) component of the CMYK colour as a value in the
|
230
|
+
# range 0.0 .. 1.0.
|
231
|
+
def m=(mm)
|
232
|
+
@m = ColorLib.normalize(mm)
|
233
|
+
end
|
234
|
+
|
235
|
+
# Returns the yellow (Y) component of the CMYK colour as a percentage
|
236
|
+
# value.
|
237
|
+
def yellow
|
238
|
+
@y * 100.0
|
239
|
+
end
|
240
|
+
|
241
|
+
# Returns the yellow (Y) component of the CMYK colour as a value in the
|
242
|
+
# range 0.0 .. 1.0.
|
243
|
+
def y
|
244
|
+
@y
|
245
|
+
end
|
246
|
+
|
247
|
+
# Sets the yellow (Y) component of the CMYK colour as a percentage value.
|
248
|
+
def yellow=(yy)
|
249
|
+
@y = ColorLib.normalize(yy / 100.0)
|
250
|
+
end
|
251
|
+
|
252
|
+
# Sets the yellow (Y) component of the CMYK colour as a value in the range
|
253
|
+
# 0.0 .. 1.0.
|
254
|
+
def y=(kk)
|
255
|
+
@y = ColorLib.normalize(kk)
|
256
|
+
end
|
257
|
+
|
258
|
+
# Returns the black (K) component of the CMYK colour as a percentage
|
259
|
+
# value.
|
260
|
+
def black
|
261
|
+
@k * 100.0
|
262
|
+
end
|
263
|
+
|
264
|
+
# Returns the black (K) component of the CMYK colour as a value in the
|
265
|
+
# range 0.0 .. 1.0.
|
266
|
+
def k
|
267
|
+
@k
|
268
|
+
end
|
269
|
+
|
270
|
+
# Sets the black (K) component of the CMYK colour as a percentage value.
|
271
|
+
def black=(kk)
|
272
|
+
@k = ColorLib.normalize(kk / 100.0)
|
273
|
+
end
|
274
|
+
|
275
|
+
# Sets the black (K) component of the CMYK colour as a value in the range
|
276
|
+
# 0.0 .. 1.0.
|
277
|
+
def k=(kk)
|
278
|
+
@k = ColorLib.normalize(kk)
|
279
|
+
end
|
280
|
+
end
|