Class: Sass::Script::Color

Inherits:
Literal show all
Extended by:
Haml::Util
Defined in:
lib/sass/script/color.rb

Overview

A SassScript object representing a CSS color.

Constant Summary collapse

HTML4_COLORS =

A hash from color names to [red, green, blue] value arrays.

map_vals({
  'black'   => 0x000000,
  'silver'  => 0xc0c0c0,
  'gray'    => 0x808080,
  'white'   => 0xffffff,
  'maroon'  => 0x800000,
  'red'     => 0xff0000,
  'purple'  => 0x800080,
  'fuchsia' => 0xff00ff,
  'green'   => 0x008000,
  'lime'    => 0x00ff00,
  'olive'   => 0x808000,
  'yellow'  => 0xffff00,
  'navy'    => 0x000080,
  'blue'    => 0x0000ff,
  'teal'    => 0x008080,
  'aqua'    => 0x00ffff
}) {|color| (0..2).map {|n| color >> (n << 3) & 0xff}.reverse}
HTML4_COLORS_REVERSE =

A hash from [red, green, blue] value arrays to color names.

map_hash(HTML4_COLORS) {|k, v| [v, k]}

Constants included from Haml::Util

Haml::Util::RUBY_VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Haml::Util

assert_html_safe!, caller_info, check_encoding, def_static_method, enum_with_index, has?, map_hash, map_keys, map_vals, merge_adjacent_strings, powerset, rails_root, rails_xss_safe?, ruby1_8?, scope, silence_warnings, static_method_name, to_hash

Methods inherited from Literal

#==, #and, #assert_int!, #comma, #concat, #neq, #or, #perform, #to_bool, #to_i, #unary_div, #unary_minus, #unary_not

Methods inherited from Node

#perform

Constructor Details

#initialize(rgba) ⇒ Color

Constructs an RGB or RGBA color object. The RGB values must be between 0 and 255, and the alpha value is generally expected to be between 0 and 1. However, the alpha value can be greater than 1 in order to allow it to be used for color multiplication.

Parameters:

  • rgba (Array<Numeric>)

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

Raises:

  • (Sass::SyntaxError)

    if any color value isn't between 0 and 255, or the alpha value is negative



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/sass/script/color.rb', line 40

def initialize(rgba)
  @red, @green, @blue = rgba[0...3].map {|c| c.to_i}
  @alpha = rgba[3] ? rgba[3].to_f : 1
  super(nil)

  unless rgb.all? {|c| (0..255).include?(c)}
    raise Sass::SyntaxError.new("Color values must be between 0 and 255")
  end

  unless (0..1).include?(alpha)
    raise Sass::SyntaxError.new("Color opacity value must between 0 and 1")
  end
end

Instance Attribute Details

#alphaFixnum

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

Returns:

  • (Fixnum)


73
74
75
# File 'lib/sass/script/color.rb', line 73

def alpha
  @alpha
end

#blueFixnum (readonly)

The blue component of the color.

Returns:

  • (Fixnum)


67
68
69
# File 'lib/sass/script/color.rb', line 67

def blue
  @blue
end

#greenFixnum (readonly)

The green component of the color.

Returns:

  • (Fixnum)


62
63
64
# File 'lib/sass/script/color.rb', line 62

def green
  @green
end

#redFixnum (readonly)

The red component of the color.

Returns:

  • (Fixnum)


57
58
59
# File 'lib/sass/script/color.rb', line 57

def red
  @red
end

Instance Method Details

#alpha?Boolean

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

Returns:

  • (Boolean)


79
80
81
# File 'lib/sass/script/color.rb', line 79

def alpha?
  alpha < 1
end

#div(other) ⇒ Color

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::Color : Divides each of this color's RGB color channels by the other color's.

Literal : See Literal#div.

Parameters:

  • other (Literal)

    The right-hand side of the operator

Returns:

  • (Color)

    The resulting color

Raises:



198
199
200
201
202
203
204
# File 'lib/sass/script/color.rb', line 198

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

#eq(other) ⇒ Bool

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

Parameters:

  • other (Literal)

    The right-hand side of the operator

Returns:

  • (Bool)

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



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

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

#minus(other) ⇒ Color

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::Color : Subtracts each of the other color's RGB color channels from this color's.

Literal : See Literal#minus.

Parameters:

  • other (Literal)

    The right-hand side of the operator

Returns:

  • (Color)

    The resulting color

Raises:



152
153
154
155
156
157
158
# File 'lib/sass/script/color.rb', line 152

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

#mod(other) ⇒ Color

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::Color : Takes each of this color's RGB color channels modulo the other color's.

Literal : See Literal#mod.

Parameters:

  • other (Literal)

    The right-hand side of the operator

Returns:

  • (Color)

    The resulting color

Raises:



221
222
223
224
225
226
227
# File 'lib/sass/script/color.rb', line 221

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

#plus(other) ⇒ Color

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::Color : Adds each of the RGB color channels together.

Literal : See Literal#plus.

Parameters:

  • other (Literal)

    The right-hand side of the operator

Returns:

  • (Color)

    The resulting color

Raises:



129
130
131
132
133
134
135
# File 'lib/sass/script/color.rb', line 129

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

#rgbArray<Fixnum>

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

Returns:

  • (Array<Fixnum>)

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



98
99
100
# File 'lib/sass/script/color.rb', line 98

def rgb
  [red, green, blue]
end

#times(other) ⇒ Color

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::Color : Multiplies each of the RGB color channels together.

Literal : See Literal#times.

Parameters:

  • other (Literal)

    The right-hand side of the operator

Returns:

  • (Color)

    The resulting color

Raises:



175
176
177
178
179
180
181
# File 'lib/sass/script/color.rb', line 175

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

#to_sString Also known as: inspect

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



234
235
236
237
238
239
# File 'lib/sass/script/color.rb', line 234

def to_s
  return "rgba(#{rgb.join(', ')}, #{alpha % 1 == 0.0 ? alpha.to_i : alpha})" if alpha?
  return HTML4_COLORS_REVERSE[rgb] if HTML4_COLORS_REVERSE[rgb]
  red, green, blue = rgb.map { |num| num.to_s(16).rjust(2, '0') }
  "##{red}#{green}#{blue}"
end

#valueObject

Deprecated.

This will be removed in version 2.6.

See Also:



85
86
87
88
89
90
91
92
# File 'lib/sass/script/color.rb', line 85

def value
  warn <<END
DEPRECATION WARNING:
The Sass::Script::Color #value attribute is deprecated and will be
removed in version 2.6. Use the #rgb attribute instead.
END
  rgb
end