Class: Sass::Value::Color

Inherits:
Object
  • Object
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/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

Methods included from Sass::Value

#[], #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: '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.

Parameters:

  • red (Numeric)
  • green (Numeric)
  • blue (Numeric)
  • hue (Numeric)
  • saturation (Numeric)
  • lightness (Numeric)
  • whiteness (Numeric)
  • blackness (Numeric)
  • a (Numeric)
  • b (Numeric)
  • chroma (Numeric)
  • x (Numeric)
  • y (Numeric)
  • z (Numeric)
  • alpha (Numeric)
  • space (::String)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/sass/value/color.rb', line 51

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

Returns:

  • (::Boolean)


316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/sass/value/color.rb', line 316

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

#alphaNumeric

Returns:

  • (Numeric)


258
259
260
# File 'lib/sass/value/color.rb', line 258

def alpha
  @alpha_or_nil.nil? ? 0 : @alpha_or_nil
end

#assert_color(_name = nil) ⇒ Color

Returns:



351
352
353
# File 'lib/sass/value/color.rb', line 351

def assert_color(_name = nil)
  self
end

#blacknessNumeric

Deprecated.

Returns:

  • (Numeric)


311
312
313
# File 'lib/sass/value/color.rb', line 311

def blackness
  _to_space(Space::HWB).channel('blackness')
end

#blueNumeric

Deprecated.

Returns:

  • (Numeric)


281
282
283
# File 'lib/sass/value/color.rb', line 281

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: '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

Parameters:

  • red (Numeric)
  • green (Numeric)
  • blue (Numeric)
  • hue (Numeric)
  • saturation (Numeric)
  • lightness (Numeric)
  • whiteness (Numeric)
  • blackness (Numeric)
  • a (Numeric)
  • b (Numeric)
  • chroma (Numeric)
  • x (Numeric)
  • y (Numeric)
  • z (Numeric)
  • alpha (Numeric)
  • space (::String)

Returns:



204
205
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
# File 'lib/sass/value/color.rb', line 204

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
      # deprecated
    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

Parameters:

  • channel (::String)
  • space (::String) (defaults to: nil)

Returns:

  • (Numeric)

Raises:



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/sass/value/color.rb', line 117

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

Parameters:

  • channel (::String)

Returns:

  • (::Boolean)

Raises:



131
132
133
134
135
136
137
138
139
# File 'lib/sass/value/color.rb', line 131

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

Parameters:

  • channel (::String)
  • space (::String) (defaults to: nil)

Returns:

  • (::Boolean)

Raises:



144
145
146
147
148
149
150
151
152
153
154
# File 'lib/sass/value/color.rb', line 144

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

#channelsArray<Numeric>

Returns:

  • (Array<Numeric>)


110
111
112
# File 'lib/sass/value/color.rb', line 110

def channels
  [channel0, channel1, channel2].freeze
end

#channels_or_nilArray<Numeric, nil>

Returns:

  • (Array<Numeric, nil>)


105
106
107
# File 'lib/sass/value/color.rb', line 105

def channels_or_nil
  [channel0_or_nil, channel1_or_nil, channel2_or_nil].freeze
end

#greenNumeric

Deprecated.

Returns:

  • (Numeric)


275
276
277
# File 'lib/sass/value/color.rb', line 275

def green
  _to_space(Space::RGB).channel('green').round
end

#hashInteger

Returns:

  • (Integer)


340
341
342
343
344
345
346
347
348
# File 'lib/sass/value/color.rb', line 340

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

#hueNumeric

Deprecated.

Returns:

  • (Numeric)


287
288
289
# File 'lib/sass/value/color.rb', line 287

def hue
  _to_space(Space::HSL).channel('hue')
end

#in_gamut?(space = nil) ⇒ ::Boolean

Parameters:

  • space (::String) (defaults to: nil)

Returns:

  • (::Boolean)


89
90
91
92
93
# File 'lib/sass/value/color.rb', line 89

def in_gamut?(space = nil)
  return to_space(space)._in_gamut? unless space.nil?

  _in_gamut?
end

#interpolate(other, method: nil, weight: nil) ⇒ Color

Parameters:

  • other (Color)
  • method (::String) (defaults to: nil)
  • weight (Numeric) (defaults to: nil)

Returns:



160
161
162
163
164
165
166
167
168
169
# File 'lib/sass/value/color.rb', line 160

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

Returns:

  • (::Boolean)


263
264
265
# File 'lib/sass/value/color.rb', line 263

def legacy?
  _space.legacy?
end

#lightnessNumeric

Deprecated.

Returns:

  • (Numeric)


299
300
301
# File 'lib/sass/value/color.rb', line 299

def lightness
  _to_space(Space::HSL).channel('lightness')
end

#redNumeric

Deprecated.

Returns:

  • (Numeric)


269
270
271
# File 'lib/sass/value/color.rb', line 269

def red
  _to_space(Space::RGB).channel('red').round
end

#saturationNumeric

Deprecated.

Returns:

  • (Numeric)


293
294
295
# File 'lib/sass/value/color.rb', line 293

def saturation
  _to_space(Space::HSL).channel('saturation')
end

#space::String

Returns:

  • (::String)


77
78
79
# File 'lib/sass/value/color.rb', line 77

def space
  _space.name
end

#to_gamut(method:, space: nil) ⇒ Color

Parameters:

  • method (::String)
  • space (::String) (defaults to: nil)

Returns:



98
99
100
101
102
# File 'lib/sass/value/color.rb', line 98

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

Parameters:

  • space (::String)

Returns:



83
84
85
# File 'lib/sass/value/color.rb', line 83

def to_space(space)
  _to_space(Space.from_name(space))
end

#whitenessNumeric

Deprecated.

Returns:

  • (Numeric)


305
306
307
# File 'lib/sass/value/color.rb', line 305

def whiteness
  _to_space(Space::HWB).channel('whiteness')
end