Copy link to clipboard
Copied
Hi Everyone!,
Is there is any mathematical function/formula to convert RGB/Hex to gray color conversion?
for example,
if the we have red color FF0000 [ 255,0,0] and convert into gray 7f7f7f [127,127,127]
[ 255,0,0] this value changed to [127,127,127]
Anyone colud explain this conversion?
Copy link to clipboard
Copied
Hi
I don't understand any of this but have a look here 😁
https://www.rapidtables.com/convert/color/how-rgb-to-hex.html
Copy link to clipboard
Copied
Hi Ged,
Thank you for the prompt reply.
I need to know RGB to grey color conversion of formula/functionalities
Copy link to clipboard
Copied
Those numbers depend on what the RGB color space is, and what the grayscale color space is. Yes, grayscale is subject to standard color management just like RGB. It's just one channel instead of three. RGB color spaces are sRGB, Adobe RGB and so on; grayscale color spaces are Dot Gain 15%, Gray gamma 2.2, sGray etc.
Numbers are color space specific. This is important to understand. Any given color will yield different numbers in different color spaces. Any given set of numbers will yield different colors in different color spaces. This applies to grayscale as much as RGB (or CMYK).
Hex is just a convenient way to express RGB numbers. It has no significance beyond convenience.
If you just use Image > Mode, the grayscale profile is determined by whatever you have as working gray. It's better to convert directly to the grayscale profile you want, with Edit > Convert to Profile. You should be aware that the default gray in Photoshop, Dot Gain 20%, is virtually useless for any practical purpose. The problem is that outside Photoshop, grayscale color management support is virtually non-existent. So while everything looks fine in Photoshop, all conversions performed as they should - the moment you move that file outside Photoshop, you get massive tone and contrast changes.
In converting from RGB to grayscale, you get the least surprises if you pick a grayscale profile with the same tone response curve as the RGB profile. So you'd use sRGB > sGray, Adobe RGB > Gray Gamma 2.2, and ProPhoto > Gray Gamma 1.8. If you do that, the numbers should stay reasonably consistent.
There is still the problem of what to do with the grayscale file outside Photoshop, because almost no software on the planet will treat it correctly. The grayscale profile will largely be ignored, even in applications that otherwise are fully color managed. The safest one all over is probably Gamma 2.2.
The simple answer, which is the one I usually give, is: avoid grayscale if you can. It's a minefield, and the likelihood of getting a nasty surprise is very high. For screen use, a monochrome sRGB file is always safe(r).
For offset CMYK print, grayscale traditionally and by convention prints on the black plate only (saving money). So it has a purpose there, and there are ways to control the result by converting to <black ink> CMYK. But that's a different and longer story.
Copy link to clipboard
Copied
If using the Image > Mode menu options, then Edit > Colour Settings controls the conversion.
* Source RGB profile, either assigned or assumed
* Destination Greyscale profile
* Rendering intent, engine, BPC etc.
If using Edit > Conver to Profile then the same rules apply, it is all about the selected settings.
Numbers such as 255r0g0b and 127r127g127b are just values, until an ICC profile is paired with them the exact colour and tone is variable.
Copy link to clipboard
Copied
Hi Everyone!,
Is there is any mathematical function/formula to convert RGB/Hex to gray color conversion?
for example,
if the we have red color FF0000 [ 255,0,0] and convert into gray 7f7f7f [127,127,127]
[ 255,0,0] this value changed to [127,127,127]
Anyone colud explain this conversion?
By @yajiviki
And how do you get these "gray" values?
It looks like you are using a hue\saturation adjustment with saturation = 0.
Or you are using the desaturate command.
If so, it looks like this conversion uses the HSL model.
That is, when the saturation is zero, the colors should have the same lightness L.
The formula for L (in HSL) from RGB is as follows
M = max(R,G,B)
m = min(R,G,B)
Lrgb = (M+m)/2/255 = (M+m)/510
Since, in the gray picture R=G=B, let's call this value K.
So Lgray = 2*K/510
From here we get
2*K/510 = (M+m)/510
K = (M+m)/2
In our case, M = 255, m = 0
K=(255+0)/2=127.5
P.S.
Copy link to clipboard
Copied
But this is still relative to color space, including gray space. For these conversions, the working space is used, except in the open file where the embedded profile is used.
All numerical values are always specific to color space. Gray Gamma 2.2 gives different numbers than Dot Gain 20% and so on.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
This is the usual thing - a discussion from several years ago, bought to life by a spammer whose post will be deleted, leaving nothing but the mystery of a revived discussion... Normally it catches me too...
Copy link to clipboard
Copied
To be precise, I meant calculations, not conversions. The calculations between color modes are based on the working spaces, including the working gray. Different working grays will give different results.
Copy link to clipboard
Copied
For anyone still reading this thread and wanting more clarification, keep in mind that Photoshop can convert RGB to gray in many different ways that produce different results. In short, there is no single formula; you get to pick the formula. You can:
Apply the Image > Mode > Grayscale command, which converts using the current gray working space profile and working space settings as discussed already. If you want to customize that, choose Edit > Convert to Profile and pick the gray profile and settings you want.
Apply the Image > Adjustments > Desaturate command, or apply a Hue/Saturation adjustment and setting Saturation to zero. Both should produce the same result.
Apply the Image > Adjustments > Black and White adjustment, and then optionally choose a preset or changing the settings.
Take the image luminance (Command/Ctrl-click the composite channel in the Channels panel) and save the selection as a new document. Or just take any channel.
Convert the image to another color space and take the L channel (from Lab) or the K channel (from CMYK).
Someone can probably find more ways.
Copy link to clipboard
Copied
Yes, you can convert an RGB color to grayscale using a mathematical formula. One common method is to calculate the luminance (perceived brightness) of the color and use that value as the grayscale equivalent. The formula to calculate the luminance of an RGB color is:
Luminance (Y) = 0.299 * R + 0.587 * G + 0.114 * B
Where:
This formula takes into account the human eye's sensitivity to different colors and is often used in color-to-grayscale conversions. The resulting luminance value is usually in the range of 0 to 255, which is suitable for grayscale values.
Let's apply this formula to the red color FF0000 [255, 0, 0]:
Luminance (Y) = 0.299 * 255 + 0.587 * 0 + 0.114 * 0 Luminance (Y) = 76.455
When rounding to the nearest whole number, you get 76.
So, the grayscale equivalent of the color FF0000 is 76, which corresponds to the gray value 4C in hexadecimal. In your example, 4C (or 7F7F7F) represents a shade of gray, and it's derived from the red color using the formula explained above.
https://www.cmtoinchesconvert.com/convert/color/how-rgb-to-hex.html
Copy link to clipboard
Copied
Yes, that is the formula for the "Luminance" value in an RGB file, as used in e.g. Luminance blend mode or luminance masks.
You can not use this in a conversion into grayscale! Then the numbers are dependent on what RGB color space is used, and what grayscale color space is used. That conversion goes from the RGB color space to the Profile Connection Space (Lab/XYZ) and then to the destination grayscale color space.
To be clear - the above formula is just an internal "ad hoc" formula used in Photoshop to give a roughly equal visual tonal appearance. It is not a conversion that conforms to any kind of standard! It doesn't even conform to Lab L.
I'll just quote two paragraphs from my first reply to the original post above (scroll up), which explains in detail:
Numbers are color space specific. This is important to understand. Any given color will yield different numbers in different color spaces. Any given set of numbers will yield different colors in different color spaces. This applies to grayscale as much as RGB (or CMYK).
In converting from RGB to grayscale, you get the least surprises if you pick a grayscale profile with the same tone response curve as the RGB profile. So you'd use sRGB > sGray, Adobe RGB > Gray Gamma 2.2, and ProPhoto > Gray Gamma 1.8. If you do that, the numbers should stay "reasonably" consistent.
Copy link to clipboard
Copied
That is one of those posts that sort of reads like it was written by a chatbot. I wrote about the multiple ways that Photoshop converts color to gray, but the reply by Maneeshkushwaha is a general post that does not seem to relate back to Photoshop specifically.
Copy link to clipboard
Copied
Right. Reading it again that seems likely.
Copy link to clipboard
Copied
I hope so,
Find more inspiration, events, and resources on the new Adobe Community
Explore Now