Class: Abachrome::Palette
- Inherits:
-
Object
- Object
- Abachrome::Palette
- Defined in:
- lib/abachrome/palette.rb
Instance Attribute Summary collapse
-
#colors ⇒ Object
readonly
Returns the value of attribute colors.
Instance Method Summary collapse
-
#[](index) ⇒ Abachrome::Color?
Access a color in the palette at the specified index.
-
#add(color) ⇒ Abachrome::Palette
(also: #<<)
Adds a color to the palette.
-
#average ⇒ Abachrome::Color?
Calculates the average color of the palette by finding the centroid in OKLAB space.
-
#blend_all(amount = 0.5) ⇒ Abachrome::Color?
Blends all colors in the palette together sequentially at the specified amount.
-
#clear ⇒ Abachrome::Palette
Clears all colors from the palette.
-
#each(&block) {|Abachrome::Color| ... } ⇒ Enumerator
Yields each color in the palette to the given block.
-
#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.
-
#empty? ⇒ Boolean
Returns whether the palette has no colors.
-
#first ⇒ Abachrome::Color?
Returns the first color in the palette.
-
#initialize(colors = []) ⇒ Abachrome::Palette
constructor
Initializes a new color palette with the given colors.
-
#inspect ⇒ String
Returns a string representation of the palette for inspection purposes.
-
#last ⇒ Abachrome::Color?
Returns the last color in the palette.
-
#map(&block) ⇒ Abachrome::Palette
Maps the palette by applying a block to each color.
-
#remove(color) ⇒ Abachrome::Palette
Removes the specified color from the palette.
-
#size ⇒ Integer
Returns the number of colors in the palette.
-
#slice(start, length = nil) ⇒ Abachrome::Palette
Slices the palette to create a new palette with a subset of colors.
-
#sort_by_lightness ⇒ Abachrome::Palette
Returns a new palette with colors sorted by lightness.
-
#sort_by_saturation ⇒ Abachrome::Palette
Returns a new palette with colors sorted by saturation from lowest to highest.
-
#to_a ⇒ Array<Abachrome::Color>
Returns a duplicate of the internal colors array.
-
#to_css(format: :hex) ⇒ Array<String>
Converts the colors in the palette to CSS-formatted strings.
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.
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
#colors ⇒ Object (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.
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
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 |
#average ⇒ Abachrome::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
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).
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 |
#clear ⇒ Abachrome::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.
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.
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” }
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.
62 63 64 |
# File 'lib/abachrome/palette.rb', line 62 def empty? @colors.empty? end |
#first ⇒ Abachrome::Color?
Returns the first color in the palette.
130 131 132 |
# File 'lib/abachrome/palette.rb', line 130 def first @colors.first end |
#inspect ⇒ String
Returns a string representation of the palette for inspection purposes.
228 229 230 |
# File 'lib/abachrome/palette.rb', line 228 def inspect "#<#{self.class} colors=#{@colors.map(&:to_s)}>" end |
#last ⇒ Abachrome::Color?
Returns the last color in the palette.
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 }
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.
36 37 38 39 |
# File 'lib/abachrome/palette.rb', line 36 def remove(color) @colors.delete(color) self end |
#size ⇒ Integer
Returns 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.
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_lightness ⇒ Abachrome::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.
146 147 148 |
# File 'lib/abachrome/palette.rb', line 146 def sort_by_lightness self.class.new(@colors.sort_by(&:lightness)) end |
#sort_by_saturation ⇒ Abachrome::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.
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_a ⇒ Array<Abachrome::Color>
Returns a duplicate of the internal colors array.
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.
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 |