mirror of
https://repo.dactyloidae.xyz/Dactyloidae/UXP.git
synced 2026-09-30 04:17:31 +09:00
Issue #2489: Adding RGB to HSL function
This commit is contained in:
parent
ff5f3b2b2d
commit
cb2547efa3
2 changed files with 40 additions and 0 deletions
|
|
@ -348,6 +348,41 @@ NS_HSL2RGB(float h, float s, float l)
|
||||||
return NS_RGB(r, g, b);
|
return NS_RGB(r, g, b);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RGB to HSL conversion function
|
||||||
|
// The uint8_t RGB parameters are expected to be in the range 0-255
|
||||||
|
// Returns HSL values in the range: H[0-1], S[0-1], L[0-1]
|
||||||
|
void
|
||||||
|
NS_RGB2HSL(uint8_t aR, uint8_t aG, uint8_t aB, float* aH, float* aS, float* aL)
|
||||||
|
{
|
||||||
|
float r = aR / 255.0f;
|
||||||
|
float g = aG / 255.0f;
|
||||||
|
float b = aB / 255.0f;
|
||||||
|
|
||||||
|
float max = std::max(r, std::max(g, b));
|
||||||
|
float min = std::min(r, std::min(g, b));
|
||||||
|
float h, s, l = (max + min) / 2.0f;
|
||||||
|
|
||||||
|
if (max == min) {
|
||||||
|
h = s = 0.0f; // achromatic
|
||||||
|
} else {
|
||||||
|
float d = max - min;
|
||||||
|
s = l > 0.5f ? d / (2.0f - max - min) : d / (max + min);
|
||||||
|
|
||||||
|
if (max == r) {
|
||||||
|
h = (g - b) / d + (g < b ? 6.0f : 0.0f);
|
||||||
|
} else if (max == g) {
|
||||||
|
h = (b - r) / d + 2.0f;
|
||||||
|
} else {
|
||||||
|
h = (r - g) / d + 4.0f;
|
||||||
|
}
|
||||||
|
h /= 6.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
*aH = h;
|
||||||
|
*aS = s;
|
||||||
|
*aL = l;
|
||||||
|
}
|
||||||
|
|
||||||
const char*
|
const char*
|
||||||
NS_RGBToColorName(nscolor aColor)
|
NS_RGBToColorName(nscolor aColor)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -113,6 +113,11 @@ const char * const * NS_AllColorNames(size_t *aSizeArray);
|
||||||
// the float parameters are all expected to be in the range 0-1
|
// the float parameters are all expected to be in the range 0-1
|
||||||
nscolor NS_HSL2RGB(float h, float s, float l);
|
nscolor NS_HSL2RGB(float h, float s, float l);
|
||||||
|
|
||||||
|
// function to convert from RGB color space to HSL color space
|
||||||
|
// the uint8_t RGB parameters are expected to be in the range 0-255
|
||||||
|
// the float HSL parameters will be set to values in the range 0-1
|
||||||
|
void NS_RGB2HSL(uint8_t aR, uint8_t aG, uint8_t aB, float* aH, float* aS, float* aL);
|
||||||
|
|
||||||
// Return a color name for the given nscolor. If there is no color
|
// Return a color name for the given nscolor. If there is no color
|
||||||
// name for it, returns null. If there are multiple possible color
|
// name for it, returns null. If there are multiple possible color
|
||||||
// names for the given color, the first one in nsColorNameList.h
|
// names for the given color, the first one in nsColorNameList.h
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue