0% found this document useful (0 votes)
9 views4 pages

Ball Halal 1

The document contains a GLSL shader code that processes image colors using various transformations including brightness, contrast, gamma correction, hue, saturation, and value adjustments. It defines functions for color conversion between RGB and HSV formats and applies these transformations to the input texture. The main function computes the final color output based on the specified parameters and renders it to the screen.

Uploaded by

alibadmd2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Ball Halal 1

The document contains a GLSL shader code that processes image colors using various transformations including brightness, contrast, gamma correction, hue, saturation, and value adjustments. It defines functions for color conversion between RGB and HSV formats and applies these transformations to the input texture. The main function computes the final color output based on the specified parameters and renders it to the screen.

Uploaded by

alibadmd2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#pragma header

uniform float iTime;


#define iChannel0 bitmap
#define texture texture2D

uniform float brightness;


uniform float contrast;
uniform float gamma;
uniform float hue;
uniform float saturation;
uniform float value;

vec2 fragCoord = openfl_TextureCoordv*openfl_TextureSize;


vec2 iResolution = openfl_TextureSize;

float MAX(float v1, float v2)


{
return v1 >= v2 ? v1 : v2;
}

float MIN(float v1, float v2)


{
return v1 <= v2 ? v1 : v2;
}

float Truncate(float val)


{
return clamp(val,0.0,1.0);
}

float TruncateAngle(float val)


{
if (val > 360.0) return val - 360.0;
if (val < 0.0) return val + 360.0;

return val;
}

vec3 RGBtoHSV(vec3 color)


{
vec3 hsv;
float _min, _max, delta;

_min = min(color.r, color.g);


_min = min(_min, color.b);

_max = max(color.r, color.g);


_max = max(_max, color.b);

hsv[2] = _max;
delta = _max - _min;

if(_max != 0.0)
{
hsv[1] = delta / _max;
}
else
{
return hsv;
}

if(color.r == _max)
{
hsv[0] = (color.g - color.b ) / delta; // between yellow &
magenta
}

else if(color.g == _max)


{
hsv[0] = 2.0 + (color.b - color.r) / delta; // between cyan & yellow
}
else
{
hsv[0] = 4.0 + (color.r - color.g) / delta; // between magenta &
cyan
}

hsv[0] *= 60.0; // degrees

if( hsv[0] < 0.0)


{
hsv[0] += 360.0;
}
return hsv;
}

vec3 HSVtoRGB(vec3 hsv)


{
vec3 color;
int i;
float f, p, q, t;

if(hsv[1] == 0.0)
{
// achromatic (grey)
color.r = hsv[2];
color.g = hsv[2];
color.b = hsv[2];

return color;
}

hsv[0] /= 60.0; // sector 0 to 5


i = int(floor(hsv[0]));
f = hsv[0] - float(i); // factorial part of h
p = hsv[2] * (1.0 - hsv[1]);
q = hsv[2] * (1.0 - hsv[1] * f);
t = hsv[2] * (1.0 - hsv[1] * (1.0 - f));

if (i == 0)
{
color.r = hsv[2];
color.g = t;
color.b = p;
}
else if (i == 1)
{
color.r = q;
color.g = hsv[2];
color.b = p;
}
else if (i == 2)
{
color.r = p;
color.g = hsv[2];
color.b = t;
}
else if (i == 3)
{
color.r = p;
color.g = q;
color.b = hsv[2];
}
else if (i == 4)
{
color.r = t;
color.g = p;
color.b = hsv[2];
}
else // case 5:
{
color.r = hsv[2];
color.g = p;
color.b = q;
}

return color;
}

// https://beesbuzz.biz/code/hsv_color_transforms.php
vec3 TransformHSV(
vec3 c, // color to transform
float H, // hue shift (in degrees)
float S, // saturation multiplier (scalar)
float V) // value multiplier (scalar)

{
float M_PI = 3.1415926;

float VSU = V * S * cos(H * M_PI / 180.0);


float VSW = V * S * sin(H * M_PI / 180.0);

vec3 ret;

ret.r = Truncate((0.299 * V + 0.701 * VSU + 0.168 * VSW) * c.r


+ (0.587 * V - 0.587*VSU + 0.330 * VSW) * c.g
+ (0.114 * V - 0.114*VSU - 0.497*VSW) * c.b);

ret.g = Truncate((0.299 * V - .299 * VSU - 0.328 * VSW) * c.r


+ (0.587 * V + 0.413 * VSU + 0.035 * VSW) * c.g
+ (0.114 * V - 0.114 * VSU + 0.292 * VSW) * c.b);

ret.b = Truncate((0.299 * V - 0.3 * VSU + 1.25 * VSW) * c.r


+ (0.587 * V - 0.588 * VSU - 1.05 * VSW) * c.g
+ (0.114 * V + 0.886 * VSU - 0.203 * VSW) * c.b);
return ret;
}

void main()
{
vec2 uv = fragCoord.xy / iResolution.xy;
vec4 color = texture2D(bitmap,uv);
vec3 c = color.rgb;

// Set contrast: http://www.dfstudios.co.uk/articles/programming/image-


programming-algorithms/image-processing-algorithms-part-5-contrast-adjustment/
float factor = (1.0158730158730 * (contrast + 1.0)) / (1.0158730158730 -
contrast);
c.r = Truncate(factor * (c.r - 0.5) + 0.5);
c.g = Truncate(factor * (c.g - 0.5) + 0.5);
c.b = Truncate(factor * (c.b - 0.5) + 0.5);
// Set hue, saturation, vibrance
c = TransformHSV(c, hue, saturation, value);
// Set gamma
float gammaCorrection = 1.0 / gamma;
c.r = pow(c.r, gammaCorrection);
c.g = pow(c.g, gammaCorrection);
c.b = pow(c.b, gammaCorrection);
// Set brightness
c.r = Truncate(c.r + brightness);
c.g = Truncate(c.g + brightness);
c.b = Truncate(c.b + brightness);
gl_FragColor = vec4(c,1.0);
}

You might also like