Class: Fontisan::Models::ColorPalette

Inherits:
Lutaml::Model::Serializable
  • Object
show all
Defined in:
lib/fontisan/models/color_palette.rb

Overview

Color palette information model

Represents a color palette from the CPAL table. Each palette contains an array of RGBA colors in hex format that can be referenced by COLR layer palette indices.

This model uses lutaml-model for structured serialization to YAML/JSON/XML.

Examples:

Creating a color palette

palette = ColorPalette.new
palette.index = 0
palette.num_colors = 3
palette.colors = ["#FF0000FF", "#00FF00FF", "#0000FFFF"]

Serializing to YAML

yaml = palette.to_yaml
# index: 0
# num_colors: 3
# colors:
#   - "#FF0000FF"
#   - "#00FF00FF"
#   - "#0000FFFF"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#colorsArray<String>

Returns Array of hex color strings (#RRGGBBAA).

Returns:

  • (Array<String>)

    Array of hex color strings (#RRGGBBAA)



40
# File 'lib/fontisan/models/color_palette.rb', line 40

attribute :colors, :string, collection: true

#indexInteger

Returns Palette index (0-based).

Returns:

  • (Integer)

    Palette index (0-based)



32
# File 'lib/fontisan/models/color_palette.rb', line 32

attribute :index, :integer

#num_colorsInteger

Returns Number of colors in this palette.

Returns:

  • (Integer)

    Number of colors in this palette



36
# File 'lib/fontisan/models/color_palette.rb', line 36

attribute :num_colors, :integer

Instance Method Details

#color_at(color_index) ⇒ String?

Get a color by index

Parameters:

  • color_index (Integer)

    Color index within palette

Returns:

  • (String, nil)

    Hex color string or nil if invalid index



46
47
48
49
50
# File 'lib/fontisan/models/color_palette.rb', line 46

def color_at(color_index)
  return nil if color_index.negative? || color_index >= colors.length

  colors[color_index]
end

#empty?Boolean

Check if palette is empty

Returns:

  • (Boolean)

    True if no colors



55
56
57
# File 'lib/fontisan/models/color_palette.rb', line 55

def empty?
  colors.nil? || colors.empty?
end