Search code examples
algorithmlinear-algebraalgebra

Get color union possibilities to generate another color


I have a set of colors in RGB hexadecimal. I need to calculate the combination of colors to get a specified hexadecimal color.

Eg:

Input hex - #3A5F34

Output: 

1 - 20% #FF0000 + 80% #00AA33
2 - 30% #FFAA00 + 40% #0A33BB + 30% #FFFFFF
3 - ...
...

For instance, if I have only red, black and yellow, is it possible to generate another color (specified) from combinations of that set. The objective is show union possibilities to create a input color.

I made much search and did not find salient discussions for that problem.

In my ideas, this circled in universe of HEX - RGB conversion and union probability but, my tests don't give satisfactory results. Some imagination possibilities:

- Brute algorithm of unions calculation based on array of main colors (primary, secondary, etc)

- Artificial Intelligence to make a evolutive population of color unions?

Any ideas to make that solution (or light to that)?


Solution

  • This is a linear combination problem in 3 dimensions. You have a basis set of vectors. In your given example, these are

    b1 = (FF, 00, 00)
    b2 = (00, AA, 33)
    b3 = (FF, FF, FF)
    

    You are trying to find a linear combination of these three vectors to give you the vector (3A, 5F, 34). Convert the vectors to integers and apply standard methods for solving systems of linear equations:

    b1   = (255,   0,   0)
    b2   = (  0, 176,  51)
    b3   = (255, 255, 255)
    need = ( 58,  95,  52)
    

    You now need to find coefficients x (for b1), y (b2), z (b3) such that

    need = x*b1 + y*b2 + z*b3
    

    Expanded, this is:

    255x +   0y + 255z = 58
      0x + 176y + 255z = 95
      0x +  51y + 255z = 52
    

    You have three equations in three variables. Can you take it from here? Both Python and Java have linear solving packages to do the arithmetic for you -- no need for artificial intelligence.


    Get percentages

    The needed proportions are x, y, z. Note that these are not percentages: there is no guarantee that the given mixture comes out as coefficients that add to 1. Consider a simple case: full-intensity green 00FF00 and blue 0000FF, but we want something in the teal range 00C0C0. The resulting mixture is (0.0, 0.75, 0.75) -- not percentages.


    Determine multiple solutions

    This is a linear system, giving us three possible cases:

    1. Inconsistent: two of the equations are parallel planes, and there are no possible solutions.
    2. Point solution: the equations describe three planes, intersecting in a point. There is exactly one algebraic solution. It is possible that one or more of the coefficients is negative, which means that there is no practical solution.
    3. Multiple solutions: the equations intersect in a common line ... or all three equations describe the same plane. In either of these cases, solving the system results in a parameterized equation in one (line) or two (plane) variables. In either of these cases, there are infinite solutions.

    Whatever tool you choose (or write) to solve the system of equations should return a parameterized form for case 3.