I’m trying to achieve making a gradient color as the design apps (Photoshop for example) does, but can’t get the exact result i want.
My shader creates very nice ‘gradients’ but also contains other colors that are different from the colors I want to switch in.
It looks nice but, my aim is adding blending functions later and make a kind of color correction shader. but first of all I have to get the right colors.
Here is my fragment shader
http://player.thebookofshaders.com/?log=171119111216
uniform vec2 u_resolution;
void main() {
vec2 st = gl_FragCoord.xy/u_resolution.xy;
vec3 color1 = vec3(1.9,0.55,0);
vec3 color2 = vec3(0.226,0.000,0.615);
float mixValue = distance(st,vec2(0,1));
vec3 color = mix(color1,color2,mixValue);
gl_FragColor = vec4(color,mixValue);
}
Thanks in advance..
2
Answers
Ther is a simple mistake when you set up the color value. You used 1.9 for the value of red color channel instead of 1.0, when you set up the orange color.
Change your code to:
Note, the final color channels are clamped to [0, 1], but since you use
mix
to interpolate the colors, a color channel above 1.0 raises the part of the color in the gradient.Preview:
In response to just the title of your question you might also want to consider doing the mix in other color spaces. Your code is mixing in RGB space but you’ll get different results in different spaces.
Example
I would also suggest passing your colors in in a ramp texture. An Nx1 texture and using
Then you can easily blend across 2 colors, 3 colors, 20 colors. You can space out the colors as well by repeating colors etc..
Example:
Note: a 1 dimensional 256×1 texture is how Chrome, Firefox, and Android render both linear and radial gradients. See src