Class: Vissen::Output::Palette

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(*colors, steps: nil, label: nil) ⇒ Palette

Returns a new instance of Palette.

Parameters:

  • colors (Array<Color>, Array<#to_a>)

    the colors to use in the palette.

  • steps (Integer) (defaults to: nil)

    the number of discrete palette values. The palette will be continuous if nil.

  • label (String) (defaults to: nil)

    the optional label to use when identifying the 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

#labelString? (readonly)

Returns the optional palette label.

Returns:

  • (String, nil)

    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.

Parameters:

  • steps (Fixnum)

    the number of discrete colors in the new palette.

Returns:

  • (Palette)

    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

#freezeself

Prevents both the palette colors and the label from changing.

Returns:

  • (self)


48
49
50
51
52
53
# File 'lib/vissen/output/palette.rb', line 48

def freeze
  @colors.freeze
  @label.freeze

  super
end

#inspectString

Example output:

"#42BEAF -> #020180 (rainbow)"

Returns:

  • (String)

    a string representation of the palette made up of the palette colors as well as the (optional) label.



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.

Parameters:

  • n (Integer)

    the number of discrete values to produce.

Returns:

  • (Array<Color>)

    an array of colors sampled from the palette.



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