Class: Vissen::Output::Palette
- Inherits:
-
Object
- Object
- Vissen::Output::Palette
- Defined in:
- lib/vissen/output/palette.rb
Overview
The Palette is, at its core, a transformation between a position (0..1) and a color value {(0..1) x 3\}. It can either be continous or be based on a pre-allocated lookup table.
Usage
The following example creates a continuous palette and acesses the color at index 0.42.
palette = Palette.new 0x11998e, 0x38ef7d, label: 'Quepal'
palette[0.42] => #21BD87
A discrete palette can also be created by specifying the number of steps to use.
palette = Palette.new 0x11998e, 0x38ef7d, steps: 5
palette[0.42] => #1BAF8A
Instance Attribute Summary collapse
-
#label ⇒ String?
readonly
The optional palette label.
Instance Method Summary collapse
-
#discretize(steps) ⇒ Palette
A new, discrete palette.
-
#freeze ⇒ self
Prevents both the palette colors and the label from changing.
-
#initialize(*colors, steps: nil, label: nil) ⇒ Palette
constructor
A new instance of Palette.
-
#inspect ⇒ String
Example output: “#42BEAF -> #020180 (rainbow)”.
-
#to_a(n) ⇒ Array<Color>
Discretize the palette into the given number of values.
Constructor Details
#initialize(*colors, steps: nil, label: nil) ⇒ Palette
Returns a new instance of Palette.
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/vissen/output/palette.rb', line 32 def initialize(*colors, steps: nil, label: nil) @colors = colors.map { |c| Color.from(c).freeze } @label = label if steps define_discrete_accessor steps else define_continous_accessor end freeze end |
Instance Attribute Details
#label ⇒ String? (readonly)
Returns the optional palette label.
24 25 26 |
# File 'lib/vissen/output/palette.rb', line 24 def label @label end |
Instance Method Details
#discretize(steps) ⇒ Palette
Returns a new, discrete palette.
57 58 59 |
# File 'lib/vissen/output/palette.rb', line 57 def discretize(steps) self.class.new(*@colors, steps: steps, label: @label) end |
#freeze ⇒ self
Prevents both the palette colors and the label from changing.
48 49 50 51 52 53 |
# File 'lib/vissen/output/palette.rb', line 48 def freeze @colors.freeze @label.freeze super end |
#inspect ⇒ String
Example output:
"#42BEAF -> #020180 (rainbow)"
75 76 77 78 79 |
# File 'lib/vissen/output/palette.rb', line 75 def inspect @colors.map(&:inspect).join(' -> ').tap do |base| break "#{base} (#{@label})" if @label end end |
#to_a(n) ⇒ Array<Color>
Discretize the palette into the given number of values. Palettes defined with a step count are sampled as if they where continuous.
66 67 68 |
# File 'lib/vissen/output/palette.rb', line 66 def to_a(n) Array.new(n) { |i| color_at(i.to_f / (n - 1)).freeze } end |