Class | Sass::Script::Color |
In: |
lib/sass/script/color.rb
|
Parent: | Literal |
A SassScript object representing a CSS color.
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. |
Creates a new color from RGB components. Note: when modifying the components of an existing color, use \{with} rather than creating a new color object. This preserves forwards-compatiblity for alpha channels and such.
@param rgb [Array<Fixnum>] A three-element array of the red, green, and blue values (respectively)
of the color
@raise [Sass::SyntaxError] if any color value isn‘t between 0 and 255
# File lib/sass/script/color.rb, line 38 38: def initialize(rgb) 39: rgb = rgb.map {|c| c.to_i} 40: raise Sass::SyntaxError.new("Color values must be between 0 and 255") if rgb.any? {|c| c < 0 || c > 255} 41: super(rgb.freeze) 42: end
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
# File lib/sass/script/color.rb, line 165 165: def div(other) 166: if other.is_a?(Sass::Script::Number) || other.is_a?(Sass::Script::Color) 167: piecewise(other, :/) 168: else 169: super 170: end 171: end
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
# File lib/sass/script/color.rb, line 122 122: def minus(other) 123: if other.is_a?(Sass::Script::Number) || other.is_a?(Sass::Script::Color) 124: piecewise(other, :-) 125: else 126: super 127: end 128: end
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
# File lib/sass/script/color.rb, line 185 185: def mod(other) 186: if other.is_a?(Sass::Script::Number) || other.is_a?(Sass::Script::Color) 187: piecewise(other, :%) 188: else 189: raise NoMethodError.new(nil, :mod) 190: end 191: end
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
# File lib/sass/script/color.rb, line 99 99: def plus(other) 100: if other.is_a?(Sass::Script::Number) || other.is_a?(Sass::Script::Color) 101: piecewise(other, :+) 102: else 103: super 104: end 105: end
Returns the red, green, and blue components of the color.
@return [Array<Fixnum>] A frozen three-element array of the red, green, and blue
values (respectively) of the color
# File lib/sass/script/color.rb, line 60 60: def rgb 61: @value 62: end
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
# File lib/sass/script/color.rb, line 142 142: def times(other) 143: if other.is_a?(Sass::Script::Number) || other.is_a?(Sass::Script::Color) 144: piecewise(other, :*) 145: else 146: raise NoMethodError.new(nil, :times) 147: end 148: end
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.
@return [String] The string representation
# File lib/sass/script/color.rb, line 198 198: def to_s 199: return HTML4_COLORS_REVERSE[rgb] if HTML4_COLORS_REVERSE[rgb] 200: red, green, blue = rgb.map { |num| num.to_s(16).rjust(2, '0') } 201: "##{red}#{green}#{blue}" 202: end
Returns a copy of this color with one or more channels changed.
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)
@param attrs [{Symbol => Fixnum}]
A map of channel names (`:red`, `:green`, or `:blue`) to values
@return [Color] The new Color object
# File lib/sass/script/color.rb, line 76 76: def with(attrs) 77: Color.new([ 78: attrs[:red] || rgb[0], 79: attrs[:green] || rgb[1], 80: attrs[:blue] || rgb[2], 81: ]) 82: end
# File lib/sass/script/color.rb, line 207 207: def piecewise(other, operation) 208: other_num = other.is_a? Number 209: if other_num && !other.unitless? 210: raise Sass::SyntaxError.new("Cannot add a number with units (#{other}) to a color (#{self}).") 211: end 212: 213: result = [] 214: for i in (0...3) 215: res = rgb[i].send(operation, other_num ? other.value : other.rgb[i]) 216: result[i] = [ [res, 255].min, 0 ].max 217: end 218: with(:red => result[0], :green => result[1], :blue => result[2]) 219: end