Class: Unmagic::Color::Gradient::Bitmap
- Inherits:
-
Object
- Object
- Unmagic::Color::Gradient::Bitmap
- 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[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
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#pixels ⇒ Object
readonly
Returns the value of attribute pixels.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
-
#[](*args) ⇒ Color
Shortcut to access a pixel.
-
#at(x, y = 0) ⇒ Color
Access a pixel at the given coordinates.
-
#initialize(width:, height:, pixels:) ⇒ Bitmap
constructor
Create a new bitmap.
-
#to_a ⇒ Array<Color>
Convert to a flat 1D array of colors.
-
#to_ansi(fill: "█") ⇒ String
Convert to ANSI escape codes for terminal display.
Constructor Details
#initialize(width:, height:, pixels:) ⇒ Bitmap
Create a new bitmap.
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`.
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.
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.
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.
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 |