Module: Abachrome::ColorMixins::Blend
- Defined in:
- lib/abachrome/color_mixins/blend.rb
Instance Method Summary collapse
-
#blend ⇒ Abachrome::Color
Interpolates between two colors, creating a new color that is a blend of the two.
-
#blend!(other, amount = 0.5) ⇒ Abachrome::Color
Blends this color with another color by the specified amount.
-
#mix(other, amount = 0.5) ⇒ Abachrome::Color
Alias for the blend method that mixes two colors together.
-
#mix!(other, amount = 0.5) ⇒ self
Mix the current color with another color.
Instance Method Details
#blend ⇒ Abachrome::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. #
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)
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.
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.
91 92 93 |
# File 'lib/abachrome/color_mixins/blend.rb', line 91 def mix!(other, amount = 0.5) blend!(other, amount) end |