Class: Unmagic::Color::Gradient::Bitmap

Inherits:
Object
  • Object
show all
Defined in:
lib/unmagic/color/gradient/bitmap.rb

Overview

A 2D grid of color pixels representing a rasterized gradient.

Bitmap represents the output of gradient rasterization as a grid of colors. For linear gradients, this is a single row (height=1). The 2D structure allows for future gradient types that need multiple rows.

Pixel Storage

Pixels are stored as a 2D array: pixels[y][x] = color

  • Linear gradients: pixels = [[color1, color2, ...]] (single row)
  • Multi-row gradients: pixels = [[row1...], [row2...], ...]

Examples

# Create a 1D bitmap (from linear gradient)
bitmap = Unmagic::Color::Gradient::Bitmap.new(
width: 5,
height: 1,
pixels: [[red, orange, yellow, green, blue]]
)

# Access pixels
bitmap.at(0, 0)  # => red (first pixel)
bitmap.at(4, 0)  # => blue (last pixel)
bitmap[]         # => red (shortcut for first pixel)

# Convert to flat array
bitmap.to_a   # => [red, orange, yellow, green, blue]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width:, height:, pixels:) ⇒ Bitmap

Create a new bitmap.

Parameters:

  • width (Integer) —

    Number of pixels horizontally

  • height (Integer) —

    Number of pixels vertically

  • pixels (Array<Array<Color>>) —

    2D array of colors (pixels[x])



43
44
45
46
47
# File 'lib/unmagic/color/gradient/bitmap.rb', line 43

def initialize(width:, height:, pixels:)
  @width = width
  @height = height
  @pixels = pixels
end

Instance Attribute Details

#height ⇒ Object (readonly)

Returns the value of attribute height.



36
37
38
# File 'lib/unmagic/color/gradient/bitmap.rb', line 36

def height
  @height
end

#pixels ⇒ Object (readonly)

Returns the value of attribute pixels.



36
37
38
# File 'lib/unmagic/color/gradient/bitmap.rb', line 36

def pixels
  @pixels
end

#width ⇒ Object (readonly)

Returns the value of attribute width.



36
37
38
# File 'lib/unmagic/color/gradient/bitmap.rb', line 36

def width
  @width
end

Instance Method Details

#[](*args) ⇒ Color

Shortcut to access a pixel.

When called without arguments, returns the first pixel (0, 0). When called with arguments, delegates to at.

Examples:

Get first pixel

bitmap[]  # => color at (0, 0)

Get specific pixel

bitmap[5, 0]  # => color at (5, 0)

Parameters:

  • args (Array) —

    Optional x and y coordinates

Returns:

  • (Color) —

    The color at the specified position



71
72
73
74
75
76
77
# File 'lib/unmagic/color/gradient/bitmap.rb', line 71

def [](*args)
  if args.empty?
    at(0, 0)
  else
    at(*args)
  end
end

#at(x, y = 0) ⇒ Color

Access a pixel at the given coordinates.

Parameters:

  • x (Integer) —

    Horizontal position (0 to width-1)

  • y (Integer) (defaults to: 0) —

    Vertical position (0 to height-1), defaults to 0

Returns:

  • (Color) —

    The color at the specified position



54
55
56
# File 'lib/unmagic/color/gradient/bitmap.rb', line 54

def at(x, y = 0)
  @pixels[y][x]
end

#to_a ⇒ Array<Color>

Convert to a flat 1D array of colors.

Flattens the 2D pixel grid into a single array, reading left-to-right, top-to-bottom.

Returns:

  • (Array<Color>) —

    Flattened array of all colors



85
86
87
# File 'lib/unmagic/color/gradient/bitmap.rb', line 85

def to_a
  @pixels.flatten
end

#to_ansi(fill: "█") ⇒ String

Convert to ANSI escape codes for terminal display.

Renders each pixel as a colored character using 24-bit true color ANSI codes. Each row is joined and rows are separated by newlines.

Examples:

Render a rainbow gradient

gradient = Gradient.linear(%w[red yellow green blue])
bitmap = gradient.rasterize(width: 40)
puts bitmap.to_ansi

Use custom fill character

puts bitmap.to_ansi(fill: "▀")

Parameters:

  • fill (String) (defaults to: "█") —

    Character to use for each pixel (default: "█")

Returns:

  • (String) —

    ANSI-colored string representation



104
105
106
107
108
# File 'lib/unmagic/color/gradient/bitmap.rb', line 104

def to_ansi(fill: "█")
  @pixels.map do |row|
    row.map { |color| "\e[#{color.to_ansi}m#{fill}\e[0m" }.join
  end.join("\n")
end