Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7502247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:47:52+00:00 2026-05-29T20:47:52+00:00

Is there a way to efficiently change hue of a 2D OpenGL texture using

  • 0

Is there a way to efficiently change hue of a 2D OpenGL texture using GLSL (fragment shader)?

Do someone have some code for it?

UPDATE: This is the code resulting from user1118321 suggestion:

uniform sampler2DRect texture;
const mat3 rgb2yiq = mat3(0.299, 0.587, 0.114, 0.595716, -0.274453, -0.321263, 0.211456, -0.522591, 0.311135);
const mat3 yiq2rgb = mat3(1.0, 0.9563, 0.6210, 1.0, -0.2721, -0.6474, 1.0, -1.1070, 1.7046);
uniform float hue;

void main() {

vec3 yColor = rgb2yiq * texture2DRect(texture, gl_TexCoord[0].st).rgb; 

float originalHue = atan(yColor.b, yColor.g);
float finalHue = originalHue + hue;

float chroma = sqrt(yColor.b*yColor.b+yColor.g*yColor.g);

vec3 yFinalColor = vec3(yColor.r, chroma * cos(finalHue), chroma * sin(finalHue));
gl_FragColor    = vec4(yiq2rgb*yFinalColor, 1.0);
}

And this is the result compared with a reference:

enter image description here

I have tried to switch I with Q inside atan but the result is wrong even around 0°

Have you got any hint?

If needed for comparison, this is the original unmodified image:
enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T20:47:55+00:00Added an answer on May 29, 2026 at 8:47 pm

    While what @awoodland says is correct, that method may cause issues with changes in luminance, I believe.

    HSV and HLS color systems are problematic for a number of reasons. I talked with a color scientist about this recently, and his recommendation was to convert to YIQ or YCbCr space and adjust the the chroma channels (I&Q, or Cb&Cr) accordingly. (You can learn how to do that here and here.)

    Once in one of those spaces, you can get the hue from the angle formed by the chroma channels, by doing hue = atan(cr/cb) (watching for cb == 0). This gives you a value in radians. Simply rotate it by adding the hue rotation amount. Once you’ve done that, you can calculate the magnitude of the chroma with chroma = sqrt(cr*cr+cb*cb). To get back to RGB, calculate the new Cb and Cr (or I & Q) using Cr = chroma * sin (hue), Cb = chroma * cos (hue). Then convert back to RGB as described on the above web pages.

    EDIT: Here’s a solution that I’ve tested and seems to give me the same results as your reference. You can probably collapse some of the dot products into matrix multiplies:

    uniform sampler2DRect inputTexture;
    uniform float   hueAdjust;
    void main ()
    {
        const vec4  kRGBToYPrime = vec4 (0.299, 0.587, 0.114, 0.0);
        const vec4  kRGBToI     = vec4 (0.596, -0.275, -0.321, 0.0);
        const vec4  kRGBToQ     = vec4 (0.212, -0.523, 0.311, 0.0);
    
        const vec4  kYIQToR   = vec4 (1.0, 0.956, 0.621, 0.0);
        const vec4  kYIQToG   = vec4 (1.0, -0.272, -0.647, 0.0);
        const vec4  kYIQToB   = vec4 (1.0, -1.107, 1.704, 0.0);
    
        // Sample the input pixel
        vec4    color   = texture2DRect (inputTexture, gl_TexCoord [ 0 ].xy);
    
        // Convert to YIQ
        float   YPrime  = dot (color, kRGBToYPrime);
        float   I      = dot (color, kRGBToI);
        float   Q      = dot (color, kRGBToQ);
    
        // Calculate the hue and chroma
        float   hue     = atan (Q, I);
        float   chroma  = sqrt (I * I + Q * Q);
    
        // Make the user's adjustments
        hue += hueAdjust;
    
        // Convert back to YIQ
        Q = chroma * sin (hue);
        I = chroma * cos (hue);
    
        // Convert back to RGB
        vec4    yIQ   = vec4 (YPrime, I, Q, 0.0);
        color.r = dot (yIQ, kYIQToR);
        color.g = dot (yIQ, kYIQToG);
        color.b = dot (yIQ, kYIQToB);
    
        // Save the result
        gl_FragColor    = color;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to compute efficiently the Fourier transform of the max of
If I have a standard for loop is there a more efficient way to
I am doing some research in the way i can deploy an application efficiently
Is there a way to have all the text of all the labels, buttons
Is there any way to implement hash tables efficiently in a purely functional language?
Is there an efficient way to version store procedures written in PL/SQL? (I only
Is there an efficient way to clone an object yet leave out specified properties?
Is there a more efficient way to list files from a bucket in Amazon
Is there are more efficient way than the following for selecting the third parent?
Is there a fast/efficiency way to check if a table is empty? DECLARE @StartEndTimes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.