Class: Unmagic::Color::OKLCH::Gradient::Linear

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

Overview

Linear gradient interpolation in OKLCH color space.

Creates perceptually uniform color transitions by interpolating lightness, chroma, and hue in the OKLCH color space. OKLCH gradients maintain consistent perceived brightness across the gradient.

## OKLCH Interpolation

OKLCH is a perceptually uniform color space, meaning equal steps in OKLCH values produce equal perceived differences in color. This makes OKLCH gradients ideal for UI design where consistent visual weight is important.

## Examples

# Perceptually uniform gradient
gradient = Unmagic::Color::OKLCH::Gradient::Linear.build(
  [
    ["oklch(0.5 0.15 30)", 0.0],
    ["oklch(0.7 0.15 240)", 1.0]
  ],
  direction: "to right"
)
bitmap = gradient.rasterize(width: 100)

# Simple two-color gradient
gradient = Unmagic::Color::OKLCH::Gradient::Linear.build(
  ["oklch(0.3 0.15 30)", "oklch(0.7 0.15 240)"],
  direction: "to bottom"
)
bitmap = gradient.rasterize(width: 1, height: 50)

# Angled gradient with color objects
gradient = Unmagic::Color::OKLCH::Gradient::Linear.build(
  [
    Unmagic::Color::OKLCH.new(lightness: 0.3, chroma: 0.15, hue: 30),
    Unmagic::Color::OKLCH.new(lightness: 0.7, chroma: 0.15, hue: 240)
  ],
  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 OKLCH color class.

Returns:

  • (Class)

    Unmagic::Color::OKLCH



53
54
55
# File 'lib/unmagic/color/oklch/gradient/linear.rb', line 53

def color_class
  Unmagic::Color::OKLCH
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. Colors are interpolated in perceptually uniform OKLCH space.

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



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

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