Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 485
  • Last Modified:

Color transformation algorithm?

Hello Experts,

I am looking for an algorithm which would have to generate specified number of color variances between the two given colors.

For eg., If I say RGB(200,240,233) and RGB(10,40,255) are the colors, and 10 as variances then I would expect the algorithm to produce 10 color codes to indicate the color transformation from RGB(200,240,233) to RGB(10,40,255).

I am not sure if .NET has built-in function for this, if so please point it out.

Thank you!
0
redflair
Asked:
redflair
1 Solution
 
redflairAuthor Commented:
I found a VB code which does color degradation of any given two colors http://www.developerfusion.com/code/2150/creating-color-degradations-with-any-two-colors/. Converted to C# and it works pretty good.
        public static List<Color> CalculateGradient(Color color1, Color color2, int steps)
        {
            List<Color> colors = new List<Color>(steps);
 
            for (int i=0; i<steps; i++)
            {
                Int64 tRed = color1.R + Math.Sign(Convert.ToInt64(color2.R) - Convert.ToInt64(color1.R)) * Convert.ToByte(Math.Abs(Convert.ToInt64(color2.R) - Convert.ToInt64(color1.R)) / (steps) * i);
                Int64 tGreen = color1.G + Math.Sign(Convert.ToInt64(color2.G) - Convert.ToInt64(color1.G)) * Convert.ToByte(Math.Abs(Convert.ToInt64(color2.G) - Convert.ToInt64(color1.G)) / (steps) * i);
                Int64 tBlue = color1.B + Math.Sign(Convert.ToInt64(color2.B) - Convert.ToInt64(color1.B)) * Convert.ToByte(Math.Abs(Convert.ToInt64(color2.B) - Convert.ToInt64(color1.B)) / (steps) * i);                
 
                colors.Add(Color.FromRgb(Convert.ToByte(tRed), Convert.ToByte(tGreen), Convert.ToByte(tBlue)));
            }
 
            return colors;
        }

Open in new window

0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now