Color fading function I found this old color fading function in my snippets folder and would like to implement it to one of my projects. It can be used to fade one color to another. It's a very long one-liner:

D3DCOLOR GetFadedColor(D3DCOLOR from, D3DCOLOR to, float factor)
{
    return (factor&lt;0.0f)?from:((factor&gt;1.0f)?to:(((((from&gt;&gt;24)&gt;(to&gt;&gt;24))?((from&gt;&gt;24)-
    (D3DCOLOR)(factor*(float)((from&gt;&gt;24)-(to&gt;&gt;24)))):((from&gt;&gt;24)+(D3DCOLOR)(factor*(float)
    ((to&gt;&gt;24)-(from&gt;&gt;24)))))&lt;&lt;24)|(((((from&lt;&lt;8)&gt;&gt;24)&gt;((to&lt;&lt;8)&gt;&gt;24))?(((from&lt;&lt;8)&gt;&gt;24)-
    (D3DCOLOR)(factor*(float)(((from&lt;&lt;8)&gt;&gt;24)-((to&lt;&lt;8)&gt;&gt;24)))):(((from&lt;&lt;8)&gt;&gt;24)+(D3DCOLOR)
    (factor*(float)(((to&lt;&lt;8)&gt;&gt;24)-((from&lt;&lt;8)&gt;&gt;24)))))&lt;&lt;16)|(((((from&lt;&lt;16)&gt;&gt;24)&gt;
    ((to&lt;&lt;16)&gt;&gt;24))?(((from&lt;&lt;16)&gt;&gt;24)-(D3DCOLOR)(factor*(float)(((from&lt;&lt;16)&gt;&gt;24)-
    ((to&lt;&lt;16)&gt;&gt;24)))):(((from&lt;&lt;16)&gt;&gt;24)+(D3DCOLOR)(factor*(float)(((to&lt;&lt;16)&gt;&gt;24)-
    ((from&lt;&lt;16)&gt;&gt;24)))))&lt;&lt;8)|(((((from&lt;&lt;24)&gt;&gt;24)&gt;((to&lt;&lt;24)&gt;&gt;24))?(((from&lt;&lt;24)&gt;&gt;24)-(D3DCOLOR)
    (factor*(float)(((from&lt;&lt;24)&gt;&gt;24)-((to&lt;&lt;24)&gt;&gt;24)))):(((from&lt;&lt;24)&gt;&gt;24)+(D3DCOLOR)(factor*
    (float)(((to&lt;&lt;24)&gt;&gt;24)-((from&lt;&lt;24)&gt;&gt;24))))))));

}


D3DCOLOR is just a DWORD (unsigned long). A color can for example be 0xAARRGGBB (A-alpha, R-red, G-green, B-blue), but works with other compositions aswell.

Obviously it's a total mess.

I actually don't know how it works and don't remember where I have it from, so I would ask for a review to improve the code and make it readable and understandable.
This is the reason why some people want ternary operators removed from the language. It's not quite the worst abuse I've seen, but it's pretty close. This took quite a while to disentangle. I'm going to go through it in steps:

First step: make it slightly more readable. Break up everything:

return (factor&lt;0.0f)
?
from 
: 
((factor&gt;1.0f)
?
to
:
(((((from &gt;&gt; 24) &gt; (to &gt;&gt; 24))
?
((from&gt;&gt;24)-(D3DCOLOR)(factor*(float)((from&gt;&gt;24)-(to&gt;&gt;24))))
:
((from&gt;&gt;24)+(D3DCOLOR)(factor*(float)((to&gt;&gt;24)-(from&gt;&gt;24)))))&lt;&lt;24)
|
(((((from&lt;&lt;8)&gt;&gt;24)&gt;((to&lt;&lt;8)&gt;&gt;24))
?
(((from&lt;&lt;8)&gt;&gt;24)-(D3DCOLOR)(factor*(float)(((from&lt;&lt;8)&gt;&gt;24)-((to&lt;&lt;8)&gt;&gt;24))))
:
(((from&lt;&lt;8)&gt;&gt;24)+(D3DCOLOR)(factor*(float)(((to&lt;&lt;8)&gt;&gt;24)-((from&lt;&lt;8)&gt;&gt;24)))))&lt;&lt;16)
|
(((((from&lt;&lt;16)&gt;&gt;24)&gt;((to&lt;&lt;16)&gt;&gt;24))
?
(((from&lt;&lt;16)&gt;&gt;24)-(D3DCOLOR)(factor*(float)(((from&lt;&lt;16)&gt;&gt;24)-((to&lt;&lt;16)&gt;&gt;24))))
:
(((from&lt;&lt;16)&gt;&gt;24)+(D3DCOLOR)(factor*(float)(((to&lt;&lt;16)&gt;&gt;24)-((from&lt;&lt;16)&gt;&gt;24)))))&lt;&lt;8)
|
(((((from&lt;&lt;24)&gt;&gt;24)&gt;((to&lt;&lt;24)&gt;&gt;24))
?
(((from&lt;&lt;24)&gt;&gt;24)-(D3DCOLOR)(factor*(float)(((from&lt;&lt;24)&gt;&gt;24)-((to&lt;&lt;24)&gt;&gt;24))))
:
(((from&lt;&lt;24)&gt;&gt;24)+(D3DCOLOR)(factor*(float)(((to&lt;&lt;24)&gt;&gt;24)-((from&lt;&lt;24)&gt;&gt;24))))))));


Not a hell of a lot of improvement, but we can at least start to see some of the shifting operations here and (sort) of what they're being applied to.

Second step: make some utility functions - 

typedef unsigned long D3DCOLOR;

D3DCOLOR Alpha(D3DCOLOR color)
{
    return (color &gt;&gt; 24);
}

D3DCOLOR Red(D3DCOLOR color)
{
    return (color &lt;&lt; 8) &gt;&gt; 24;
}

D3DCOLOR Green(D3DCOLOR color)
{
    return (color &lt;&lt; 16) &gt;&gt; 24;
};

D3DCOLOR Blue(D3DCOLOR color)
{
    return (color &lt;&lt; 24) &gt;&gt; 24;
}


Effectively, what's being done here is masking out each part (A, R, G, B) one at a time. I'm assuming it's in this format: it doesn't really matter if it isn't, because it's doing the same thing to each byte in the long. The only thing that will change is the order, but we'll stick with these function names for now. Let's add this into the mix:

return (factor&lt;0.0f)
?
from 
: 
((factor&gt;1.0f)
?
to
:
(Alpha(from) &gt; Alpha(to))
?
(Alpha(from) - (D3DCOLOR)(factor * (float)(Alpha(from) - Alpha(to)))
:
((Alpha(from) + (D3DCOLOR)(factor * (float)((Alpha(to)- Alpha(from))))))) &lt;&lt; 24
|
(Red(from) &gt; Red(to))
?
((Red(from) - (D3DCOLOR)(factor * (float)(Red(from) - Red(to))))
:
(Red(from) + (D3DCOLOR)(factor * (float)((Red(to) - Red(from)))))) &lt;&lt; 16
|
(Green(from) &gt; Green(to))
?
((Green(from) - (D3DCOLOR)(factor*(float)(Green(from) - Green(to)))
:
(Green(from) + (D3DCOLOR)(factor * (float)((Green(to) - Green(from)))))) &lt;&lt; 8
|
(Blue(from) &gt; Blue(to))
?
((Blue(from) - (D3DCOLOR)(factor * (float)(Blue(from) - Blue(to)))
:
(Blue(from) + (D3DCOLOR)(factor * (float)((Blue(to) - (Blue(from)))))) &gt;&gt; 24;


Ok, getting there. It's almost readable now. Note the parens here are something of a pain, and we must be careful to get them correct.

We can now do the (laborious) task of replacing ternary operators with if/else:

D3DCOLOR GetFadedColor(D3DCOLOR from, D3DCOLOR to, float factor)
{
    D3DCOLOR col = 0;

    if (factor &lt; 0.0f) {
        col = from; 
    }
    else if (factor &gt; 1.0f) {
        col = to;
    }
    else {
        if(Alpha(from) &gt; Alpha(to)) {
            col = (Alpha(from) - (D3DCOLOR)(factor * (float)((Alpha(from) - Alpha(to))))) &lt;&lt; 24;
        } 
        else {
            col = (Alpha(from) + (D3DCOLOR)(factor * (float)((Alpha(to)- Alpha(from))))) &lt;&lt; 24;
        }

        if(Red(from) &gt; Red(to)) {
            col |= (Red(from) - (D3DCOLOR)(factor * (float)((Red(from) - Red(to))))) &lt;&lt; 16;
        }
        else {
            col |= (Red(from) + (D3DCOLOR)(factor * (float)((Red(to) - Red(from))))) &lt;&lt; 16;
        }

        if(Green(from) &gt; Green(to)) {
            col |= (Green(from) - (D3DCOLOR)(factor * (float)((Green(from) - Green(to))))) &lt;&lt; 8;
        } 
        else {
            col |= (Green(from) + (D3DCOLOR)(factor * (float)((Green(to) - Green(from))))) &lt;&lt; 8;
        }

        if(Blue(from) &gt; Blue(to)) {
            col |= (Blue(from) - (D3DCOLOR)(factor * (float)((Blue(from) - Blue(to)))));
        }
        else {
            col |= (Blue(from) + (D3DCOLOR)(factor * (float)((Blue(to) - (Blue(from))))));
        }
    }

    return col;
}


Ok, so now it's readable. This can likely be simplified further - there's quite a bit of duplication here. All that really changes are some shift values. Hopefully this is enough that you can do that simplification yourself, however.