# Calculate an equally bright grey to a linear RGB colour

I'm trying to get a grey linear-RGB-triple that is equally bright as another (colorful) RGB-triple.

I have to admit, that there is great confusion for me with all the color stuff, even though I read a lot of wikipedia articles, but I don't really get the big picture. I'm kind of lost and need help.

Wikipedia seems to mention a formula to receive the perceived brightness of a linear RGB color.
So I can measure the brightness of my color and do something like this I suppose:

public Vector3d get_equally_bright_grey(Vector3d color) {
for (double c = 0.0; c <= 1.0; c += 0.001) {
Vector3d grey = new Vector3d(c, c, c);
if (brightness(color) == brightness(grey)){
return grey;
}
}
}


But there has to be something other :)

Some context:

My input colors were spectral distributions that I converted to XYZ values (after Bruce Lindbloom). Then I have converted those XYZ values to linear RGB (again with Bruce). And here comes my question.

I know that the Y component of my XYZ triples is also something like the luminance, but I don't know how I generate a grey color XYZ color from this either..

The easiest way to get the perceived brightness of a color is to calculate the Luma. Finding the grey color with the same luma is easy - just set all of the RGB components to the desired luma value. That works because the coefficients for calculating the luma sum up to 1.

public Vector3d get_equally_bright_grey(Vector3d color) {
double luma = color.x * 0.299 + color.y * 0.587 + color.z * 0.114;
return new Vector3d(luma, luma, luma);
}

• Thanks for your response! Two questions: 1) The wiki article uses R' G' B' in the equation which corespond to "gamma-compressed" values. So wouldn't give me this wrong numbers because I'm using linear RGB? 2) Could you explain why you've chosen this formula from the article? I'm confused why this does apply to my case and not the one with 0.2126 ... – PeteParly Nov 1 '16 at 20:58
• @PeteParly It's a matter of choice. I tend to use this formula for calculating the brightness of colors in linear space, because it gives subjectively more pleasing results than the rather aggressive Rec. 709 formula, at least on my monitor. As for gamma-compressed colors, the wiki article mentions the same formula is arbitrarily used for both gamma and linear color spaces, despite it giving different results. – Quinchilion Nov 1 '16 at 21:41
• @PeteParly The best thing you can do is to try out multiple formulas and choose the one that works for you. I'm not aware of any particular standard, besides the Lab color space, which tries to more closely approximate human vision: en.wikipedia.org/wiki/Lab_color_space Using it is a bit more involved, though. To get just the gray color, you'd have to convert your RGB color to Lab, then set the a and b components to 0, effectively removing chrominance, and then convert it back to the RGB space of your choice. – Quinchilion Nov 1 '16 at 21:44
• I think this "The best thing you can do is to try out multiple formulas and choose the one that works for you." answers a bit of my confusion in my head :) I'll try that and accept your answer – PeteParly Nov 1 '16 at 21:48
• @PeteParly you can do better if you have a colorimeter, than you can make a icc profile and be more exact for your monitor – joojaa Nov 2 '16 at 17:02

As @Quinchilion mentioned, the calculation is done using three weights applied to the color components:

luminance = color.r * r_weight + color.g * g_weight + color.b * b_weight;


There are many coefficients that can be found on the internet, there are two sample in an old OpenGL note: