Class: Unmagic::Color::OKLCH::Gradient::Linear
- Inherits:
-
Gradient::Base
- Object
- Gradient::Base
- Unmagic::Color::OKLCH::Gradient::Linear
- 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
Class Method Summary collapse
-
.color_class ⇒ Class
Get the OKLCH color class.
Instance Method Summary collapse
-
#rasterize(width: 1, height: 1) ⇒ Bitmap
Rasterize the gradient to a bitmap.
Methods inherited from Gradient::Base
Constructor Details
This class inherits a constructor from Unmagic::Color::Gradient::Base
Class Method Details
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.
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 |