color 1.7.1 → 2.0.0.pre.0
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 +5 -13
- data/CHANGELOG.md +298 -0
- data/CODE_OF_CONDUCT.md +128 -0
- data/CONTRIBUTING.md +70 -0
- data/CONTRIBUTORS.md +10 -0
- data/LICENCE.md +27 -0
- data/Manifest.txt +11 -21
- data/README.md +54 -0
- data/Rakefile +74 -53
- data/SECURITY.md +34 -0
- data/lib/color/cielab.rb +348 -0
- data/lib/color/cmyk.rb +279 -213
- data/lib/color/grayscale.rb +128 -160
- data/lib/color/hsl.rb +205 -173
- data/lib/color/rgb/colors.rb +177 -163
- data/lib/color/rgb.rb +534 -537
- data/lib/color/version.rb +5 -0
- data/lib/color/xyz.rb +214 -0
- data/lib/color/yiq.rb +91 -46
- data/lib/color.rb +208 -141
- data/test/fixtures/cielab.json +444 -0
- data/test/minitest_helper.rb +20 -4
- data/test/test_cmyk.rb +49 -71
- data/test/test_color.rb +58 -106
- data/test/test_grayscale.rb +35 -56
- data/test/test_hsl.rb +72 -76
- data/test/test_rgb.rb +195 -267
- data/test/test_yiq.rb +12 -30
- metadata +165 -150
- checksums.yaml.gz.sig +0 -0
- data/.autotest +0 -5
- data/.gemtest +0 -0
- data/.hoerc +0 -2
- data/.minitest.rb +0 -2
- data/.travis.yml +0 -35
- data/Contributing.rdoc +0 -60
- data/Gemfile +0 -9
- data/History.rdoc +0 -172
- data/Licence.rdoc +0 -27
- data/README.rdoc +0 -50
- data/lib/color/css.rb +0 -7
- data/lib/color/palette/adobecolor.rb +0 -260
- data/lib/color/palette/gimp.rb +0 -104
- data/lib/color/palette/monocontrast.rb +0 -164
- data/lib/color/palette.rb +0 -4
- data/lib/color/rgb/contrast.rb +0 -57
- data/lib/color/rgb/metallic.rb +0 -28
- data/test/test_adobecolor.rb +0 -405
- data/test/test_css.rb +0 -19
- data/test/test_gimp.rb +0 -87
- data/test/test_monocontrast.rb +0 -130
- data.tar.gz.sig +0 -0
- metadata.gz.sig +0 -0
data/lib/color/grayscale.rb
CHANGED
@@ -1,197 +1,165 @@
|
|
1
|
-
#
|
2
|
-
|
3
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
##
|
4
|
+
# \Grayscale is a color object representing shades of gray as a ratio of black to white,
|
5
|
+
# where 0% (0.0) gray is black and 100% (1.0) gray is white.
|
6
|
+
#
|
7
|
+
# \Grayscale colors are immutable Data class instances. Array deconstruction is `[gray]`
|
8
|
+
# and hash deconstruction is `{g:, gray:}`. See #g, #gray.
|
9
|
+
class Color::Grayscale
|
4
10
|
include Color
|
5
11
|
|
6
|
-
|
7
|
-
#
|
8
|
-
#
|
9
|
-
PDF_FORMAT_STR = "%.3f %s"
|
12
|
+
##
|
13
|
+
# :attr_reader: brightness
|
14
|
+
# Returns the grayscale value as a proportion of white (0.0 .. 1.0).
|
10
15
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
# Color::GreyScale.from_fraction(0.5)
|
15
|
-
def from_fraction(g = 0, &block)
|
16
|
-
new(g, 1.0, &block)
|
17
|
-
end
|
16
|
+
##
|
17
|
+
# :attr_reader: g
|
18
|
+
# Returns the grayscale value as a proportion of white (0.0 .. 1.0).
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
def from_percent(g = 0, &block)
|
23
|
-
new(g, &block)
|
24
|
-
end
|
25
|
-
end
|
20
|
+
##
|
21
|
+
# :attr_reader: gray
|
22
|
+
# Returns the grayscale value as a percentage of white (0.0 .. 100.0).
|
26
23
|
|
27
|
-
|
24
|
+
##
|
25
|
+
# Creates a grayscale color object from a percentage value (0.0 .. 100.0).
|
26
|
+
#
|
27
|
+
# ```ruby
|
28
|
+
# Color::Grayscale.from_percentage(50) # => Grayscale [0.50%]
|
29
|
+
# Color::Grayscale.from_values(50) # => Grayscale [0.50%]
|
30
|
+
# ```
|
28
31
|
#
|
29
|
-
#
|
30
|
-
|
31
|
-
|
32
|
-
|
32
|
+
# :call-seq:
|
33
|
+
# from_percentage(g)
|
34
|
+
# from_percentage(g:)
|
35
|
+
# from_values(g)
|
36
|
+
# from_values(g:)
|
37
|
+
def self.from_percentage(*args, **kwargs)
|
38
|
+
g =
|
39
|
+
case [args, kwargs]
|
40
|
+
in [[g], {}]
|
41
|
+
g
|
42
|
+
in [[], {g:}]
|
43
|
+
g
|
44
|
+
else
|
45
|
+
new(*args, **kwargs)
|
46
|
+
end
|
47
|
+
|
48
|
+
new(g: g / 100.0)
|
33
49
|
end
|
34
50
|
|
35
|
-
|
36
|
-
|
37
|
-
|
51
|
+
class << self
|
52
|
+
alias_method :from_values, :from_percentage
|
53
|
+
alias_method :from_fraction, :new
|
54
|
+
alias_method :from_internal, :from_fraction # :nodoc:
|
38
55
|
end
|
39
56
|
|
40
|
-
|
41
|
-
#
|
42
|
-
|
43
|
-
|
57
|
+
##
|
58
|
+
# Creates a grayscale color object from a fractional value (0.0 .. 1.0).
|
59
|
+
#
|
60
|
+
# ```ruby
|
61
|
+
# Color::Grayscale.from_fraction(0.5)
|
62
|
+
# Color::Grayscale.new(0.5)
|
63
|
+
# Color::Grayscale[g: 0.5]
|
64
|
+
# ```
|
65
|
+
def initialize(g:)
|
66
|
+
super(g: normalize(g))
|
44
67
|
end
|
45
68
|
|
46
|
-
|
47
|
-
#
|
48
|
-
def
|
49
|
-
PDF_FORMAT_STR % [ @g, "G" ]
|
50
|
-
end
|
69
|
+
##
|
70
|
+
# Coerces the other Color object to grayscale.
|
71
|
+
def coerce(other) = other.to_grayscale
|
51
72
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
private :to_255
|
73
|
+
##
|
74
|
+
# Convert \Grayscale to Color::CMYK.
|
75
|
+
def to_cmyk(...) = Color::CMYK.from_fraction(0, 0, 0, 1.0 - g.to_f)
|
56
76
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
"##{gs * 3}"
|
61
|
-
end
|
77
|
+
##
|
78
|
+
# Convert \Grayscale to Color::RGB.
|
79
|
+
def to_rgb(...) = Color::RGB.from_fraction(g, g, g)
|
62
80
|
|
63
|
-
|
64
|
-
|
65
|
-
def css_rgb
|
66
|
-
"rgb(%3.2f%%, %3.2f%%, %3.2f%%)" % [ gray, gray, gray ]
|
67
|
-
end
|
81
|
+
##
|
82
|
+
def to_grayscale(...) = self
|
68
83
|
|
69
|
-
|
70
|
-
#
|
71
|
-
|
72
|
-
|
84
|
+
##
|
85
|
+
# Convert \Grayscale to Color::YIQ.
|
86
|
+
#
|
87
|
+
# This approximates the actual value, as I and Q are calculated by treating the
|
88
|
+
# grayscale value as a RGB value. The Y (intensity or brightness) value is the same as
|
89
|
+
# the grayscale value.
|
90
|
+
def to_yiq(...)
|
91
|
+
y = g
|
92
|
+
i = (g * 0.596) + (g * -0.275) + (g * -0.321)
|
93
|
+
q = (g * 0.212) + (g * -0.523) + (g * 0.311)
|
94
|
+
Color::YIQ.from_fraction(y, i, q)
|
73
95
|
end
|
74
96
|
|
75
|
-
|
76
|
-
#
|
77
|
-
def
|
78
|
-
to_hsl.css_hsl
|
79
|
-
end
|
97
|
+
##
|
98
|
+
# Converts \Grayscale to Color::HSL.
|
99
|
+
def to_hsl(...) = Color::HSL.from_fraction(0, 0, g)
|
80
100
|
|
81
|
-
|
82
|
-
#
|
83
|
-
|
84
|
-
def css_hsla
|
85
|
-
to_hsl.css_hsla
|
86
|
-
end
|
101
|
+
##
|
102
|
+
# Converts \Grayscale to Color::CIELAB via Color::RGB.
|
103
|
+
def to_lab(...) = to_rgb(...).to_lab(...)
|
87
104
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
105
|
+
##
|
106
|
+
# Present the color as an HTML/CSS color string (e.g., `#dddddd`).
|
107
|
+
def html
|
108
|
+
"##{("%02x" % translate_range(g, to: 0.0..255.0)) * 3}"
|
92
109
|
end
|
93
110
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
111
|
+
##
|
112
|
+
# Present the color as a CSS `rgb` color with optional `alpha`.
|
113
|
+
#
|
114
|
+
# ```ruby
|
115
|
+
# Color::Grayscale[0.5].css # => rgb(50.00% 50.00% 50.00%)
|
116
|
+
# Color::Grayscale[0.5].css(alpha: 0.75) # => rgb(50.00% 50.00% 50.00% / 0.75)
|
117
|
+
# ```
|
118
|
+
def css(alpha: nil)
|
119
|
+
params = ([css_value(gray, :percent)] * 3).join(" ")
|
120
|
+
params = "#{params} / #{css_value(alpha)}" if alpha
|
98
121
|
|
99
|
-
|
100
|
-
def to_grayscale
|
101
|
-
self
|
122
|
+
"rgb(#{params})"
|
102
123
|
end
|
103
|
-
alias to_greyscale to_grayscale
|
104
124
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
Color::GrayScale.from_fraction(g)
|
109
|
-
end
|
125
|
+
##
|
126
|
+
# Lightens the grayscale color by the stated percent.
|
127
|
+
def lighten_by(percent) = Color::Grayscale.from_fraction([g + (g * (percent / 100.0)), 1.0].min)
|
110
128
|
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
Color::GrayScale.from_fraction(g)
|
115
|
-
end
|
129
|
+
##
|
130
|
+
# Darken the grayscale color by the stated percent.
|
131
|
+
def darken_by(percent) = Color::Grayscale.from_fraction([g - (g * (percent / 100.0)), 0.0].max)
|
116
132
|
|
117
|
-
|
118
|
-
|
119
|
-
# the greyscale value as an RGB value. The Y (intensity or brightness)
|
120
|
-
# value is the same as the greyscale value.
|
121
|
-
def to_yiq
|
122
|
-
y = @g
|
123
|
-
i = (@g * 0.596) + (@g * -0.275) + (@g * -0.321)
|
124
|
-
q = (@g * 0.212) + (@g * -0.523) + (@g * 0.311)
|
125
|
-
Color::YIQ.from_fraction(y, i, q)
|
126
|
-
end
|
133
|
+
##
|
134
|
+
alias_method :brightness, :g
|
127
135
|
|
128
|
-
|
129
|
-
def
|
130
|
-
Color::HSL.from_fraction(0, 0, @g)
|
131
|
-
end
|
136
|
+
##
|
137
|
+
def gray = g * 100.0
|
132
138
|
|
133
|
-
|
134
|
-
|
135
|
-
def brightness
|
136
|
-
@g
|
137
|
-
end
|
139
|
+
##
|
140
|
+
def inspect = "Grayscale [%.2f%%]" % [gray] # :nodoc:
|
138
141
|
|
139
|
-
|
140
|
-
#
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
# 0.0 .. 1.0.
|
147
|
-
def g
|
148
|
-
@g
|
149
|
-
end
|
150
|
-
# Sets the grayscale value as a percentage of white.
|
151
|
-
def gray=(gg)
|
152
|
-
@g = Color.normalize(gg / 100.0)
|
153
|
-
end
|
154
|
-
alias grey= gray= ;
|
155
|
-
# Returns the grayscale value as a fractional value of white in the range
|
156
|
-
# 0.0 .. 1.0.
|
157
|
-
def g=(gg)
|
158
|
-
@g = Color.normalize(gg)
|
142
|
+
##
|
143
|
+
def pretty_print(q) # :nodoc:
|
144
|
+
q.text "Grayscale"
|
145
|
+
q.breakable
|
146
|
+
q.group 2, "[", "]" do
|
147
|
+
q.text "%.2f%%" % gray
|
148
|
+
end
|
159
149
|
end
|
160
150
|
|
161
|
-
|
162
|
-
|
163
|
-
# #to_grayscale method on the other colour.
|
164
|
-
#
|
165
|
-
# The addition is done using the grayscale accessor methods to ensure a
|
166
|
-
# valid colour in the result.
|
167
|
-
def +(other)
|
168
|
-
self.class.from_fraction(g + other.to_grayscale.g)
|
169
|
-
end
|
151
|
+
##
|
152
|
+
def to_a = [gray] # :nodoc:
|
170
153
|
|
171
|
-
|
172
|
-
|
173
|
-
# a #to_grayscale method on the other colour.
|
174
|
-
#
|
175
|
-
# The subtraction is done using the grayscale accessor methods to ensure a
|
176
|
-
# valid colour in the result.
|
177
|
-
def -(other)
|
178
|
-
self + (-other)
|
179
|
-
end
|
154
|
+
##
|
155
|
+
alias_method :deconstruct, :to_a
|
180
156
|
|
181
|
-
|
182
|
-
|
183
|
-
end
|
157
|
+
##
|
158
|
+
def deconstruct_keys(_keys) = {g:, gray:}
|
184
159
|
|
185
|
-
|
186
|
-
|
187
|
-
end
|
160
|
+
##
|
161
|
+
def to_internal = [g] # :nodoc:
|
188
162
|
|
189
|
-
|
190
|
-
|
191
|
-
gs.instance_variable_set(:@g, -g)
|
192
|
-
gs
|
193
|
-
end
|
163
|
+
##
|
164
|
+
def components = 1 # :nodoc:
|
194
165
|
end
|
195
|
-
|
196
|
-
# A synonym for Color::GrayScale.
|
197
|
-
Color::GreyScale = Color::GrayScale
|