Class | Sass::Script::Color |
In: |
lib/sass/script/color.rb
|
Parent: | Literal |
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.
HTML4_COLORS | = | 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 | A hash from color names to `[red, green, blue]` value arrays. | |
HTML4_COLORS_REVERSE | = | map_hash(HTML4_COLORS) {|k, v| [v, k]} | A hash from `[red, green, blue]` value arrays to color names. |
Constructs an RGB or HSL color object, optionally with an alpha channel.
The RGB values must be between 0 and 255. The saturation and lightness values must be between 0 and 100. The alpha value must be between 0 and 1.
@raise [Sass::SyntaxError] if any color value isn‘t in the specified range
@overload initialize(attrs)
The attributes are specified as a hash. This hash must contain either `:hue`, `:saturation`, and `:value` keys, or `:red`, `:green`, and `:blue` keys. It cannot contain both HSL and RGB keys. It may also optionally contain an `:alpha` key. @param attrs [{Symbol => Numeric}] A hash of color attributes to values @raise [ArgumentError] if not enough attributes are specified, or both RGB and HSL attributes are specified
@overload initialize(rgba)
The attributes are specified as an array. This overload only supports RGB or RGBA colors. @param rgba [Array<Numeric>] A three- or four-element array of the red, green, blue, and optionally alpha values (respectively) of the color @raise [ArgumentError] if not enough attributes are specified
The alpha channel (opacity) of the color. This is 1 unless otherwise defined.
@return [Fixnum]
Returns whether this color object is translucent; that is, whether the alpha channel is non-1.
@return [Boolean]
The SassScript `/` operation. Its functionality depends on the type of its argument:
{Number} : Divides each of the RGB color channels by the number.
{Color} : Divides each of this color‘s RGB color channels by the other color‘s.
{Literal} : See {Literal#div}.
@param other [Literal] The right-hand side of the operator @return [Color] The resulting color @raise [Sass::SyntaxError] if `other` is a number with units
The SassScript `==` operation. **Note that this returns a {Sass::Script::Bool} object, not a Ruby boolean**.
@param other [Literal] The right-hand side of the operator @return [Bool] True if this literal is the same as the other,
false otherwise
Returns the hue, saturation, and lightness components of the color.
@return [Array<Fixnum>] A frozen three-element array of the
hue, saturation, and lightness values (respectively) of the color
The SassScript `-` operation. Its functionality depends on the type of its argument:
{Number} : Subtracts the number from each of the RGB color channels.
{Color} : Subtracts each of the other color‘s RGB color channels from this color‘s.
{Literal} : See {Literal#minus}.
@param other [Literal] The right-hand side of the operator @return [Color] The resulting color @raise [Sass::SyntaxError] if `other` is a number with units
The SassScript `%` operation. Its functionality depends on the type of its argument:
{Number} : Takes each of the RGB color channels module the number.
{Color} : Takes each of this color‘s RGB color channels modulo the other color‘s.
@param other [Number, Color] The right-hand side of the operator @return [Color] The resulting color @raise [Sass::SyntaxError] if `other` is a number with units
The SassScript `+` operation. Its functionality depends on the type of its argument:
{Number} : Adds the number to each of the RGB color channels.
{Color} : Adds each of the RGB color channels together.
{Literal} : See {Literal#plus}.
@param other [Literal] The right-hand side of the operator @return [Color] The resulting color @raise [Sass::SyntaxError] if `other` is a number with units
The SassScript `*` operation. Its functionality depends on the type of its argument:
{Number} : Multiplies the number by each of the RGB color channels.
{Color} : Multiplies each of the RGB color channels together.
@param other [Number, Color] The right-hand side of the operator @return [Color] The resulting color @raise [Sass::SyntaxError] if `other` is a number with units
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)
@param attrs [{Symbol => Numeric}]
A map of channel names (`:red`, `:green`, `:blue`, `:hue`, `:saturation`, `:lightness`, or `:alpha`) to values
@return [Color] The new Color object @raise [ArgumentError] if both RGB and HSL keys are specified