Class: Unmagic::Color::HSL::Gradient::Linear

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

Overview

Linear gradient interpolation in HSL color space.

Creates smooth color transitions by interpolating hue, saturation, and lightness separately. HSL gradients often produce more visually pleasing results than RGB for transitions across the color wheel.

## HSL Interpolation

HSL gradients interpolate through the color wheel (hue), which can create smoother transitions through vivid colors. The hue component uses shortest-arc interpolation via the existing blend method.

## Examples

# Rainbow gradient
gradient = Unmagic::Color::HSL::Gradient::Linear.build(
  [
    ["hsl(0, 100%, 50%)", 0.0],     # Red
    ["hsl(120, 100%, 50%)", 0.5],   # Green
    ["hsl(240, 100%, 50%)", 1.0]    # Blue
  ],
  direction: "to right"
)
bitmap = gradient.rasterize(width: 100)

# Simple two-color gradient
gradient = Unmagic::Color::HSL::Gradient::Linear.build(
  ["hsl(0, 100%, 50%)", "hsl(240, 100%, 50%)"],
  direction: "to bottom"
)
bitmap = gradient.rasterize(width: 1, height: 50)

# Angled gradient with color objects
gradient = Unmagic::Color::HSL::Gradient::Linear.build(
  [
    Unmagic::Color::HSL.new(hue: 0, saturation: 100, lightness: 50),
    Unmagic::Color::HSL.new(hue: 240, saturation: 100, lightness: 50)
  ],
  direction: "45deg"
)
bitmap = gradient.rasterize(width: 100, height: 100)

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_classClass

Get the HSL color class.

Returns:

  • (Class)

    Unmagic::Color::HSL



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

def color_class
  Unmagic::Color::HSL
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. Hue interpolation uses shortest-arc blending for smooth color wheel transitions.

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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/unmagic/color/hsl/gradient/linear.rb', line 69

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