Module: Abachrome::ColorMixins::Blend

Defined in:
lib/abachrome/color_mixins/blend.rb

Instance Method Summary collapse

Instance Method Details

#blendAbachrome::Color

Interpolates between two colors, creating a new color that is a blend of the two. The blend happens in the specified color space or the current color space if none is provided. #

Examples:

Blend two colors equally

red.blend(blue, 0.5)

Blend with 25% of another color

red.blend(blue, 0.25)

Blend in a specific color space

red.blend(blue, 0.5, target_color_space: :oklab)

Parameters:

  • other (Abachrome::Color)

    The color to blend with

  • amount (Float, Integer, #to_d)

    The blend amount between 0 and 1, where 0 returns the original color and 1 returns the other color. Defaults to 0.5 (midpoint)

  • target_color_space (Symbol, nil)

    The color space to perform the blend in (optional)

Returns:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/abachrome/color_mixins/blend.rb', line 36

def blend(other, amount = 0.5, target_color_space: nil)
  amount = AbcDecimal(amount)

  source = target_color_space ? to_color_space(target_color_space) : self
  other = other.to_color_space(source.color_space)

  l1, a1, b1 = coordinates.map { |_| AbcDecimal(_) }
  l2, a2, b2 = other.coordinates.map { |_| AbcDecimal(_) }

  blended_l = (AbcDecimal(1 - amount) * l1)     + (AbcDecimal(amount) * l2)
  blended_a = (AbcDecimal(1 - amount) * a1)     + (AbcDecimal(amount) * a2)
  blended_b = (AbcDecimal(1 - amount) * b1)     + (AbcDecimal(amount) * b2)

  blended_alpha = alpha + ((other.alpha - alpha) * amount)

  Color.new(
    color_space,
    [blended_l, blended_a, blended_b],
    blended_alpha
  )
end

#blend!(other, amount = 0.5) ⇒ Abachrome::Color

Blends this color with another color by the specified amount. This is a destructive version of the blend method, modifying the current color in place.

this color and 1.0 is the other color (default: 0.5)

Parameters:

  • other (Abachrome::Color)

    The color to blend with

  • amount (Float) (defaults to: 0.5)

    The blend amount, between 0.0 and 1.0, where 0.0 is

Returns:



66
67
68
69
70
71
72
# File 'lib/abachrome/color_mixins/blend.rb', line 66

def blend!(other, amount = 0.5)
  blended = blend(other, amount)
  @color_space = blended.color_space
  @coordinates = blended.coordinates
  @alpha = blended.alpha
  self
end

#mix(other, amount = 0.5) ⇒ Abachrome::Color

Alias for the blend method that mixes two colors together.

Parameters:

  • other (Abachrome::Color)

    The color to mix with

  • amount (Float) (defaults to: 0.5)

    The amount to mix, between 0.0 and 1.0, where 0.0 returns the original color and 1.0 returns the other color (default: 0.5)

Returns:



79
80
81
# File 'lib/abachrome/color_mixins/blend.rb', line 79

def mix(other, amount = 0.5)
  blend(other, amount)
end

#mix!(other, amount = 0.5) ⇒ self

Mix the current color with another color.

This method is an alias for blend!. It combines the current color with the provided color at the specified amount.

Parameters:

  • other (Abachrome::Color)

    The color to mix with the current color

  • amount (Numeric) (defaults to: 0.5)

    The amount of the other color to mix in, from 0 to 1 (default: 0.5)

Returns:

  • (self)

    Returns the modified color object



91
92
93
# File 'lib/abachrome/color_mixins/blend.rb', line 91

def mix!(other, amount = 0.5)
  blend!(other, amount)
end