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 7606701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:28:32+00:00 2026-05-31T00:28:32+00:00

OpenGL can colour a rectangle with a gradient of colours from 1 side to

  • 0

OpenGL can colour a rectangle with a gradient of colours from 1 side to the other. I’m using the following code for that in C++

glBegin(GL_QUADS);
{
    glColor3d(simulationSettings->hotColour.redF(), simulationSettings->hotColour.greenF(), simulationSettings->hotColour.blueF());
    glVertex2d(keyPosX - keyWidth/2, keyPosY + keyHight/2);
    glColor3d(simulationSettings->coldColour.redF(), simulationSettings->coldColour.greenF(), simulationSettings->coldColour.blueF());
    glVertex2d(keyPosX - keyWidth/2, keyPosY - keyHight/2);
    glColor3d(simulationSettings->coldColour.redF(), simulationSettings->coldColour.greenF(), simulationSettings->coldColour.blueF());
    glVertex2d(keyPosX + keyWidth/2, keyPosY - keyHight/2);
    glColor3d(simulationSettings->hotColour.redF(), simulationSettings->hotColour.greenF(), simulationSettings->hotColour.blueF());
    glVertex2d(keyPosX + keyWidth/2, keyPosY + keyHight/2);
}

I’m using some Qt libraries to do the conversions between HSV and RGB. As you can see from the code, I’m drawing a rectangle with colour gradient from what I call hotColour to coldColour.

Why am I doing this? The program I made draws 3D Vectors in space and indicates their length by their colour. The user is offered to choose the hot (high value) and cold (low value) colours, and the program will automatically do the gradient using HSV scaling.

Why HSV scaling? because HSV is single valued along the colour map I’m using, and creating gradients with it linearly is a very easy task. For the user to select the colours, I offer him a QColourDialog colour map

http://qt-project.org/doc/qt-4.8/qcolordialog.html

On this colour map, you can see that red is available on the right and left side, making it impossible to have a linear scale for this colour-map with RGB. But with HSV, the linear scale is very easily achievable, where I just have to use a linear scale between 0 and 360 for Hue values.

With this paradigm, we can see that hot and cold colours define the direction of the gradient, so for example, if I choose hue to be 0 for cold and 359 for hot, HSV will give me a gradient between 0 and 359, and will include the whole spectrum of colours in the gradient; whilst, in OpenGL, it will basically go from red to red, which is no gradient!!!!!!

How can I force OpenGL to use an HSV gradient rather than RGB? The only idea that occurs to me is slicing the rectangle I wanna colour and do many gradients over smaller rectangles, but I think this isn’t the most efficient way to do it.

Any ideas?

  • 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-31T00:28:34+00:00Added an answer on May 31, 2026 at 12:28 am

    How can I force OpenGL to use an HSV gradient rather than RGB?

    I wouldn’t call it “forcing”, but “teaching”. The default way of OpenGL to interpolate vertex attributes vectors is by barycentric interpolation of the single vector elements based on the NDC coordinates of the fragment.

    You must tell OpenGL how to turn those barycentric interpolated HSV values into RGB.

    For this we introduce a fragment shader that assumes the color vertex attribute not being RGB but HSV.

    #version 120
    varying vec3 vertex_hsv; /* set this in appropriate vertex shader to the vertex attribute data*/
    
    vec3 hsv2rgb(vec3 hsv)
    {
        float h = hsv.x * 6.; /* H in 0°=0 ... 1=360° */
        float s = hsv.y;
        float v = hsv.z;
        float c = v * s;
    
        vec2 cx = vec2(v*s, c * ( 1 - abs(mod(h, 2.)-1.) ));
    
        vec3 rgb = vec3(0., 0., 0.);
        if( h < 1. ) {
            rgb.rg = cx;
        } else if( h < 2. ) {
            rgb.gr = cx;
        } else if( h < 3. ) {
            rgb.gb = cx;
        } else if( h < 4. ) {
            rgb.bg = cx;
        } else if( h < 5. ) {
            rgb.br = cx;
        } else {
            rgb.rb = cx;
        }
        return rgb + vec3(v-cx.y);
    }
    
    void main()
    {
        gl_FragColor = hsv2rgb(vertex_hsv);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been reading that M3G is something based on OpenGL ES...so can I work
Does anyone know how I can achieve the following effect in OpenGL: Change the
It seems that all of the documentation I can find about OpenGL-ES says something
I have built an OpenGL Viewer control that can simply be dropped onto a
I'm drawing an object (say, a cube) in OpenGL that a user can rotate
I am looking for a technique in OpenGL that I can use in order
When using glBegin() and glEnd() in opengl you are able to set and change
I'm trying to simulate a particle system using OpenGl but I can't get it
this is simple rectangle drawing code in opengl..i want to fill the rectangle with
I understand that by setting the depth function in OpenGL ES one can control

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.