Class: Abachrome::Palette

Inherits:
Object
  • Object
show all
Defined in:
lib/abachrome/palette.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(colors = []) ⇒ Abachrome::Palette

Initializes a new color palette with the given colors. Automatically converts non-Color objects to Color objects by parsing them as hex values.

either a Color object or a string-convertible object representing a hex color code.

Parameters:

  • colors (Array) (defaults to: [])

    An array of colors to include in the palette. Each element can be



13
14
15
# File 'lib/abachrome/palette.rb', line 13

def initialize(colors = [])
  @colors = colors.map { |c| c.is_a?(Color) ? c : Color.from_hex(c.to_s) }
end

Instance Attribute Details

#colorsObject (readonly)

Returns the value of attribute colors.



5
6
7
# File 'lib/abachrome/palette.rb', line 5

def colors
  @colors
end

Instance Method Details

#[](index) ⇒ Abachrome::Color?

Access a color in the palette at the specified index.

Parameters:

  • index (Integer)

    The index of the color to retrieve from the palette

Returns:

  • (Abachrome::Color, nil)

    The color at the specified index, or nil if the index is out of bounds



112
113
114
# File 'lib/abachrome/palette.rb', line 112

def [](index)
  @colors[index]
end

#add(color) ⇒ Abachrome::Palette Also known as: <<

Adds a color to the palette. Accepts either an Abachrome::Color object or any object that can be converted to a string and parsed as a hex color code.

If not already an Abachrome::Color object, it will be converted using Color.from_hex

Parameters:

Returns:



24
25
26
27
28
# File 'lib/abachrome/palette.rb', line 24

def add(color)
  color = Color.from_hex(color.to_s) unless color.is_a?(Color)
  @colors << color
  self
end

#averageAbachrome::Color?

Calculates the average color of the palette by finding the centroid in OKLAB space. This method converts each color in the palette to OKLAB coordinates, calculates the arithmetic mean of these coordinates, and creates a new color from the average values. Alpha values are also averaged.

or nil if the palette is empty

Returns:



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/abachrome/palette.rb', line 184

def average
  return nil if empty?

  oklab_coords = @colors.map(&:to_oklab).map(&:coordinates)
  avg_coords = oklab_coords.reduce([0, 0, 0]) do |sum, coords|
    [sum[0] + coords[0], sum[1] + coords[1], sum[2] + coords[2]]
  end
  avg_coords.map! { |c| c / size }

  Color.new(
    ColorSpace.find(:oklab),
    avg_coords,
    @colors.map(&:alpha).sum / size
  )
end

#blend_all(amount = 0.5) ⇒ Abachrome::Color?

Blends all colors in the palette together sequentially at the specified amount. This method takes each color in the palette and blends it with the accumulated result of previous blends. It starts with the first color and progressively blends each subsequent color at the specified blend amount.

Defaults to 0.5 (equal blend).

Parameters:

  • amount (Float) (defaults to: 0.5)

    The blend amount to use between each color pair, between 0.0 and 1.0.

Returns:

  • (Abachrome::Color, nil)

    The final blended color result, or nil if the palette is empty.



167
168
169
170
171
172
173
174
175
# File 'lib/abachrome/palette.rb', line 167

def blend_all(amount = 0.5)
  return nil if empty?

  result = first
  @colors[1..].each do |color|
    result = result.blend(color, amount)
  end
  result
end

#clearAbachrome::Palette

Clears all colors from the palette.

This method removes all stored colors in the palette. It provides a way to reset the palette to an empty state while maintaining the same palette object.

Returns:



47
48
49
50
# File 'lib/abachrome/palette.rb', line 47

def clear
  @colors.clear
  self
end

#each(&block) {|Abachrome::Color| ... } ⇒ Enumerator

Yields each color in the palette to the given block.

Parameters:

  • block (Proc)

    The block to be executed for each color in the palette.

Yields:

Returns:

  • (Enumerator)

    Returns an Enumerator if no block is given.

See Also:

  • Enumerable#each


72
73
74
# File 'lib/abachrome/palette.rb', line 72

def each(&block)
  @colors.each(&block)
end

#each_with_index(&block) {|color, index| ... } ⇒ Enumerator, Array<Abachrome::Color>

Calls the given block once for each color in the palette, passing the color and its index as parameters.

palette.each_with_index { |color, index| puts “Color #index: #color” }

Parameters:

  • block (Proc)

    The block to be called for each color

Yields:

  • (color, index)

    Yields a color and its index

Yield Parameters:

  • color (Abachrome::Color)

    The color at the current position

  • index (Integer)

    The index of the current color

Returns:

  • (Enumerator)

    If no block is given, returns an Enumerator

  • (Array<Abachrome::Color>)

    Returns the array of colors if a block is given



86
87
88
# File 'lib/abachrome/palette.rb', line 86

def each_with_index(&block)
  @colors.each_with_index(&block)
end

#empty?Boolean

Returns whether the palette has no colors.

Returns:

  • (Boolean)

    true if the palette contains no colors, false otherwise



62
63
64
# File 'lib/abachrome/palette.rb', line 62

def empty?
  @colors.empty?
end

#firstAbachrome::Color?

Returns the first color in the palette.

Returns:

  • (Abachrome::Color, nil)

    The first color in the palette, or nil if the palette is empty.



130
131
132
# File 'lib/abachrome/palette.rb', line 130

def first
  @colors.first
end

#inspectString

Returns a string representation of the palette for inspection purposes.

Returns:

  • (String)

    A string containing the class name and a list of colors in the palette



228
229
230
# File 'lib/abachrome/palette.rb', line 228

def inspect
  "#<#{self.class} colors=#{@colors.map(&:to_s)}>"
end

#lastAbachrome::Color?

Returns the last color in the palette.

Returns:

  • (Abachrome::Color, nil)

    The last color in the palette or nil if palette is empty.



137
138
139
# File 'lib/abachrome/palette.rb', line 137

def last
  @colors.last
end

#map(&block) ⇒ Abachrome::Palette

Maps the palette by applying a block to each color.

# Convert all colors in palette to grayscale palette.map { |color| color.grayscale }

Parameters:

  • block (Proc)

    A block that takes a color and returns a new color.

Returns:



97
98
99
# File 'lib/abachrome/palette.rb', line 97

def map(&block)
  self.class.new(@colors.map(&block))
end

#remove(color) ⇒ Abachrome::Palette

Removes the specified color from the palette.

Parameters:

Returns:



36
37
38
39
# File 'lib/abachrome/palette.rb', line 36

def remove(color)
  @colors.delete(color)
  self
end

#sizeInteger

Returns the number of colors in the palette.

Returns:

  • (Integer)

    the number of colors in the palette



55
56
57
# File 'lib/abachrome/palette.rb', line 55

def size
  @colors.size
end

#slice(start, length = nil) ⇒ Abachrome::Palette

Slices the palette to create a new palette with a subset of colors.

returns a new palette containing the single color at that index. If start is a Range, length is ignored.

Parameters:

  • start (Integer)

    The starting index (or range) from which to start the slice.

  • length (Integer, nil) (defaults to: nil)

    The number of colors to include in the slice. If nil and start is an Integer,

Returns:



122
123
124
125
# File 'lib/abachrome/palette.rb', line 122

def slice(start, length = nil)
  new_colors = length ? @colors[start, length] : @colors[start]
  self.class.new(new_colors)
end

#sort_by_lightnessAbachrome::Palette

Returns a new palette with colors sorted by lightness. This method creates a new palette instance containing the same colors as the current palette but sorted in ascending order based on their lightness values.

Returns:



146
147
148
# File 'lib/abachrome/palette.rb', line 146

def sort_by_lightness
  self.class.new(@colors.sort_by(&:lightness))
end

#sort_by_saturationAbachrome::Palette

Returns a new palette with colors sorted by saturation from lowest to highest. Saturation is determined by the second coordinate (a*) in the OKLAB color space. Lower values represent less saturated colors, higher values represent more saturated colors.

Returns:

  • (Abachrome::Palette)

    A new palette instance with the same colors sorted by saturation



155
156
157
# File 'lib/abachrome/palette.rb', line 155

def sort_by_saturation
  self.class.new(@colors.sort_by { |c| c.to_oklab.coordinates[1] })
end

#to_aArray<Abachrome::Color>

Returns a duplicate of the internal colors array.

Returns:



104
105
106
# File 'lib/abachrome/palette.rb', line 104

def to_a
  @colors.dup
end

#to_css(format: :hex) ⇒ Array<String>

Converts the colors in the palette to CSS-formatted strings.

The format of the output can be specified with the format parameter.

:hex - Outputs colors in hexadecimal format (e.g., “#RRGGBB”) :rgb - Outputs colors in rgb() function format :oklab - Outputs colors in oklab() function format When any other value is provided, a default format is used.

Parameters:

  • format (Symbol) (defaults to: :hex)

    The format to use for the CSS color strings.

Returns:

  • (Array<String>)

    An array of CSS-formatted color strings



210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/abachrome/palette.rb', line 210

def to_css(format: :hex)
  to_a.map do |color|
    case format
    when :hex
      Outputs::CSS.format_hex(color)
    when :rgb
      Outputs::CSS.format_rgb(color)
    when :oklab
      Outputs::CSS.format_oklab(color)
    else
      Outputs::CSS.format(color)
    end
  end
end