Class: Sass::Value::Color
- Inherits:
-
Object
- Object
- Sass::Value::Color
show all
- Includes:
- Sass::Value
- Defined in:
- lib/sass/value/color.rb,
lib/sass/value/color/space.rb,
lib/sass/value/color/channel.rb,
lib/sass/value/color/space/hsl.rb,
lib/sass/value/color/space/hwb.rb,
lib/sass/value/color/space/lab.rb,
lib/sass/value/color/space/lch.rb,
lib/sass/value/color/space/lms.rb,
lib/sass/value/color/space/rgb.rb,
lib/sass/value/color/space/srgb.rb,
lib/sass/value/color/conversions.rb,
lib/sass/value/color/space/oklab.rb,
lib/sass/value/color/space/oklch.rb,
lib/sass/value/color/space/utils.rb,
lib/sass/value/color/space/a98_rgb.rb,
lib/sass/value/color/space/rec2020.rb,
lib/sass/value/color/space/xyz_d50.rb,
lib/sass/value/color/space/xyz_d65.rb,
lib/sass/value/color/gamut_map_method.rb,
lib/sass/value/color/space/display_p3.rb,
lib/sass/value/color/space/srgb_linear.rb,
lib/sass/value/color/space/prophoto_rgb.rb,
lib/sass/value/color/interpolation_method.rb,
lib/sass/value/color/gamut_map_method/clip.rb,
lib/sass/value/color/space/display_p3_linear.rb,
lib/sass/value/color/gamut_map_method/local_minde.rb
Overview
Sass’s color type.
No matter what representation was originally used to create this color, all of its channels are accessible.
Instance Method Summary
collapse
#[], #assert_boolean, #assert_calculation, #assert_function, #assert_map, #assert_mixin, #assert_number, #assert_string, #at, #bracketed?, #eql?, #sass_index_to_array_index, #separator, #to_a, #to_bool, #to_map, #to_nil
Constructor Details
#initialize(red: nil, green: nil, blue: nil, alpha: nil, space: 'rgb') ⇒ Color
#initialize(hue: nil, saturation: nil, lightness: nil, alpha: nil, space: 'hsl') ⇒ Color
#initialize(hue: nil, whiteness: nil, blackness: nil, alpha: nil, space: 'hwb') ⇒ Color
#initialize(lightness: nil, a: nil, b: nil, alpha: nil, space: 'lab') ⇒ Color
#initialize(lightness: nil, a: nil, b: nil, alpha: nil, space: 'oklab') ⇒ Color
#initialize(lightness: nil, chroma: nil, hue: nil, alpha: nil, space: 'lch') ⇒ Color
#initialize(lightness: nil, chroma: nil, hue: nil, alpha: nil, space: 'oklch') ⇒ Color
#initialize(red: nil, green: nil, blue: nil, alpha: nil, space: 'a98-rgb') ⇒ Color
#initialize(red: nil, green: nil, blue: nil, alpha: nil, space: 'display-p3') ⇒ Color
#initialize(red: nil, green: nil, blue: nil, alpha: nil, space: 'display-p3-linear') ⇒ Color
#initialize(red: nil, green: nil, blue: nil, alpha: nil, space: 'prophoto-rgb') ⇒ Color
#initialize(red: nil, green: nil, blue: nil, alpha: nil, space: 'rec2020') ⇒ Color
#initialize(red: nil, green: nil, blue: nil, alpha: nil, space: 'srgb') ⇒ Color
#initialize(red: nil, green: nil, blue: nil, alpha: nil, space: 'srgb-linear') ⇒ Color
#initialize(x: nil, y: nil, z: nil, alpha: nil, space: 'xyz') ⇒ Color
#initialize(x: nil, y: nil, z: nil, alpha: nil, space: 'xyz-d50') ⇒ Color
#initialize(x: nil, y: nil, z: nil, alpha: nil, space: 'xyz-d65') ⇒ Color
Returns a new instance of Color.
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/sass/value/color.rb', line 52
def initialize(**options)
unless options.key?(:space)
options[:space] = case options
in { red: _, green: _, blue: _ }
'rgb'
in { hue: _, saturation: _, lightness: _ }
'hsl'
in { hue: _, whiteness: _, blackness: _ }
'hwb'
else
raise Sass::ScriptError.new('No color space found', 'space')
end
end
space = Space.from_name(options[:space])
keys = _assert_options(space, options)
_initialize_for_space_internal(space,
options[keys[0]],
options[keys[1]],
options[keys[2]],
options.fetch(:alpha, 1))
end
|
Instance Method Details
#==(other) ⇒ ::Boolean
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
# File 'lib/sass/value/color.rb', line 318
def ==(other)
return false unless other.is_a?(Sass::Value::Color)
if legacy?
return false unless other.legacy?
return false unless FuzzyMath.equals_nilable?(other.alpha_or_nil, alpha_or_nil)
if _space == other._space
FuzzyMath.equals_nilable?(other.channel0_or_nil, channel0_or_nil) &&
FuzzyMath.equals_nilable?(other.channel1_or_nil, channel1_or_nil) &&
FuzzyMath.equals_nilable?(other.channel2_or_nil, channel2_or_nil)
else
_to_space(Space::RGB) == other._to_space(Space::RGB)
end
else
other._space == _space &&
FuzzyMath.equals_nilable?(other.channel0_or_nil, channel0_or_nil) &&
FuzzyMath.equals_nilable?(other.channel1_or_nil, channel1_or_nil) &&
FuzzyMath.equals_nilable?(other.channel2_or_nil, channel2_or_nil) &&
FuzzyMath.equals_nilable?(other.alpha_or_nil, alpha_or_nil)
end
end
|
#alpha ⇒ Numeric
260
261
262
|
# File 'lib/sass/value/color.rb', line 260
def alpha
@alpha_or_nil.nil? ? 0 : @alpha_or_nil
end
|
#assert_color(_name = nil) ⇒ Color
353
354
355
|
# File 'lib/sass/value/color.rb', line 353
def assert_color(_name = nil)
self
end
|
#blackness ⇒ Numeric
313
314
315
|
# File 'lib/sass/value/color.rb', line 313
def blackness
_to_space(Space::HWB).channel('blackness')
end
|
#blue ⇒ Numeric
283
284
285
|
# File 'lib/sass/value/color.rb', line 283
def blue
_to_space(Space::RGB).channel('blue').round
end
|
#change(red: nil, green: nil, blue: nil, alpha: nil, space: 'rgb') ⇒ Color
#change(hue: nil, saturation: nil, lightness: nil, alpha: nil, space: 'hsl') ⇒ Color
#change(hue: nil, whiteness: nil, blackness: nil, alpha: nil, space: 'hwb') ⇒ Color
#change(lightness: nil, a: nil, b: nil, alpha: nil, space: 'lab') ⇒ Color
#change(lightness: nil, a: nil, b: nil, alpha: nil, space: 'oklab') ⇒ Color
#change(lightness: nil, chroma: nil, hue: nil, alpha: nil, space: 'lch') ⇒ Color
#change(lightness: nil, chroma: nil, hue: nil, alpha: nil, space: 'oklch') ⇒ Color
#change(red: nil, green: nil, blue: nil, alpha: nil, space: 'a98-rgb') ⇒ Color
#change(red: nil, green: nil, blue: nil, alpha: nil, space: 'display-p3') ⇒ Color
#change(red: nil, green: nil, blue: nil, alpha: nil, space: 'display-p3-linear') ⇒ Color
#change(red: nil, green: nil, blue: nil, alpha: nil, space: 'prophoto-rgb') ⇒ Color
#change(red: nil, green: nil, blue: nil, alpha: nil, space: 'rec2020') ⇒ Color
#change(red: nil, green: nil, blue: nil, alpha: nil, space: 'srgb') ⇒ Color
#change(red: nil, green: nil, blue: nil, alpha: nil, space: 'srgb-linear') ⇒ Color
#change(x: nil, y: nil, z: nil, alpha: nil, space: 'xyz') ⇒ Color
#change(x: nil, y: nil, z: nil, alpha: nil, space: 'xyz-d50') ⇒ Color
#change(x: nil, y: nil, z: nil, alpha: nil, space: 'xyz-d65') ⇒ Color
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
# File 'lib/sass/value/color.rb', line 206
def change(**options)
space_set_explictly = !options[:space].nil?
space = space_set_explictly ? Space.from_name(options[:space]) : _space
if legacy? && !space_set_explictly
case options
in { whiteness: _ } | { blackness: _ }
space = Space::HWB
in { saturation: _ } | { lightness: _ }
space = Space::HSL
in { hue: _ }
space = if _space == Space::HWB
Space::HWB
else
Space::HSL
end
in { red: _ } | { blue: _ } | { green: _ }
space = Space::RGB
else
end
if space != _space
end
end
keys = _assert_options(space, options)
color = _to_space(space)
changed_color = if space_set_explictly
Color.send(:for_space_internal,
space,
options.fetch(keys[0], color.channel0_or_nil),
options.fetch(keys[1], color.channel1_or_nil),
options.fetch(keys[2], color.channel2_or_nil),
options.fetch(:alpha, color.alpha_or_nil))
else
changed_channel0_or_nil = options[keys[0]]
changed_channel1_or_nil = options[keys[1]]
changed_channel2_or_nil = options[keys[2]]
changed_alpha_or_nil = options[:alpha]
Color.send(:for_space_internal,
space,
changed_channel0_or_nil.nil? ? color.channel0_or_nil : changed_channel0_or_nil,
changed_channel1_or_nil.nil? ? color.channel1_or_nil : changed_channel1_or_nil,
changed_channel2_or_nil.nil? ? color.channel2_or_nil : changed_channel2_or_nil,
changed_alpha_or_nil.nil? ? color.alpha_or_nil : changed_alpha_or_nil)
end
changed_color._to_space(_space)
end
|
#channel(channel, space: nil) ⇒ Numeric
118
119
120
121
122
123
124
125
126
127
128
|
# File 'lib/sass/value/color.rb', line 118
def channel(channel, space: nil)
return to_space(space).channel(channel) unless space.nil?
channels = _space.channels
return channel0 if channel == channels[0].name
return channel1 if channel == channels[1].name
return channel2 if channel == channels[2].name
return alpha if channel == 'alpha'
raise Sass::ScriptError.new("Color #{self} doesn't have a channel named \"#{channel}\".", channel)
end
|
#channel_missing?(channel) ⇒ ::Boolean
132
133
134
135
136
137
138
139
140
|
# File 'lib/sass/value/color.rb', line 132
def channel_missing?(channel)
channels = _space.channels
return channel0_missing? if channel == channels[0].name
return channel1_missing? if channel == channels[1].name
return channel2_missing? if channel == channels[2].name
return alpha_missing? if channel == 'alpha'
raise Sass::ScriptError.new("Color #{self} doesn't have a channel named \"#{channel}\".", channel)
end
|
#channel_powerless?(channel, space: nil) ⇒ ::Boolean
145
146
147
148
149
150
151
152
153
154
155
|
# File 'lib/sass/value/color.rb', line 145
def channel_powerless?(channel, space: nil)
return to_space(space).channel_powerless?(channel) unless space.nil?
channels = _space.channels
return channel0_powerless? if channel == channels[0].name
return channel1_powerless? if channel == channels[1].name
return channel2_powerless? if channel == channels[2].name
return false if channel == 'alpha'
raise Sass::ScriptError.new("Color #{self} doesn't have a channel named \"#{channel}\".", channel)
end
|
#channels ⇒ Array<Numeric>
111
112
113
|
# File 'lib/sass/value/color.rb', line 111
def channels
[channel0, channel1, channel2].freeze
end
|
#channels_or_nil ⇒ Array<Numeric, nil>
106
107
108
|
# File 'lib/sass/value/color.rb', line 106
def channels_or_nil
[channel0_or_nil, channel1_or_nil, channel2_or_nil].freeze
end
|
#green ⇒ Numeric
277
278
279
|
# File 'lib/sass/value/color.rb', line 277
def green
_to_space(Space::RGB).channel('green').round
end
|
#hash ⇒ Integer
342
343
344
345
346
347
348
349
350
|
# File 'lib/sass/value/color.rb', line 342
def hash
@hash ||= [
_space.name,
FuzzyMath._hash(channel0_or_nil),
FuzzyMath._hash(channel1_or_nil),
FuzzyMath._hash(channel2_or_nil),
FuzzyMath._hash(alpha_or_nil)
].hash
end
|
#hue ⇒ Numeric
289
290
291
|
# File 'lib/sass/value/color.rb', line 289
def hue
_to_space(Space::HSL).channel('hue')
end
|
#in_gamut?(space = nil) ⇒ ::Boolean
90
91
92
93
94
|
# File 'lib/sass/value/color.rb', line 90
def in_gamut?(space = nil)
return to_space(space)._in_gamut? unless space.nil?
_in_gamut?
end
|
#interpolate(other, method: nil, weight: nil) ⇒ Color
161
162
163
164
165
166
167
168
169
170
|
# File 'lib/sass/value/color.rb', line 161
def interpolate(other, method: nil, weight: nil)
interpolation_method = if !method.nil?
InterpolationMethod.new(_space, HueInterpolationMethod.from_name(method))
elsif !_space.polar?
InterpolationMethod.new(_space)
else
InterpolationMethod.new(_space, :shorter)
end
_interpolate(other, interpolation_method, weight:)
end
|
#legacy? ⇒ ::Boolean
265
266
267
|
# File 'lib/sass/value/color.rb', line 265
def legacy?
_space.legacy?
end
|
#lightness ⇒ Numeric
301
302
303
|
# File 'lib/sass/value/color.rb', line 301
def lightness
_to_space(Space::HSL).channel('lightness')
end
|
#red ⇒ Numeric
271
272
273
|
# File 'lib/sass/value/color.rb', line 271
def red
_to_space(Space::RGB).channel('red').round
end
|
#saturation ⇒ Numeric
295
296
297
|
# File 'lib/sass/value/color.rb', line 295
def saturation
_to_space(Space::HSL).channel('saturation')
end
|
#space ⇒ ::String
78
79
80
|
# File 'lib/sass/value/color.rb', line 78
def space
_space.name
end
|
#to_gamut(method:, space: nil) ⇒ Color
99
100
101
102
103
|
# File 'lib/sass/value/color.rb', line 99
def to_gamut(method:, space: nil)
return to_space(space).to_gamut(method:)._to_space(_space) unless space.nil?
_to_gamut(GamutMapMethod.from_name(method, 'method'))
end
|
#to_space(space) ⇒ Color
84
85
86
|
# File 'lib/sass/value/color.rb', line 84
def to_space(space)
_to_space(Space.from_name(space))
end
|
#whiteness ⇒ Numeric
307
308
309
|
# File 'lib/sass/value/color.rb', line 307
def whiteness
_to_space(Space::HWB).channel('whiteness')
end
|