Class: Sass::Script::Value::Color

Inherits:
Base
  • Object
show all
Defined in:
/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/color.rb

Overview

A SassScript object representing a CSS color.

A color may be represented internally as RGBA, HSLA, or both. It’s originally represented as whatever its input is; if it’s created with RGB values, it’s represented as RGBA, and if it’s created with HSL values, it’s represented as HSLA. Once a property is accessed that requires the other representation – for example, #red for an HSL color – that component is calculated and cached.

The alpha channel of a color is independent of its RGB or HSL representation. It’s always stored, as 1 if nothing else is specified. If only the alpha channel is modified using #with, the cached RGB and HSL values are retained.

Constant Summary

Instance Attribute Summary (collapse)

Attributes inherited from Base

#options, #source_range, #value

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from Base

#==, #_perform, #assert_int!, #eql?, #neq, #null?, #separator, #single_eq, #to_a, #to_bool, #to_h, #to_i, #unary_div, #unary_minus, #unary_not, #unary_plus

Constructor Details

- (Color) initialize(attrs) - (Color) initialize(rgba, [representation])

Constructs an RGB or HSL color object, optionally with an alpha channel.

RGB values are clipped within 0 and 255. Saturation and lightness values are clipped within 0 and 100. The alpha value is clipped within 0 and 1.

Overloads:

  • - (Color) initialize(attrs)

    The attributes are specified as a hash. This hash must contain either :hue, :saturation, and :lightness keys, or :red, :green, and :blue keys. It cannot contain both HSL and RGB keys. It may also optionally contain an :alpha key, and a :representation key indicating the original representation of the color that the user wrote in their stylesheet.

    Parameters:

    • attrs ({Symbol => Numeric})

      A hash of color attributes to values

    Raises:

    • (ArgumentError)

      if not enough attributes are specified, or both RGB and HSL attributes are specified

  • - (Color) initialize(rgba, [representation])

    The attributes are specified as an array. This overload only supports RGB or RGBA colors.

    Parameters:

    • rgba (Array<Numeric>)

      A three- or four-element array of the red, green, blue, and optionally alpha values (respectively) of the color

    • representation (String)

      The original representation of the color that the user wrote in their stylesheet.

    Raises:

    • (ArgumentError)

      if not enough attributes are specified

Raises:



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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/color.rb', line 229

def initialize(attrs, representation = nil, allow_both_rgb_and_hsl = false)
  super(nil)

  if attrs.is_a?(Array)
    unless (3..4).include?(attrs.size)
      raise ArgumentError.new("Color.new(array) expects a three- or four-element array")
    end

    red, green, blue = attrs[0...3].map {|c| Sass::Util.round(c)}
    @attrs = {:red => red, :green => green, :blue => blue}
    @attrs[:alpha] = attrs[3] ? attrs[3].to_f : 1
    @representation = representation
  else
    attrs = attrs.reject {|_k, v| v.nil?}
    hsl = [:hue, :saturation, :lightness] & attrs.keys
    rgb = [:red, :green, :blue] & attrs.keys
    if !allow_both_rgb_and_hsl && !hsl.empty? && !rgb.empty?
      raise ArgumentError.new("Color.new(hash) may not have both HSL and RGB keys specified")
    elsif hsl.empty? && rgb.empty?
      raise ArgumentError.new("Color.new(hash) must have either HSL or RGB keys specified")
    elsif !hsl.empty? && hsl.size != 3
      raise ArgumentError.new("Color.new(hash) must have all three HSL values specified")
    elsif !rgb.empty? && rgb.size != 3
      raise ArgumentError.new("Color.new(hash) must have all three RGB values specified")
    end

    @attrs = attrs
    @attrs[:hue] %= 360 if @attrs[:hue]
    @attrs[:alpha] ||= 1
    @representation = @attrs.delete(:representation)
  end

  [:red, :green, :blue].each do |k|
    next if @attrs[k].nil?
    @attrs[k] = Sass::Util.restrict(Sass::Util.round(@attrs[k]), 0..255)
  end

  [:saturation, :lightness].each do |k|
    next if @attrs[k].nil?
    @attrs[k] = Sass::Util.restrict(@attrs[k], 0..100)
  end

  @attrs[:alpha] = Sass::Util.restrict(@attrs[:alpha], 0..1)
end

Instance Attribute Details

- (String) representation (readonly)

The user’s original representation of the color.

Returns:



196
197
198
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/color.rb', line 196

def representation
  @representation
end

Class Method Details

+ (Color) from_hex(hex_string, alpha = nil)

Create a new color from a valid CSS hex string.

The leading hash is optional.

Returns:



279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/color.rb', line 279

def self.from_hex(hex_string, alpha = nil)
  unless hex_string =~ /^#?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i ||
         hex_string =~ /^#?([0-9a-f])([0-9a-f])([0-9a-f])$/i
    raise ArgumentError.new("#{hex_string.inspect} is not a valid hex color.")
  end
  red   = $1.ljust(2, $1).to_i(16)
  green = $2.ljust(2, $2).to_i(16)
  blue  = $3.ljust(2, $3).to_i(16)

  hex_string = "##{hex_string}" unless hex_string[0] == ?#
  attrs = {:red => red, :green => green, :blue => blue, :representation => hex_string}
  attrs[:alpha] = alpha if alpha
  new(attrs)
end

Instance Method Details

- (Fixnum) alpha

The alpha channel (opacity) of the color. This is 1 unless otherwise defined.

Returns:

  • (Fixnum)


346
347
348
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/color.rb', line 346

def alpha
  @attrs[:alpha].to_f
end

- (Boolean) alpha?

Returns whether this color object is translucent; that is, whether the alpha channel is non-1.

Returns:

  • (Boolean)


354
355
356
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/color.rb', line 354

def alpha?
  alpha < 1
end

- (Fixnum) blue

The blue component of the color.

Returns:

  • (Fixnum)


313
314
315
316
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/color.rb', line 313

def blue
  hsl_to_rgb!
  @attrs[:blue]
end

- (Color) div(other)

The SassScript / operation. Its functionality depends on the type of its argument:

Number
Divides each of the RGB color channels by the number.
Sass::Script::Value::Color
Divides each of this color’s RGB color channels by the other color’s.
Sass::Script::Value
See Base#div.

Parameters:

  • other (Value)

    The right-hand side of the operator

Returns:

  • (Color)

    The resulting color

Raises:



528
529
530
531
532
533
534
535
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/color.rb', line 528

def div(other)
  if other.is_a?(Sass::Script::Value::Number) ||
      other.is_a?(Sass::Script::Value::Color)
    piecewise(other, :/)
  else
    super
  end
end

- (Bool) eq(other)

The SassScript == operation. Note that this returns a Bool object, not a Ruby boolean.

Parameters:

  • other (Value)

    The right-hand side of the operator

Returns:

  • (Bool)

    True if this value is the same as the other, false otherwise



397
398
399
400
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/color.rb', line 397

def eq(other)
  Sass::Script::Value::Bool.new(
    other.is_a?(Color) && rgb == other.rgb && alpha == other.alpha)
end

- (Fixnum) green

The green component of the color.

Returns:

  • (Fixnum)


305
306
307
308
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/color.rb', line 305

def green
  hsl_to_rgb!
  @attrs[:green]
end

- hash



402
403
404
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/color.rb', line 402

def hash
  [rgb, alpha].hash
end

- (Array<Fixnum>) hsl

Returns the hue, saturation, and lightness components of the color.

Returns:

  • (Array<Fixnum>)

    A frozen three-element array of the hue, saturation, and lightness values (respectively) of the color



378
379
380
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/color.rb', line 378

def hsl
  [hue, saturation, lightness].freeze
end

- (Array<Fixnum>) hsla

Returns the hue, saturation, lightness, and alpha components of the color.

Returns:

  • (Array<Fixnum>)

    A frozen four-element array of the hue, saturation, lightness, and alpha values (respectively) of the color



386
387
388
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/color.rb', line 386

def hsla
  [hue, saturation, lightness, alpha].freeze
end

- (Numeric) hue

The hue component of the color.

Returns:

  • (Numeric)


321
322
323
324
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/color.rb', line 321

def hue
  rgb_to_hsl!
  @attrs[:hue]
end

- (String) inspect

Returns a string representation of the color.

Returns:



574
575
576
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/color.rb', line 574

def inspect
  alpha? ? rgba_str : hex_str
end

- (Numeric) lightness

The lightness component of the color.

Returns:

  • (Numeric)


337
338
339
340
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/color.rb', line 337

def lightness
  rgb_to_hsl!
  @attrs[:lightness]
end

- (Color) minus(other)

The SassScript - operation. Its functionality depends on the type of its argument:

Number
Subtracts the number from each of the RGB color channels.
Sass::Script::Value::Color
Subtracts each of the other color’s RGB color channels from this color’s.
Sass::Script::Value
See Base#minus.

Parameters:

  • other (Value)

    The right-hand side of the operator

Returns:

  • (Color)

    The resulting color

Raises:



485
486
487
488
489
490
491
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/color.rb', line 485

def minus(other)
  if other.is_a?(Sass::Script::Value::Number) || other.is_a?(Sass::Script::Value::Color)
    piecewise(other, :-)
  else
    super
  end
end

- (Color) mod(other)

The SassScript % operation. Its functionality depends on the type of its argument:

Number
Takes each of the RGB color channels module the number.
Sass::Script::Value::Color
Takes each of this color’s RGB color channels modulo the other color’s.

Parameters:

  • other (Number, Color)

    The right-hand side of the operator

Returns:

  • (Color)

    The resulting color

Raises:



549
550
551
552
553
554
555
556
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/color.rb', line 549

def mod(other)
  if other.is_a?(Sass::Script::Value::Number) ||
      other.is_a?(Sass::Script::Value::Color)
    piecewise(other, :%)
  else
    raise NoMethodError.new(nil, :mod)
  end
end

- (String?) name

Returns the color’s name, if it has one.

Returns:



581
582
583
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/color.rb', line 581

def name
  COLOR_NAMES_REVERSE[rgba]
end

- (Color) plus(other)

The SassScript + operation. Its functionality depends on the type of its argument:

Number
Adds the number to each of the RGB color channels.
Sass::Script::Value::Color
Adds each of the RGB color channels together.
Sass::Script::Value
See Base#plus.

Parameters:

  • other (Value)

    The right-hand side of the operator

Returns:

  • (Color)

    The resulting color

Raises:



462
463
464
465
466
467
468
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/color.rb', line 462

def plus(other)
  if other.is_a?(Sass::Script::Value::Number) || other.is_a?(Sass::Script::Value::Color)
    piecewise(other, :+)
  else
    super
  end
end

- (Fixnum) red

The red component of the color.

Returns:

  • (Fixnum)


297
298
299
300
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/color.rb', line 297

def red
  hsl_to_rgb!
  @attrs[:red]
end

- (Array<Fixnum>) rgb

Returns the red, green, and blue components of the color.

Returns:

  • (Array<Fixnum>)

    A frozen three-element array of the red, green, and blue values (respectively) of the color



362
363
364
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/color.rb', line 362

def rgb
  [red, green, blue].freeze
end

- (Array<Fixnum>) rgba

Returns the red, green, blue, and alpha components of the color.

Returns:

  • (Array<Fixnum>)

    A frozen four-element array of the red, green, blue, and alpha values (respectively) of the color



370
371
372
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/color.rb', line 370

def rgba
  [red, green, blue, alpha].freeze
end

- (Numeric) saturation

The saturation component of the color.

Returns:

  • (Numeric)


329
330
331
332
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/color.rb', line 329

def saturation
  rgb_to_hsl!
  @attrs[:saturation]
end

- (Color) times(other)

The SassScript * operation. Its functionality depends on the type of its argument:

Number
Multiplies the number by each of the RGB color channels.
Sass::Script::Value::Color
Multiplies each of the RGB color channels together.

Parameters:

  • other (Number, Color)

    The right-hand side of the operator

Returns:

  • (Color)

    The resulting color

Raises:



505
506
507
508
509
510
511
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/color.rb', line 505

def times(other)
  if other.is_a?(Sass::Script::Value::Number) || other.is_a?(Sass::Script::Value::Color)
    piecewise(other, :*)
  else
    raise NoMethodError.new(nil, :times)
  end
end

- (String) to_s(opts = {}) Also known as: to_sass

Returns a string representation of the color. This is usually the color’s hex value, but if the color has a name that’s used instead.

Returns:

  • (String)

    The string representation



563
564
565
566
567
568
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/color.rb', line 563

def to_s(opts = {})
  return smallest if options[:style] == :compressed
  return representation if representation
  return name if name
  alpha? ? rgba_str : hex_str
end

- (Color) with(attrs)

Returns a copy of this color with one or more channels changed. RGB or HSL colors may be changed, but not both at once.

For example:

Color.new([10, 20, 30]).with(:blue => 40)
  #=> rgb(10, 40, 30)
Color.new([126, 126, 126]).with(:red => 0, :green => 255)
  #=> rgb(0, 255, 126)
Color.new([255, 0, 127]).with(:saturation => 60)
  #=> rgb(204, 51, 127)
Color.new([1, 2, 3]).with(:alpha => 0.4)
  #=> rgba(1, 2, 3, 0.4)

Parameters:

  • attrs ({Symbol => Numeric})

    A map of channel names (:red, :green, :blue, :hue, :saturation, :lightness, or :alpha) to values

Returns:

  • (Color)

    The new Color object

Raises:

  • (ArgumentError)

    if both RGB and HSL keys are specified



425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/color.rb', line 425

def with(attrs)
  attrs = attrs.reject {|_k, v| v.nil?}
  hsl = !([:hue, :saturation, :lightness] & attrs.keys).empty?
  rgb = !([:red, :green, :blue] & attrs.keys).empty?
  if hsl && rgb
    raise ArgumentError.new("Cannot specify HSL and RGB values for a color at the same time")
  end

  if hsl
    [:hue, :saturation, :lightness].each {|k| attrs[k] ||= send(k)}
  elsif rgb
    [:red, :green, :blue].each {|k| attrs[k] ||= send(k)}
  else
    # If we're just changing the alpha channel,
    # keep all the HSL/RGB stuff we've calculated
    attrs = @attrs.merge(attrs)
  end
  attrs[:alpha] ||= alpha

  Color.new(attrs, nil, :allow_both_rgb_and_hsl)
end