Class: Transform::Parser::Color

Inherits:
Object
  • Object
show all
Defined in:
lib/source/redshift/transform.rb

Overview

Class Color is responsible for handling css color values in either hex (#fff) or rgb triplet (rgb(0,0,0)) formats by parsing them to an array of numbers, transforming each elemement in the array during a transition and finally serving the then number at the end of a series of transforms.

Class Method Summary collapse

Class Method Details

.compute(from, to, delta) ⇒ Object

call-seq:

Parser::Color.compute(array,array,float) -> string

Computes from an array of current values to an array of new values based on a delta by computing each value in the array. returns a css-compatible rgb triplet string (rgb(00,00,00))

In a sequence of steps converting from rgb(0,0,0) to rgb(240,240,240) Parser::Color.compute may be called with Parser::Color.compute(, [240,240,240], 0.8272711231312) and will return ‘rbg(199,199,199)’



202
203
204
205
206
207
208
# File 'lib/source/redshift/transform.rb', line 202

def self.compute(from, to, delta)
  			rgb = []
  			from.each do |i|
 rgb << `Math.round(#{::Transform.compute(from[i], to[i], delta)})`
			  end
  `'rgb(' + rgb + ')'`
end

.hex_to_array(color) ⇒ Object



180
181
182
183
184
185
186
187
188
189
# File 'lib/source/redshift/transform.rb', line 180

def self.hex_to_array(color)
  `var hex = color.match(/^#?(\\w{1,2})(\\w{1,2})(\\w{1,2})$/).slice(1)`
  `var rgb = []
for(i = 0, l = hex.length; i < hex.length; i++){
  value = hex[i]
  if (value.length == 1) value += value;
  rgb[i] = parseInt(value,16);
}`
`rgb`
end

.parse(value) ⇒ Object

call-seq:

Parser::Color.parse(str) -> array or false

Parses a css color value from either hex (#f0f0f0) or rgb triplet (rbg(240,240,240)) to an array of rgb values or returns false if the value cannot be parsed

Parser::Color.parse(‘#fff’) # => [255,255,255] Parser::Color.parse(‘f0f0f0’) # => [240,240,240] Parser::Color.parse(‘rbg(240,240,240)) # => [240,240,240]

Parser::Color.parse(‘40px’) # => false Parser::Color.parse(‘left’) # => false



221
222
223
224
225
# File 'lib/source/redshift/transform.rb', line 221

def self.parse(value) # :nodoc
  `value = value.__value__ || String(value)`
  `if (value.match(/^#[0-9a-f]{3,6}$/i)) return #{Transform::Parser::Color.hex_to_array(value)}`
  			`((value = value.match(/(\\d+),\\s*(\\d+),\\s*(\\d+)/))) ? #{[value[1], value[2], value[3]]} : false`
end

.serve(value, unit) ⇒ Object

Returns the final value in rgb triplet format at the termination of a series of transformations The second argument (unit) exists for polymorphic calls and is not used.



229
230
231
# File 'lib/source/redshift/transform.rb', line 229

def self.serve(value, unit)
  value
end