Get a color from vec4

I got a vec4 which contains so many colors.
how do I get 1 colour and save it?


if(color.r > 0.9){
    float color2 = vec4 color.r;

A vec4 is a container with 4 components. It can represent many colors but it contains exactly one color at the time.

A color can be represented by several formats (rgb, hsv, hsl, rgba).

I guess you need the rgba format. That means the color consists of four components:

  • Red ®
  • Green (g)
  • Blue (b)
  • Opacity (a) (called alpha)

You can express these 4 components in a container. Vec4 is such a container. The components are placed in the given order (r,g,b,a)

vec4 red = vec4(1.0, 0.0, 0.0, 0.0);

You can use Swizzling (see GLSL wiki):

vec4 color= vec4(0.2, 0.2, 0.2, 0.0);
float redComponent = color.r;
float alphaComponent = color.a;

Your code snippet

In your code snippet “color2” is not a color. It is a component of a color. The color is still “color”.

I guess you could do something like that:

if(color.r > 0.9){
    vec4 otherColor = color.rrra; /* no saturation (= black to white) */

uh… I got a vec4 from texture2D, and it has so many different colors in it.
what I want is to get specific color from the vec4 and save the color in a float with only 1 color, so there’s only R = 0.9 in the entire float, not like a depth texture which contains R=0.0,R=0.1 … R=1.0

Maybe someone will ask, “why not just create with float R = 0.9???”.
well… I’m creating a filter which needs to get the float color instantly from a vec4.

doesnt just vector[3] work? - vector[3] is the fourth item in the vector

( vector[0] is the first item )

[0,1,2,3] are the indexes - the indexes are 0 based

the vector is the color e.g. (1.0, 0.0, 0.0, 1.0)

0.9 is not a color. It is a number.

A texture has several colors. One for each texel (= pixel in the image). Each color is a vec4 (or vec3 dependent on the texture format).

A depth map is an array of distances. Each distance is a float. This is different to a texture which is an array of colors (and colors are vectors).

According to your filter, how do you want to identify colors to be processed?

Do you want all red texels? Or all bright texels? Or all dark texels? …

sorry for being vague, I was in a hurry, but I got the solution
you can just use texelFetchto do this
thanks guys!