Skip to content

Commit

Permalink
neater
Browse files Browse the repository at this point in the history
  • Loading branch information
PankajBhojwani committed Sep 2, 2021
1 parent 041e5ce commit 12e8d6c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 96 deletions.
153 changes: 62 additions & 91 deletions src/cascadia/TerminalCore/ColorFix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,33 +248,46 @@ double dE00::_DegreesToRadians(double degrees)
return degrees * (M_PI / 180);
};

// Method Description:
// - Clamps the given value to be between 0-255 inclusive
// Arguments:
// - v: the value to clamp
// Return Value:
// - The clamped value
BYTE dE00::Clamp(double v)
ColorFix::ColorFix()
{
if (v <= 0)
return 0;
else if (v >= 255)
return 255;
else
return (BYTE)v;
rgb = 0;
L = 0;
A = 0;
B = 0;
}

ColorFix::ColorFix(COLORREF color)
{
rgb = color;
ToLab();
}

ColorFix::ColorFix(double l, double a, double b)
{
L = l;
A = a;
B = b;
ToRGB();
}

ColorFix::ColorFix(const ColorFix& color)
{
L = color.L;
A = color.A;
B = color.B;
rgb = color.rgb;
}

// Method Description:
// - Populates our L, A, B values, based on our r, g, b values
// - Converts a color in rgb format to a color in lab format
// - Reference: http://www.easyrgb.com/index.php?X=MATH&H=01#text1
// Arguments:
// R, G, B: the rgb values of the input color
// l_s, a_s, b_s: where the output lab format should be stored
void dE00::RGBToLAB(double R, double G, double B, double& l_s, double& a_s, double& b_s)
void ColorFix::ToLab()
{
double var_R = R / 255.0;
double var_G = G / 255.0;
double var_B = B / 255.0;
//dE00::RGBToLAB(r, g, b, L, A, B);
double var_R = r / 255.0;
double var_G = g / 255.0;
double var_B = b / 255.0;

if (var_R > 0.04045)
var_R = pow(((var_R + 0.055) / 1.055), 2.4);
Expand Down Expand Up @@ -315,22 +328,20 @@ void dE00::RGBToLAB(double R, double G, double B, double& l_s, double& a_s, doub
else
var_Z = (7.787 * var_Z) + (16. / 116.);

l_s = (116. * var_Y) - 16.;
a_s = 500. * (var_X - var_Y);
b_s = 200. * (var_Y - var_Z);
};
L = (116. * var_Y) - 16.;
A = 500. * (var_X - var_Y);
B = 200. * (var_Y - var_Z);
}

// Method Description:
// - Populates our r, g, b values, based on our L, A, B values
// - Converts a color in lab format to a color in rgb format
// - Reference: http://www.easyrgb.com/index.php?X=MATH&H=01#text1
// Arguments:
// l_s, a_s, b_s: the lab values of the input color
// R, G, B: where the output rgb format should be stored
void dE00::LABToRGB(double l_s, double a_s, double b_s, double& R, double& G, double& B)
void ColorFix::ToRGB()
{
double var_Y = (l_s + 16.) / 116.;
double var_X = a_s / 500. + var_Y;
double var_Z = var_Y - b_s / 200.;
double var_Y = (L + 16.) / 116.;
double var_X = A / 500. + var_Y;
double var_Z = var_Y - B / 200.;

if (pow(var_Y, 3) > 0.008856)
var_Y = pow(var_Y, 3);
Expand Down Expand Up @@ -370,66 +381,9 @@ void dE00::LABToRGB(double l_s, double a_s, double b_s, double& R, double& G, do
else
var_B = 12.92 * var_B;

R = var_R * 255.;
G = var_G * 255.;
B = var_B * 255.;
};

// Method Description:
// - Converts a color in lab format to a color in rgb format
// - Reference: http://www.easyrgb.com/index.php?X=MATH&H=01#text1
// Arguments:
// - l_s, a_s, b_s: the lab values of the input color
// - rgb: where the output rgb format should be stored
void dE00::LABToRGB(double l_s, double a_s, double b_s, COLORREF& rgb)
{
double _r = 0, _g = 0, _b = 0;
LABToRGB(l_s, a_s, b_s, _r, _g, _b);
rgb = RGB(Clamp(_r), Clamp(_g), Clamp(_b));
};

ColorFix::ColorFix()
{
rgb = 0;
L = 0;
A = 0;
B = 0;
}

ColorFix::ColorFix(COLORREF color)
{
rgb = color;
ToLab();
}

ColorFix::ColorFix(double l, double a, double b)
{
L = l;
A = a;
B = b;
ToRGB();
}

ColorFix::ColorFix(const ColorFix& color)
{
L = color.L;
A = color.A;
B = color.B;
rgb = color.rgb;
}

// Method Description:
// - Populates our L, A, B values, based on our r, g, b values
void ColorFix::ToLab()
{
dE00::RGBToLAB(r, g, b, L, A, B);
}

// Method Description:
// - Populates our r, g, b values, based on our L, A, B values
void ColorFix::ToRGB()
{
dE00::LABToRGB(L, A, B, rgb);
r = _Clamp(var_R * 255.);
g = _Clamp(var_G * 255.);
b = _Clamp(var_B * 255.);
}

// Method Description:
Expand Down Expand Up @@ -490,3 +444,20 @@ bool ColorFix::PerceivableColor(COLORREF back, ColorFix& pColor, double* oldDE,
pColor = *this;
return bChanged;
}

// Method Description:
// - Clamps the given value to be between 0-255 inclusive
// - Converts the result to BYTE
// Arguments:
// - v: the value to clamp
// Return Value:
// - The clamped value
BYTE ColorFix::_Clamp(double v)
{
if (v <= 0)
return 0;
else if (v >= 255)
return 255;
else
return (BYTE)v;
}
9 changes: 4 additions & 5 deletions src/cascadia/TerminalCore/ColorFix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

struct ColorFix
{
public:
ColorFix();
ColorFix(COLORREF color);
ColorFix(double l, double a, double b);
Expand Down Expand Up @@ -35,6 +36,9 @@ struct ColorFix
{
double L, A, B;
};

private:
BYTE _Clamp(double v);
};

struct dE00
Expand All @@ -43,11 +47,6 @@ struct dE00
dE00(ColorFix x1, ColorFix x2, double weightLightness = 1, double weightChroma = 1, double weightHue = 1);
double GetDeltaE();

static void RGBToLAB(double R, double G, double B, double& l_s, double& a_s, double& b_s);
static void LABToRGB(double R, double G, double B, double& l_s, double& a_s, double& b_s);
static void LABToRGB(double l_s, double a_s, double b_s, COLORREF& rgb);
static BYTE Clamp(double v);

private:
double _GetRSubT();
double _GetT();
Expand Down

1 comment on commit 12e8d6c

@github-actions

This comment was marked as outdated.

Please sign in to comment.