Click here to Skip to main content
15,176,980 members
Articles / Multimedia / GDI
Posted 20 Nov 2008


59 bookmarked

Image Transformation: Grayscale to Color

Rate me:
Please Sign up or sign in to vote.
4.19/5 (20 votes)
6 May 2009CPOL1 min read
Algorithm for easy transformation images


This is an easy algorithm of image transformation from grayscale image to color. It's far from ideal, but it is very simple.


Usually we calculate gray color as:

Gray  = Green * 0.59 + Blue * 0.30 + Red * 0.11;

Each color with structure [Gray, Gray, Gray] has a set of colors:

G = [Gray, Gray, Gray]<br />G -> P = {C}, for each C from P:  Green * 0.59 + Blue * 0.30 + Red * 0.11 = Gray.

First of all, we create a set of control points. Control point is equivalent of "gray" color and "full" color. When we have control points set, we can approximate any "gray" color to "full" color. If "gray" color is located between two control points C1 and C2, then the color approximates as:

K = (Gray - C1<sub>Gray</sub>)/(C2<sub>Gray </sub>- C1<sub>Gray</sub>)<br />Red = C1<sub>Red </sub>+ K*(C2<sub>Red </sub>- C1<sub>Red</sub>)<br />Green = C1<sub>Green </sub>+ K*(C2<sub>Green </sub>- C1<sub>Green</sub>)<br />Blue = C1<sub>Blue </sub>+ K*(C2.<sub>Blue </sub>- C1<sub>Blue</sub>)

Now we has "full color" equivalent for each "gray" color.


Figure 1. Transformation schema.

Using the Code  

ControlPoint is a structure for storing data about control points.

public struct ControlPoint
    private int level;
     public int Level
        get { return level; }
        set { level = value; }
    private Color color;
     public Color Color
        get { return color; }
        set { color = value; }
     public ControlPoint(int level, Color color)
        this.color = color;
        this.level = level;
     public override string ToString()
        return "Level: " + level.ToString() + "; Color: " + color.ToString();

PointsComparer is a class for comparing two control points:

public class PointsComparer: IComparer<ControlPoint>
    #region IComparer<ControlPoint> Members
     public int Compare(ControlPoint x, ControlPoint y)
        if (x.Level > y.Level)
            return 1;
        if (x.Level < y.Level)
            return -1;
        return 0;

ColorBuilder is a class for building a color diagram. It returns an array of 256 colors - our diagram.

public static Color[] GetColorDiagram(List<ControlPoint> points)
    Color[] colors = new Color[256];
    points.Sort(new PointsComparer());
     for (int i = 0; i < 256; i++)
        ControlPoint leftColor = new ControlPoint
				(0, GetNearestLeftColor(points[0].Color)); 
        ControlPoint rightColor = new ControlPoint
			(255, GetNearestRigthColor(points[points.Count - 1].Color));
        if (i < points[0].Level)
            rightColor = points[0];
        if (i > points[points.Count - 1].Level)
            leftColor = points[points.Count - 1];
            for (int j = 0; j < points.Count - 1; j++)
                if ((points[j].Level <= i) & (points[j + 1].Level > i))
                    leftColor = points[j];
                    rightColor = points[j + 1];
        if ((rightColor.Level - leftColor.Level) != 0)
            double koef = (double)(i - leftColor.Level) / 
			(double)(rightColor.Level - leftColor.Level);
            int r = leftColor.Color.R + (int)(koef * 
			(rightColor.Color.R - leftColor.Color.R));
            int g = leftColor.Color.G + (int)(koef * 
			(rightColor.Color.G - leftColor.Color.G));
            int b = leftColor.Color.B + (int)(koef * 
			(rightColor.Color.B - leftColor.Color.B));
            colors[i] = Color.FromArgb(r, g, b);
            colors[i] = leftColor.Color;
     return colors;

Now we can get the color image:

colorBitmap = new Bitmap(sourceBitmap);
for (int i = 0; i < sourceBitmap.Width; i++)
    for (int j = 0; j < sourceBitmap.Height; j++)
        int level = sourceBitmap.GetPixel(i, j).B;
        colorBitmap.SetPixel(i, j, colors[level]);
pbxColorImage.Image = colorBitmap;


Figure 2. Simple color dialog.


Figure 3. Grayscale image, color image and color diagram.

Points of Interest  

The algorithms is very easy, and has many defects. But it is a good example for beginners in image processing. 

Update Information

  1. New formula for grayscale level calculation
  2. Bug with empty list was removed


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
Russian Federation Russian Federation
Hello! My name is Maxim Subbotin.

Now I work in sphere of web-development. I'm interesting researches in SEO field.
If you interesting, you can see this tool:


Comments and Discussions

Questiongrayscale to color image Pin
Member 1419464424-Aug-20 2:35
MemberMember 1419464424-Aug-20 2:35 
Questionabout algorithm Pin
Norhaat18-Dec-11 14:44
MemberNorhaat18-Dec-11 14:44 
Generalgrayscale to color on HSI model Pin
thanh_rio27-Jun-09 1:28
Memberthanh_rio27-Jun-09 1:28 
GeneralRe: grayscale to color on HSI model Pin
Maxim_Barsuk28-Jun-09 20:16
MemberMaxim_Barsuk28-Jun-09 20:16 
QuestionCouldnt you make it better??? Pin
gonzospy1-May-09 7:26
Membergonzospy1-May-09 7:26 
AnswerRe: Couldnt you make it better??? Pin
Maxim_Barsuk3-May-09 20:45
MemberMaxim_Barsuk3-May-09 20:45 
GeneralRe: Couldnt you make it better??? Pin
gonzospy5-May-09 19:25
Membergonzospy5-May-09 19:25 
GeneralRe: Couldnt you make it better??? [modified] Pin
Maxim_Barsuk6-May-09 20:08
MemberMaxim_Barsuk6-May-09 20:08 
GeneralКруто Pin
Laserson26-Jan-09 21:44
MemberLaserson26-Jan-09 21:44 
GeneralRe: Круто Pin
Maxim_Barsuk27-Jan-09 5:53
MemberMaxim_Barsuk27-Jan-09 5:53 
GeneralSomething to go off of.... Pin
Sike Mullivan26-Jan-09 5:34
MemberSike Mullivan26-Jan-09 5:34 
In document imaging it is important for companies to store documents that are a small size. Now we all know that color images are huge and shouldn't be stored in an imaging system, especially SharePoint. The huge files could clog up the database back end. What you could do is save these color documents as tiff. Then covert them to gray-scale while storing the Control Points in a Tag on the tiff file. You would have to use tiff because no other documents support tagging. Then when a user views the doc, you could build an app that takes the control point and coverts them back to color for their viewing pleasure. It may not be practical but I think it would make a good article.

I think you've started something cool here but your article lacks presentation quality. I know my articles are not perfect so I rated this a four. It seems that the worst of critics are people who can't write articles themselves. Good luck in the future. Keep that english flowing. Smile | :)
GeneralRe: Something to go off of.... Pin
Maxim_Barsuk26-Jan-09 7:10
MemberMaxim_Barsuk26-Jan-09 7:10 
GeneralRe: Something to go off of.... Pin
MicroImaging3-Jun-09 8:13
MemberMicroImaging3-Jun-09 8:13 
GeneralMy vote of 2 Pin
MadZookeeper26-Jan-09 3:37
MemberMadZookeeper26-Jan-09 3:37 
AnswerRe: My vote of 2 Pin
Afzaal Ahmad Zeeshan3-Sep-15 4:55
mveAfzaal Ahmad Zeeshan3-Sep-15 4:55 
Questionwhat about color to grayscale? Pin
bluish24-Jan-09 9:26
Memberbluish24-Jan-09 9:26 
AnswerRe: what about color to grayscale? Pin
Maxim_Barsuk25-Jan-09 20:24
MemberMaxim_Barsuk25-Jan-09 20:24 
GeneralLuminace from RGB Pin
terry mohre25-Nov-08 4:22
Memberterry mohre25-Nov-08 4:22 
GeneralRe: Luminace from RGB Pin
Maxim_Barsuk25-Nov-08 6:55
MemberMaxim_Barsuk25-Nov-08 6:55 
GeneralMy vote of 1 Pin
Christian Wikander25-Nov-08 2:59
MemberChristian Wikander25-Nov-08 2:59 
QuestionWhy? Pin
Christian Wikander25-Nov-08 2:58
MemberChristian Wikander25-Nov-08 2:58 
GeneralBugs Pin
Alexander Yakovlev21-Nov-08 2:30
MemberAlexander Yakovlev21-Nov-08 2:30 
GeneralRe: Bugs PinPopular
Maxim_Barsuk21-Nov-08 6:39
MemberMaxim_Barsuk21-Nov-08 6:39 
NewsRe: Bugs Pin
thanh_rio4-Jun-09 7:55
Memberthanh_rio4-Jun-09 7:55 
GeneralRe: Bugs Pin
Maxim_Barsuk4-Jun-09 20:08
MemberMaxim_Barsuk4-Jun-09 20:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.