Class: SassC::Script::Value::Color

Inherits:
SassC::Script::Value show all
Defined in:
lib/sassc/script/value/color.rb

Overview

A SassScript object representing a CSS color. This class provides a very bare-bones system for storing a RGB(A) or HSL(A) color and converting it to a CSS color function.

If your Sass method accepts a color you will need to perform any needed color mathematics or transformations yourself.

Instance Attribute Summary collapse

Attributes inherited from SassC::Script::Value

#options, #source_range

Instance Method Summary collapse

Methods inherited from SassC::Script::Value

#assert_int!, #bracketed, #inspect, #null?, #separator, #to_a, #to_bool, #to_h, #to_i, #with_contents

Constructor Details

#initialize(red: nil, green: nil, blue: nil, hue: nil, saturation: nil, lightness: nil, alpha: 1.0) ⇒ Color

Creates a new color with (red, green, blue) or (hue, saturation, lightness values, plus an optional alpha transparency value.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sassc/script/value/color.rb', line 22

def initialize(red:nil, green:nil, blue:nil, hue:nil, saturation:nil, lightness:nil, alpha:1.0)
  if red && green && blue && alpha
    @mode = :rgba
    @red = SassC::Util.clamp(red.to_i, 0, 255)
    @green = SassC::Util.clamp(green.to_i, 0, 255)
    @blue = SassC::Util.clamp(blue.to_i, 0, 255)
    @alpha = SassC::Util.clamp(alpha.to_f, 0.0, 1.0)
  elsif hue && saturation && lightness && alpha
    @mode = :hsla
    @hue = SassC::Util.clamp(hue.to_i, 0, 360)
    @saturation = SassC::Util.clamp(saturation.to_i, 0, 100)
    @lightness = SassC::Util.clamp(lightness.to_i, 0, 100)
    @alpha = SassC::Util.clamp(alpha.to_f, 0.0, 1.0)
  else
    raise SassC::UnsupportedValue, "Unable to determine color configuration for "
  end
end

Instance Attribute Details

#alphaObject (readonly)

Returns the value of attribute alpha.



18
19
20
# File 'lib/sassc/script/value/color.rb', line 18

def alpha
  @alpha
end

#blueObject (readonly)

Returns the value of attribute blue.



14
15
16
# File 'lib/sassc/script/value/color.rb', line 14

def blue
  @blue
end

#greenObject (readonly)

Returns the value of attribute green.



13
14
15
# File 'lib/sassc/script/value/color.rb', line 13

def green
  @green
end

#hueObject (readonly)

Returns the value of attribute hue.



15
16
17
# File 'lib/sassc/script/value/color.rb', line 15

def hue
  @hue
end

#lightnessObject (readonly)

Returns the value of attribute lightness.



17
18
19
# File 'lib/sassc/script/value/color.rb', line 17

def lightness
  @lightness
end

#redObject (readonly)

Returns the value of attribute red.



12
13
14
# File 'lib/sassc/script/value/color.rb', line 12

def red
  @red
end

#saturationObject (readonly)

Returns the value of attribute saturation.



16
17
18
# File 'lib/sassc/script/value/color.rb', line 16

def saturation
  @saturation
end

Instance Method Details

#alpha_stringObject

Returns the alpha value of this color as a string and rounded to 8 decimal places.



66
67
68
# File 'lib/sassc/script/value/color.rb', line 66

def alpha_string
  alpha.round(8).to_s
end

#eql?(other_color) ⇒ Boolean Also known as: ==

True if this Color is equal to other_color

Returns:

  • (Boolean)


81
82
83
84
85
86
# File 'lib/sassc/script/value/color.rb', line 81

def eql?(other_color)
  unless other_color.is_a?(self.class)
    raise ArgumentError, "No implicit conversion of #{other_color.class} to #{self.class}"
  end
  self.value == other_color.value
end

#hashObject

Returns a numeric value for comparing two Color objects This method is used internally by the Hash class and is not the same as .to_h



91
92
93
# File 'lib/sassc/script/value/color.rb', line 91

def hash
  value.hash
end

#hlsa?Boolean

True if this color has HSLA values

Returns:

  • (Boolean)


60
61
62
# File 'lib/sassc/script/value/color.rb', line 60

def hlsa?
  @mode == :hlsa
end

#rgba?Boolean

True if this color has RGBA values

Returns:

  • (Boolean)


55
56
57
# File 'lib/sassc/script/value/color.rb', line 55

def rgba?
  @mode == :rgba
end

#to_sObject

Returns a CSS color declaration in the form ‘rgb(…)`, `rgba(…)`, `hsl(…)`, or `hsla(…)`.



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

def to_s
  if rgba? && @alpha == 1.0
    return "rgb(#{@red}, #{@green}, #{@blue})"
  elsif rgba?
    return "rgba(#{@red}, #{@green}, #{@blue}, #{alpha_string})"
  elsif hsla? && @alpha == 1.0
    return "hsl(#{@hue}, #{@saturation}%, #{@lightness}%)"
  else # hsla?
    return "hsla(#{@hue}, #{@saturation}%, #{@lightness}%, #{alpha_string})"
  end
end

#valueObject

Returns the values of this color in an array. Provided for compatibility between different SassC::Script::Value classes



72
73
74
75
76
77
78
# File 'lib/sassc/script/value/color.rb', line 72

def value
  return [
    red, green, blue,
    hue, saturation, lightness,
    alpha,
  ].compact
end