Class: Unmagic::Color::RGB::Gradient::Linear

Inherits:
Gradient::Base show all
Defined in:
lib/unmagic/color/rgb/gradient/linear.rb

Overview

Linear gradient interpolation in RGB color space.

Creates smooth color transitions by interpolating RGB components linearly between color stops. Each color stop has a position (0.0-1.0) that defines where the color appears in the gradient.

RGB Interpolation

RGB gradients interpolate the red, green, and blue components separately. This can produce different visual results compared to HSL or OKLCH gradients, especially when transitioning through complementary colors.

Examples

# Simple horizontal gradient
gradient = Unmagic::Color::RGB::Gradient::Linear.build(
["#FF0000", "#0000FF"],
direction: "to right"
)
bitmap = gradient.rasterize(width: 10)
bitmap.pixels[0].map(&:to_hex)
#=> ["#ff0000", "#e60019", ..., "#0000ff"]

# Gradient with intermediate stops
gradient = Unmagic::Color::RGB::Gradient::Linear.build(
[
  ["#FF0000", 0.0],   # Red at start
  ["#00FF00", 0.5],   # Green at middle
  ["#0000FF", 1.0]    # Blue at end
],
direction: "to bottom"
)
bitmap = gradient.rasterize(width: 1, height: 20)

# Angled gradient
gradient = Unmagic::Color::RGB::Gradient::Linear.build(
["#FF0000", "#0000FF"],
direction: "45deg"
)
bitmap = gradient.rasterize(width: 100, height: 100)

# Use Stop objects directly
stops = [
Unmagic::Color::Gradient::Stop.new(
  color: Unmagic::Color::RGB.parse("#FF0000"),
  position: 0.0
),
Unmagic::Color::Gradient::Stop.new(
  color: Unmagic::Color::RGB.parse("#0000FF"),
  position: 1.0
)
]
direction = Unmagic::Color::Units::Degrees::Direction::LEFT_TO_RIGHT
gradient = Unmagic::Color::RGB::Gradient::Linear.new(stops, direction: direction)

Instance Attribute Summary

Attributes inherited from Gradient::Base

#direction, #stops

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Gradient::Base

build, #initialize

Constructor Details

This class inherits a constructor from Unmagic::Color::Gradient::Base

Class Method Details

.color_class ⇒ Class

Get the RGB color class.

Returns:

  • (Class) —

    Unmagic::Color::RGB



67
68
69
# File 'lib/unmagic/color/rgb/gradient/linear.rb', line 67

def color_class
  Unmagic::Color::RGB
end

Instance Method Details

#rasterize(width: 1, height: 1) ⇒ Bitmap

Rasterize the gradient to a bitmap.

Generates a bitmap containing the gradient with support for angled directions. The direction is determined by the gradient's direction parameter.

Parameters:

  • width (Integer) (defaults to: 1) —

    Width of the bitmap (default 1)

  • height (Integer) (defaults to: 1) —

    Height of the bitmap (default 1)

Returns:

  • (Bitmap) —

    A bitmap with the specified dimensions

Raises:

  • (Error) —

    If width or height is less than 1



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/unmagic/color/rgb/gradient/linear.rb', line 82

def rasterize(width: 1, height: 1)
  raise self.class::Error, "width must be at least 1" if width < 1
  raise self.class::Error, "height must be at least 1" if height < 1

  # Get the angle from the direction's "to" component
  degrees = @direction.to.value

  # Generate pixels row by row
  pixels = Array.new(height) do |y|
    Array.new(width) do |x|
      position = calculate_position(x, y, width, height, degrees)
      color_at_position(position)
    end
  end

  Unmagic::Color::Gradient::Bitmap.new(
    width: width,
    height: height,
    pixels: pixels,
  )
end