Module: Sai::Conversion::RGB::ColorTransformer Private

Defined in:
lib/sai/conversion/rgb/color_transformer.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Perform color transformations

Author:

Since:

  • 0.3.1

Class Method Summary collapse

Class Method Details

.darken(color, amount) ⇒ Array<Integer>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Darken an RGB color by a percentage

Parameters:

  • color (Array<Integer>, String, Symbol) —

    the color to darken

  • amount (Float) —

    amount to darken by (0.0-1.0)

Returns:

  • (Array<Integer>) —

    the darkened RGB values

Raises:

  • (ArgumentError) —

    if amount is not between 0.0 and 1.0

Author:

Since:

  • 0.3.1



29
30
31
32
33
34
# File 'lib/sai/conversion/rgb/color_transformer.rb', line 29

def darken(color, amount)
  raise ArgumentError, "Invalid amount: #{amount}" unless amount.between?(0.0, 1.0)

  rgb = ColorSpace.resolve(color)
  rgb.map { |c| [0, (c * (1 - amount)).round].max }
end

.gradient(start_color, end_color, steps) ⇒ Array<Array<Integer>>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generate a gradient between two colors with a specified number of steps

Parameters:

  • start_color (Array<Integer>, String, Symbol) —

    the starting color

  • end_color (Array<Integer>, String, Symbol) —

    the ending color

  • steps (Integer) —

    the number of colors to generate (minimum 2)

Returns:

  • (Array<Array<Integer>>) —

    the gradient colors as RGB values

Raises:

  • (ArgumentError) —

    if steps is less than 2

Author:

Since:

  • 0.3.1



54
55
56
57
58
59
60
61
# File 'lib/sai/conversion/rgb/color_transformer.rb', line 54

def gradient(start_color, end_color, steps)
  raise ArgumentError, "Steps must be at least 2, got: #{steps}" if steps < 2

  (0...steps).map do |i|
    step = i.to_f / (steps - 1)
    interpolate_color(start_color, end_color, step)
  end
end

.interpolate_color(start_color, end_color, step) ⇒ Array<Integer>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Interpolate between two colors to create a gradient step

Parameters:

  • start_color (Array<Integer>, String, Symbol) —

    the starting color

  • end_color (Array<Integer>, String, Symbol) —

    the ending color

  • step (Float) —

    the interpolation step (0.0-1.0)

Returns:

  • (Array<Integer>) —

    the interpolated RGB values

Raises:

  • (ArgumentError) —

    if step is not between 0.0 and 1.0

Author:

Since:

  • 0.3.1



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/sai/conversion/rgb/color_transformer.rb', line 81

def interpolate_color(start_color, end_color, step)
  raise ArgumentError, "Invalid step: #{step}" unless step.between?(0.0, 1.0)

  start_rgb = ColorSpace.resolve(start_color)
  end_rgb = ColorSpace.resolve(end_color)

  start_rgb.zip(end_rgb).map do |values|
    start_val, end_val = values
    next 0 unless start_val && end_val # Handle potential nil values

    (start_val + ((end_val - start_val) * step)).round.clamp(0, 255)
  end
end

.lighten(color, amount) ⇒ Array<Integer>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Lighten an RGB color by a percentage

Parameters:

  • color (Array<Integer>, String, Symbol) —

    the color to lighten

  • amount (Float) —

    amount to lighten by (0.0-1.0)

Returns:

  • (Array<Integer>) —

    the lightened RGB values

Raises:

  • (ArgumentError) —

    if amount is not between 0.0 and 1.0

Author:

Since:

  • 0.3.1



108
109
110
111
112
113
# File 'lib/sai/conversion/rgb/color_transformer.rb', line 108

def lighten(color, amount)
  raise ArgumentError, "Invalid amount: #{amount}" unless amount.between?(0.0, 1.0)

  rgb = ColorSpace.resolve(color)
  rgb.map { |c| [255, (c * (1 + amount)).round].min }
end

.rainbow_gradient(steps) ⇒ Array<Array<Integer>>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generate a rainbow gradient with a specified number of steps

Parameters:

  • steps (Integer) —

    the number of colors to generate (minimum 2)

Returns:

  • (Array<Array<Integer>>) —

    the rainbow gradient colors as RGB values

Raises:

  • (ArgumentError) —

    if steps is less than 2

Author:

Since:

  • 0.3.1



127
128
129
130
131
132
133
134
135
# File 'lib/sai/conversion/rgb/color_transformer.rb', line 127

def rainbow_gradient(steps)
  raise ArgumentError, "Steps must be at least 2, got: #{steps}" if steps < 2

  hue_step = 360.0 / steps
  (0...steps).map do |i|
    hue = (i * hue_step) % 360
    ColorSpace.hsv_to_rgb(hue, 1.0, 1.0)
  end
end