Skip to content

Instantly share code, notes, and snippets.

@horushe93
Last active November 18, 2024 02:22
Show Gist options
  • Save horushe93/d4596fbfc5bdcda083faed9493c1d8de to your computer and use it in GitHub Desktop.
Save horushe93/d4596fbfc5bdcda083faed9493c1d8de to your computer and use it in GitHub Desktop.
RGB Color Mixing Algorithm: A Mathematical Approach to Digital Color Blending

RGB Color Mixing Algorithm: A Mathematical Approach to Digital Color Blending

Color mixing in digital space is fundamentally different from mixing physical pigments. While artists blend paints using subtractive color mixing, digital displays use additive color mixing with RGB values. This implementation demonstrates a precise mathematical approach to mixing RGB colors with specific proportions.

The Algorithm

Here's a TypeScript implementation that handles proportional color mixing:

typescript
interface Color {
red: number; // 0-100 range
green: number; // 0-100 range
blue: number; // 0-100 range
}
interface ColorMix {
color: Color;
proportion: number; // percentage (0-100)
}
function mixColors(colorMixes: ColorMix[]): Color {
// Validate that proportions sum to 100%
const totalProportion = colorMixes.reduce((sum, mix) => sum + mix.proportion, 0);
if (Math.abs(totalProportion - 100) > 0.001) {
throw new Error('Color proportions must sum up to 100%');
}
// Initialize result color
const mixedColor: Color = {
red: 0,
blue: 0,
green: 0
};
// Calculate weighted average for each channel
colorMixes.forEach(mix => {
mixedColor.red += (mix.color.red mix.proportion) / 100;
mixedColor.blue += (mix.color.blue mix.proportion) / 100;
mixedColor.green += (mix.color.green mix.proportion) / 100;
});
return mixedColor;
}

How It Works

The algorithm employs a weighted average approach for each color channel (Red, Green, and Blue). Here's what makes it effective:

  1. Proportion Validation: Ensures all color proportions sum to 100%, maintaining color integrity
  2. Linear Interpolation: Uses weighted averages to calculate the final value of each channel
  3. Channel Independence: Processes each RGB channel separately, preserving color accuracy

Usage Example

typescript
const colorMixes: ColorMix[] = [
{ color: { red: 100, green: 0, blue: 0 }, proportion: 30 }, // Red
{ color: { red: 0, green: 100, blue: 0 }, proportion: 45 }, // Green
{ color: { red: 0, green: 0, blue: 100 }, proportion: 25 } // Blue
];
const result = mixColors(colorMixes);
// Result: { red: 30, green: 45, blue: 25 }

Key Features

  • Precision: Uses floating-point arithmetic for accurate color calculations
  • Type Safety: Leverages TypeScript interfaces for robust type checking
  • Error Handling: Validates input proportions to ensure mathematical correctness
  • Flexibility: Supports mixing any number of colors with different proportions

Real-world Applications

This algorithm finds practical applications in:

  • Digital painting applications
  • Color palette generators
  • UI/UX design tools
  • Data visualization
  • Digital art creation
  • Color mixing puzzles

Limitations and Considerations

While this implementation is mathematically sound, it's important to note:

  1. The algorithm uses the RGB color space, which might not perfectly match human color perception
  2. Results may vary slightly from physical color mixing due to the additive nature of digital colors
  3. Color values are normalized to 0-100 range for easier percentage calculations

Further Improvements

Potential enhancements could include:

  • Support for different color spaces (HSL, CMYK)
  • Gamma correction for more accurate visual results
  • Color temperature considerations
  • Perceptual color mixing models

References


Feel free to use, modify, and improve this implementation for your projects. Contributions and suggestions are welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment