Class: Unmagic::Color::HSL::Gradient::Linear
- Inherits:
-
Gradient::Base
- Object
- Gradient::Base
- Unmagic::Color::HSL::Gradient::Linear
- 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
Class Method Summary collapse
-
.color_class ⇒ Class
Get the HSL 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. Hue interpolation uses shortest-arc blending for smooth color wheel transitions.
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 |