Get color from grayscale
Post your questions regarding programming in C# in here.
10 posts
Page 1 of 1
I'm making this image editing library, and I thought of making an "anti grayscale" filter. The problem is, I just can't find the right formula to get the red, green and blue values from the gray value. Lets say I have the color R=128, G=191, B = 191. To get the gray value, I use the following function:
, I have failed in every attempt to calculate the red, green and blue components from the value that is returned by the grayscale function, in this case 172.
Code: Select all
Since this is basic math, there should be a somewhat easy way to reverse it. Though my math grades are good byte grayScale = (byte)((b * 0.114) +
(g * 0.587) +
(r * 0.299));

LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!
![Image]()
![Image]()
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!

You can't do it easily...
Basically grayscale is averaging the RGB values of each pixel.
say the original RGB values of the pixel were 126, 127, 128. This in grayscale is 127, 127, 127. There are also many other pixel colors that can average to 127, for example, 27, 127, 227. These two colors are completely different, even though they average to the same grayscale color.
The method you are using is similar to averaging the values together, so this applies o what you are doing.
So basically, this can't be (easily) done.
Basically grayscale is averaging the RGB values of each pixel.
say the original RGB values of the pixel were 126, 127, 128. This in grayscale is 127, 127, 127. There are also many other pixel colors that can average to 127, for example, 27, 127, 227. These two colors are completely different, even though they average to the same grayscale color.
The method you are using is similar to averaging the values together, so this applies o what you are doing.
So basically, this can't be (easily) done.
My method isn't averaging. Average would be (R + G + B) / 3. I have (R * 0.299 + G * 0.587 + B * 0.114) which makes the result the way the human eye would see it, so I thought I could use those constants to convert the grayscale value back to its original colors.
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!
![Image]()
![Image]()
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!

MrAksel wrote:My method isn't averaging. Average would be (R + G + B) / 3. I have (R * 0.299 + G * 0.587 + B * 0.114) which makes the result the way the human eye would see it, so I thought I could use those constants to convert the grayscale value back to its original colors.I know you are using a different grayscaling method, but the concept is the same.
Well, seeing as you are multiplying it by those values to get the grayscale, wouldn't it be logical that you divide your grayscale by those same values? It would cancel the math operation, and theoretically restore the color to it's original tone.
comathi wrote:Well, seeing as you are multiplying it by those values to get the grayscale, wouldn't it be logical that you divide your grayscale by those same values? It would cancel the math operation, and theoretically restore the color to it's original tone.The problem with that is that you don't know what to divide by.
Take a look at this: http://stackoverflow.com/questions/8357 ... esentation
We shall let the revolution begin.. the revolution for freedom, freedom against censorship. We shall fight in the fields and in the streets, we shall fight in the hills; we shall never surrender
Im trying to restore the original colors Codex, not making a Color from a grayscale value (Color.FromArgb(gray, gray, gray)).
The problem is where I add the three values together to make the gray. That is ruining my possibilities to restore the colors. You are right Cheatmasterbw :(
The problem is where I add the three values together to make the gray. That is ruining my possibilities to restore the colors. You are right Cheatmasterbw :(
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!
![Image]()
![Image]()
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!

If you have 2 of the 3 original RGB values, you can find out the third. You can probably make it so the user chooses 2 of the original values of each pixel, and then paint over part of the image. The user could adjust the 2 values until the part of the image looks right.
Edit:
like this
http://planetphotoshop.com/colorizing-a ... image.html
Edit:
like this
http://planetphotoshop.com/colorizing-a ... image.html
Last edited by Cheatmasterbw on Mon Apr 23, 2012 9:11 pm, edited 1 time in total.
That looks pretty good, thanks. Ill probably implement a "key color" that will fill the grayscale image as in the PS tutorial 

LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!
![Image]()
![Image]()
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!

10 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023