• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Calculate Hue from RGB?

Engaged ,
Dec 18, 2021 Dec 18, 2021

Copy link to clipboard

Copied

Hi there --

 

Do any of you know the formulas to calculate hue from RGB values? I know I can plug the RGB values into the color picker, which will show the HSL values. That's a tedious process, and I want to do it in an Excel spreadsheet. I have a grid with blocks of solid colors in Photoshop, and I want to arrange the blocks in hue order (0-360). There are different formulas based on red, green, and blue light ratios:

R > G > B

R > B > G

B > R > G

B > G > R

G > R > B

G > B > R

 

Scott

 

Scott

TOPICS
Actions and scripting , Windows

Views

6.2K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Dec 19, 2021 Dec 19, 2021
quote

I'm not in Photoshop. I have a list of RGB values in an Excel spreadsheet.


By @scotwllm

 

 

So this sounds like it is more of a MS Excel question than a Photoshop one at this point...

 

https://www.mrexcel.com/board/threads/rgb-to-hue-formula.559852/

 

=IF(180/PI()*ATAN2(2*A2-B2-C2,SQRT(3)*(B2-C2))<0,180/PI()*ATAN2(2*A2-B2-C2,SQRT(3)*(B2-C2))+360,180/PI()*ATAN2(2*A2-B2-C2,SQRT(3)*(B2-C2)))

 

formula.png

 

To round the result of the formula down to 161 to match Photoshop sRGB, apply the following formula

...

Votes

Translate

Translate
Adobe
Community Expert ,
Dec 19, 2021 Dec 19, 2021

Copy link to clipboard

Copied

quote

There's only one conversion on there.

 

There are multiple ways to copy the formula so that it is applied to other cells based on other rows/columns etc. Only one conversion was needed to test that the formula works, I replaced the content multiple times when testing rather than adding new rows.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 19, 2021 Dec 19, 2021

Copy link to clipboard

Copied

What I was getting at is that the calculation has to work in six conditions:

 

R > G > B

R > B > G

G > R > B

G > B > R

B > R > G

B > G > R

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 20, 2021 Dec 20, 2021

Copy link to clipboard

Copied

Hi @scotwllm , this Javascript will loop throught the RGB channels and get the hue for each RGB value. Incrementing by 10 outputs 17,576 lines—change the n value to output at different increments. Writes a CSV file to your desktop:

 

 

 

 

//the channel increment number. 10 writes 17,576 color lines
var n = 10;

var ht = Folder.desktop + "/RGB_HUE.csv";
var sc = [255,255,255];
var h,fc;
var s = "RGB\tHue\r";


for (var i = 255; i >= 0; i -= n) {
    sc[0]=i;
    for (var j = 255; j >= 0; j -= n) {
        sc[1]=j;
        for (var k = 255; k >= 0; k -= n) {
            sc[2]=k;
            fc = makeRGB(sc);
            h = fc.hsb.hue; 
            s +=  sc.toString() + "\t" + Math.round(h) + "\r"   
        }
    } 
}

//save the spreadsheet to desktop
writeText(ht, s)


/**
* Makes and returns an RGB color 
* @Param a an array of RGB values
* @Returns a SolidColor 
* 
*/	

function makeRGB(a){
    colorobj = new SolidColor()
    colorobj.rgb.red = a[0];
    colorobj.rgb.green = a[1];
    colorobj.rgb.blue = a[2]; 
    return colorobj;
};


/**
* Write a text file 
* @param the file path 
* @param the text 
**/
function writeText(p,s){
    var file = new File(p.toString());
    file.encoding = 'UTF-8';
    file.open('w');
    file.write(s);
    file.close();
}

 

 

Screen Shot 13.png

 

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 20, 2021 Dec 20, 2021

Copy link to clipboard

Copied

You could also read in a 3 column spreadsheet of RGB values and get the H values. Something like this:

 

 

 

 

var tf = File.openDialog("Select a CSV file to read");
tf.open("r");
var txt = tf.read();
var hxList = txt.split("\n");

var fc,h; 
var a=[];


var ht = Folder.desktop + "/HUES.csv";
var s = "R\tG\tB\tH\r";
for (var i = 0; i < hxList.length; i++){
    try {
        a = hxList[i].split("\t")
        fc = makeRGB(a);
        h= fc.hsb.hue; 
        
        s +=  a[0] + "\t" + a[1]+ "\t" + a[2] + "\t" + Math.round(h) + "\r" 
    }catch(e) {}  
};   


writeText(ht, s)

/**
* Makes and returns an RGB color 
*  a an array of RGB values
*  a SolidColor 
* 
*/	

function makeRGB(a){
    colorobj = new SolidColor()
    colorobj.rgb.red = a[0];
    colorobj.rgb.green = a[1];
    colorobj.rgb.blue = a[2]; 
    return colorobj;
};


/**
* Write a text file 
*  the file path 
*  the text 
**/
function writeText(p,s){
    var file = new File(p.toString());
    file.encoding = 'UTF-8';
    file.open('w');
    file.write(s);
    file.close();
}

 

 

 

 

Outputs this from the attached spreadsheet:

 

Screen Shot 21.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 21, 2021 Dec 21, 2021

Copy link to clipboard

Copied

I appreciate the effort you put into this. However, we found more elegant solution that doesn't do any unnecessary calculating and doesn't require me to search through an endless  list of numbers to find an answer. The solution is to include the following formula on the same row as an RGB triplet. For example, if R is in column C, G is in column D, and B is in column E, input this formula in column F:

 

=IF(180/PI()*ATAN2(2*C2-D2-E2,SQRT(3)*(D2-E2))<0,180/PI()*ATAN2(2*C2-D2-E2,SQRT(3)*(D2-E2))+360,180/PI()*ATAN2(2*C2-D2-E2,SQRT(3)*(D2-E2)))

 

The colors in the leftmost cell are automatically generated with this script:

 

Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range, cell As Range
Dim i As Long

Set rng = Intersect(Target, Range("c:e"))
If Not rng Is Nothing Then
On Error Resume Next
For Each cell In Target.Columns(1).Cells
If Application.CountA(Range("c" & cell.Row & ":e" & cell.Row)) < 3 Or _
Application.Count(Range("c" & cell.Row & ":e" & cell.Row)) < 3 Then GoTo next_row
Cells(cell.Row, "b").Interior.Color = _
RGB(Cells(cell.Row, "c").Value, Cells(cell.Row, "d").Value, Cells(cell.Row, "e").Value)
next_row:
Next cell
End If
End Sub

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 21, 2021 Dec 21, 2021

Copy link to clipboard

Copied

Hi @scotwllm I doubt it matters much, but the spreadsheet cell script is returning a value that’s 1 degree higher than Photoshop’s color picker.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 21, 2021 Dec 21, 2021

Copy link to clipboard

Copied

Hi Rob, I covered this on page 1:

 

=ROUNDDOWN(D2)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 21, 2021 Dec 21, 2021

Copy link to clipboard

Copied

Thanks for checking my work, Rob. As Stephen mentioned, it's just a rounding error. What I was after was the order in which the color swatches would appear in a grid. If you look at the first two rows, it may appear that it's just a hodge podge of different polo shirt colors. They're not arranged randomly, though. Each shirt his a higher hue value than the one that came before it in the grid. 

 

That's me. You might be thinking I should crank up the contrast. This particular set of colors are supposed to work well on low contrast people like me. My skin and hair are close to the same color. If you get close enough, you can see I have eyebrows and eyelashes.  I'm making six of these templates of 45 colors each -- light colors, dark colors, clear colors, muted colors, warm colors and cool colors. I just swap in someone's head, match the face and skin colors, copy it all into the cells of the grid and evaluate which of the sets of colors looks the best. 

 

Scott

 

Scott

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 19, 2021 Dec 19, 2021

Copy link to clipboard

Copied

In general I say BEWARE of colour conversion information on the internet. Much of it is written by people who know nothing about colour. However, this is one of the safe ones. RGB is (as you may know) not an exact reference of colour: two people may see the same RGB colour as wildly different. However, HSL is a simple conversion of RGB. This means of course that HSL is also not an exact reference of colour and may look wildly different to different people.

 

Ok, warning aside, this page has both a converter and the actual formulae it uses:

RGB to HSL converter | color conversion (rapidtables.com) 

Don't necessarily trust the other converters on this site though, it has the common nonsense of a "CMYK to RGB converter" as if this even exists.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 19, 2021 Dec 19, 2021

Copy link to clipboard

Copied

I understand about output looking different on different screens or when printed. I doubt the people I share these files with will have a monitor as good as mine. All I am trying to do is to sort the colors. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines