Class: Sass::Value::Color

Inherits:
Object
  • Object
show all
Includes:
Sass::Value
Defined in:
lib/sass/value/color.rb

Overview

Sass’s color type.

No matter what representation was originally used to create this color, all of its channels are accessible.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Sass::Value

#[], #assert_boolean, #assert_calculation, #assert_function, #assert_map, #assert_mixin, #assert_number, #assert_string, #at, #bracketed?, #eql?, #sass_index_to_array_index, #separator, #to_a, #to_bool, #to_map, #to_nil

Constructor Details

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

Returns a new instance of Color.

Parameters:

  • red (Numeric) (defaults to: nil)
  • green (Numeric) (defaults to: nil)
  • blue (Numeric) (defaults to: nil)
  • hue (Numeric) (defaults to: nil)
  • saturation (Numeric) (defaults to: nil)
  • lightness (Numeric) (defaults to: nil)
  • whiteness (Numeric) (defaults to: nil)
  • blackness (Numeric) (defaults to: nil)
  • alpha (Numeric) (defaults to: 1)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/sass/value/color.rb', line 22

def initialize(red: nil,
               green: nil,
               blue: nil,
               hue: nil,
               saturation: nil,
               lightness: nil,
               whiteness: nil,
               blackness: nil,
               alpha: 1)
  @alpha = alpha.nil? ? 1 : FuzzyMath.assert_between(alpha, 0, 1, 'alpha')
  if red && green && blue
    @red = FuzzyMath.assert_between(FuzzyMath.round(red), 0, 255, 'red')
    @green = FuzzyMath.assert_between(FuzzyMath.round(green), 0, 255, 'green')
    @blue = FuzzyMath.assert_between(FuzzyMath.round(blue), 0, 255, 'blue')
  elsif hue && saturation && lightness
    @hue = hue % 360
    @saturation = FuzzyMath.assert_between(saturation, 0, 100, 'saturation')
    @lightness = FuzzyMath.assert_between(lightness, 0, 100, 'lightness')
  elsif hue && whiteness && blackness
    @hue = hue % 360
    @whiteness = FuzzyMath.assert_between(whiteness, 0, 100, 'whiteness')
    @blackness = FuzzyMath.assert_between(blackness, 0, 100, 'blackness')
    hwb_to_rgb
    @whiteness = @blackness = nil
  else
    raise Sass::ScriptError, 'Invalid Color'
  end
end

Instance Attribute Details

#alphaNumeric (readonly)

Returns:

  • (Numeric)


104
105
106
# File 'lib/sass/value/color.rb', line 104

def alpha
  @alpha
end

Instance Method Details

#==(other) ⇒ ::Boolean

Returns:

  • (::Boolean)


149
150
151
152
153
154
155
# File 'lib/sass/value/color.rb', line 149

def ==(other)
  other.is_a?(Sass::Value::Color) &&
    other.red == red &&
    other.green == green &&
    other.blue == blue &&
    other.alpha == alpha
end

#assert_color(_name = nil) ⇒ Color

Returns:



163
164
165
# File 'lib/sass/value/color.rb', line 163

def assert_color(_name = nil)
  self
end

#blacknessNumeric

Returns:

  • (Numeric)


99
100
101
# File 'lib/sass/value/color.rb', line 99

def blackness
  @blackness ||= 100 - (Rational([red, green, blue].max, 255) * 100)
end

#blueInteger

Returns:

  • (Integer)


66
67
68
69
70
# File 'lib/sass/value/color.rb', line 66

def blue
  hsl_to_rgb unless defined?(@blue)

  @blue
end

#change(red: nil, green: nil, blue: nil, hue: nil, saturation: nil, lightness: nil, whiteness: nil, blackness: nil, alpha: nil) ⇒ Color

Parameters:

  • red (Numeric) (defaults to: nil)
  • green (Numeric) (defaults to: nil)
  • blue (Numeric) (defaults to: nil)
  • hue (Numeric) (defaults to: nil)
  • saturation (Numeric) (defaults to: nil)
  • lightness (Numeric) (defaults to: nil)
  • whiteness (Numeric) (defaults to: nil)
  • blackness (Numeric) (defaults to: nil)
  • alpha (Numeric) (defaults to: nil)

Returns:



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/sass/value/color.rb', line 116

def change(red: nil,
           green: nil,
           blue: nil,
           hue: nil,
           saturation: nil,
           lightness: nil,
           whiteness: nil,
           blackness: nil,
           alpha: nil)
  if whiteness || blackness
    Sass::Value::Color.new(hue: hue || self.hue,
                           whiteness: whiteness || self.whiteness,
                           blackness: blackness || self.blackness,
                           alpha: alpha || self.alpha)
  elsif hue || saturation || lightness
    Sass::Value::Color.new(hue: hue || self.hue,
                           saturation: saturation || self.saturation,
                           lightness: lightness || self.lightness,
                           alpha: alpha || self.alpha)
  elsif red || green || blue
    Sass::Value::Color.new(red: red ? FuzzyMath.round(red) : self.red,
                           green: green ? FuzzyMath.round(green) : self.green,
                           blue: blue ? FuzzyMath.round(blue) : self.blue,
                           alpha: alpha || self.alpha)
  else
    dup.instance_eval do
      @alpha = FuzzyMath.assert_between(alpha, 0, 1, 'alpha')
      self
    end
  end
end

#greenInteger

Returns:

  • (Integer)


59
60
61
62
63
# File 'lib/sass/value/color.rb', line 59

def green
  hsl_to_rgb unless defined?(@green)

  @green
end

#hashInteger

Returns:

  • (Integer)


158
159
160
# File 'lib/sass/value/color.rb', line 158

def hash
  @hash ||= [red, green, blue, alpha].hash
end

#hueNumeric

Returns:

  • (Numeric)


73
74
75
76
77
# File 'lib/sass/value/color.rb', line 73

def hue
  rgb_to_hsl unless defined?(@hue)

  @hue
end

#lightnessNumeric

Returns:

  • (Numeric)


87
88
89
90
91
# File 'lib/sass/value/color.rb', line 87

def lightness
  rgb_to_hsl unless defined?(@lightness)

  @lightness
end

#redInteger

Returns:

  • (Integer)


52
53
54
55
56
# File 'lib/sass/value/color.rb', line 52

def red
  hsl_to_rgb unless defined?(@red)

  @red
end

#saturationNumeric

Returns:

  • (Numeric)


80
81
82
83
84
# File 'lib/sass/value/color.rb', line 80

def saturation
  rgb_to_hsl unless defined?(@saturation)

  @saturation
end

#whitenessNumeric

Returns:

  • (Numeric)


94
95
96
# File 'lib/sass/value/color.rb', line 94

def whiteness
  @whiteness ||= Rational([red, green, blue].min, 255) * 100
end