Class: XRVG::Palette

Inherits:
Object
  • Object
show all
Includes:
Attributable, Interpolation, Samplable
Defined in:
lib/color.rb

Overview

class Palette

Intro

Palette defines color palettes, as interpolation between color points. As such, use Interpolation module, so uses for the moment only linear interpolation. But once built with interpolation, palette provides a continuous color “interval”, and so is Samplable !

Use

palette  = Palette[ :colorlist, [ 0.0, Color.blue, 0.5, Color.orange, 1.0, Color.yellow ] ]
palette.rand( 10 )   # => return 10 random colors in palette
palette.color( 0.5 ) # => Color.orange

Direct Known Subclasses

Gradient

Instance Method Summary collapse

Methods included from Interpolation

#compute_simplebezier, #getcurve, #interpoltype, #linear, #simplebezier

Methods included from Samplable

#apply_samples, build, #mean, #sample, #samples

Methods included from FloatFunctor

#addfilter, #alternate, #apply, #applyhash, #compute, #filter, #generate, #geo, #geofull, #modify, #process, #random, #shuffle, #sin, #ssort, #transform, #transforms, #trigger

Constructor Details

#initialize(*args) ⇒ Palette

Returns a new instance of Palette.



291
292
293
294
# File 'lib/color.rb', line 291

def initialize( *args )
  super( *args )
  build_interpolators
end

Instance Method Details

#apply_sample(abs) ⇒ Object

:nodoc:



346
347
348
349
# File 'lib/color.rb', line 346

def apply_sample( abs ) #:nodoc:
  # Trace("Palette#apply_sample abs #{abs}")
  return self.color( abs )
end

#build_interpolatorsObject

build an interpolator by color componant



297
298
299
300
301
302
303
304
305
306
# File 'lib/color.rb', line 297

def build_interpolators()
  vlists = [[],[],[],[]]
  self.colorlist.foreach do |index, color|
    vlists[0] += [index, color.r ]
    vlists[1] += [index, color.g ]
    vlists[2] += [index, color.b ]
    vlists[3] += [index, color.a ]
  end
  @interpolators = vlists.map {|samplelist| Interpolator[ :samplelist, samplelist, :interpoltype, self.interpoltype]}
end

#color(dindex) ⇒ Object

compute color given float pourcentage.

Palette[ :colorlist, [ 0.0, Color.black, 1.0, Color.white ] ].sample( 0.5 ) => Color[0.5,0.5,0.5,1.O]

“sample” method as defined in Samplable module



330
331
332
333
# File 'lib/color.rb', line 330

def color(dindex)
  result = self.interpolate(dindex)
  return result
end

#interpolate(dindex) ⇒ Object

method overloading to delegate computation to componant interpolators



322
323
324
325
# File 'lib/color.rb', line 322

def interpolate( dindex )
  vs = self.interpolators.map {|inter| inter.interpolate( dindex )}
  return Color[ *vs ]
end

#interpolatorsObject

interpolators accessor (for debugging)



309
310
311
# File 'lib/color.rb', line 309

def interpolators()
  return @interpolators
end

#interpoltype=(value) ⇒ Object

overloading to reset interpolators if interpoltype changes



314
315
316
317
318
319
# File 'lib/color.rb', line 314

def interpoltype=(value)
  @interpoltype = value
  if @interpolators
    self.build_interpolators
  end
end

#reverseObject

return a new palette by reversing the current one



336
337
338
339
340
341
342
# File 'lib/color.rb', line 336

def reverse()
  newcolorlist = []
  self.colorlist.reverse.foreach do |color, index|
    newcolorlist += [(0.0..1.0).complement( index ), color]
  end
  return Palette[ :colorlist, newcolorlist ]
end