Module: Abachrome::ColorMixins::SpectralMix

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

Instance Method Summary collapse

Instance Method Details

#spectral_mix(other, amount = 0.5, tinting_strength_self: 1.0, tinting_strength_other: 1.0) ⇒ Abachrome::Color

Mix this color with another color using Kubelka-Munk spectral mixing.

This method produces more realistic color mixing than simple RGB or LAB interpolation by simulating how real pigments absorb and scatter light.

Examples:

Mix red and blue equally

red = Abachrome.from_rgb(1, 0, 0)
blue = Abachrome.from_rgb(0, 0, 1)
purple = red.spectral_mix(blue, 0.5)

Mix with 25% of blue

mostly_red = red.spectral_mix(blue, 0.25)

Mix with different tinting strengths

# Stronger blue pigment
purple = red.spectral_mix(blue, 0.5, tinting_strength_other: 2.0)

Parameters:

  • other (Abachrome::Color)

    The color to mix with

  • amount (Float) (defaults to: 0.5)

    The mix ratio, between 0 and 1. 0.5 means equal mixing. Values closer to 0 favor this color, values closer to 1 favor the other color.

  • tinting_strength_self (Float) (defaults to: 1.0)

    Tinting strength of this color (default: 1.0) Higher values mean stronger pigment concentration

  • tinting_strength_other (Float) (defaults to: 1.0)

    Tinting strength of other color (default: 1.0)

Returns:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/abachrome/color_mixins/spectral_mix.rb', line 46

def spectral_mix(other, amount = 0.5, tinting_strength_self: 1.0, tinting_strength_other: 1.0)
  require_relative "../spectral"

  # Convert amount to weights
  # amount = 0 means 100% self, 0% other
  # amount = 0.5 means 50% self, 50% other
  # amount = 1 means 0% self, 100% other
  weight_self = 1.0 - amount.to_f
  weight_other = amount.to_f

  colors = [
    { color: self, weight: weight_self },
    { color: other, weight: weight_other }
  ]

  tinting_strengths = {
    self => tinting_strength_self.to_f,
    other => tinting_strength_other.to_f
  }

  Spectral.mix(colors, tinting_strengths: tinting_strengths)
end