mirror of
https://github.com/ddnet/ddnet.git
synced 2024-11-20 06:58:20 +00:00
60 lines
1.1 KiB
C
60 lines
1.1 KiB
C
|
|
#ifndef BASE_COLOR_H
|
|
#define BASE_COLOR_H
|
|
|
|
#include "vmath.h"
|
|
|
|
/*
|
|
Title: Color handling
|
|
*/
|
|
|
|
/*
|
|
Function: HueToRgb
|
|
Converts Hue to RGB
|
|
*/
|
|
inline float HueToRgb(float v1, float v2, float h)
|
|
{
|
|
if(h < 0.0f) h += 1;
|
|
if(h > 1.0f) h -= 1;
|
|
if((6.0f * h) < 1.0f) return v1 + (v2 - v1) * 6.0f * h;
|
|
if((2.0f * h) < 1.0f) return v2;
|
|
if((3.0f * h) < 2.0f) return v1 + (v2 - v1) * ((2.0f/3.0f) - h) * 6.0f;
|
|
return v1;
|
|
}
|
|
|
|
/*
|
|
Function: HslToRgb
|
|
Converts HSL to RGB
|
|
*/
|
|
inline vec3 HslToRgb(vec3 HSL)
|
|
{
|
|
if(HSL.s == 0.0f)
|
|
return vec3(HSL.l, HSL.l, HSL.l);
|
|
else
|
|
{
|
|
float v2 = HSL.l < 0.5f ? HSL.l * (1.0f + HSL.s) : (HSL.l+HSL.s) - (HSL.s*HSL.l);
|
|
float v1 = 2.0f * HSL.l - v2;
|
|
|
|
return vec3(HueToRgb(v1, v2, HSL.h + (1.0f/3.0f)), HueToRgb(v1, v2, HSL.h), HueToRgb(v1, v2, HSL.h - (1.0f/3.0f)));
|
|
}
|
|
}
|
|
|
|
/*
|
|
Function: HexToRgba
|
|
Converts Hex to Rgba
|
|
|
|
Remarks: Hex should be RGBA8
|
|
*/
|
|
inline vec4 HexToRgba(int hex)
|
|
{
|
|
vec4 c;
|
|
c.r = ((hex >> 24) & 0xFF) / 255.0f;
|
|
c.g = ((hex >> 16) & 0xFF) / 255.0f;
|
|
c.b = ((hex >> 8) & 0xFF) / 255.0f;
|
|
c.a = (hex & 0xFF) / 255.0f;
|
|
|
|
return c;
|
|
}
|
|
|
|
#endif
|