Use saturated part colour when calculating edge colour

This commit is contained in:
Trevor SANDY 2021-02-02 18:23:01 +01:00
parent f033578abc
commit 87c23a6d21
5 changed files with 199 additions and 16 deletions

View file

@ -837,8 +837,7 @@ lcCommandLineOptions lcApplication::ParseCommandLineOptions()
if (Options.AutomateEdgeColor && lcIsHighContrast(Options.StudStyle))
{
Options.StdErr += tr("Automate edge color and high contrast stud style cannot be enabled at the same time.\n");
Options.ParseOK = false;
Options.StdErr += tr("High contrast stud and edge color settings are ignored when -aec or --automate-edge-color is set.\n");
}
if (!Options.CameraName.isEmpty())

View file

@ -217,35 +217,37 @@ static void lcAdjustStudStyleColors(std::vector<lcColor>& Colors, lcStudStyle St
if (!Preferences.mAutomateEdgeColor && !lcIsHighContrast(StudStyle))
return;
const float LDIndex = LC_SRGB_TO_LINEAR(Preferences.mPartColorValueLDIndex);
const lcVector4 Edge = lcVector4FromColor(Preferences.mPartEdgeColor);
const lcVector4 DarkEdge = lcVector4FromColor(Preferences.mDarkEdgeColor);
const lcVector4 BlackEdge = lcVector4FromColor(Preferences.mBlackEdgeColor);
const float ContrastControl = Preferences.mPartEdgeContrast;
const float LightDarkControl = Preferences.mAutomateEdgeColor ?
Preferences.mPartColorValueLDIndex :
LC_SRGB_TO_LINEAR(Preferences.mPartColorValueLDIndex);
for (lcColor& Color : Colors)
{
float ValueLuminescence = lcLuminescenceFromSRGB(Color.Value);
if (Preferences.mAutomateEdgeColor)
{
float EdgeLuminescence = 0.0f;
if (Color.Adjusted)
continue;
if (ValueLuminescence > LDIndex)
EdgeLuminescence = ValueLuminescence - (ValueLuminescence * Preferences.mPartEdgeContrast);
else
EdgeLuminescence = (1.0f - ValueLuminescence) * Preferences.mPartEdgeContrast + ValueLuminescence;
float EdgeLuminescence = lcLuminescenceFromSRGB(Color.Edge);
EdgeLuminescence = LC_LINEAR_TO_SRGB(EdgeLuminescence);
Color.Edge = lcVector4(EdgeLuminescence, EdgeLuminescence, EdgeLuminescence, 1.0f);
Color.Edge = lcAlgorithmicEdgeColor(Color.Value, ValueLuminescence, EdgeLuminescence, ContrastControl, LightDarkControl);
Color.Adjusted = true;
}
else
{
if (Color.Code == 4242)
continue;
else if (Color.Code == 0)
Color.Edge = BlackEdge;
else if (ValueLuminescence < LDIndex)
else if (ValueLuminescence < LightDarkControl)
Color.Edge = DarkEdge;
else
Color.Edge = Edge;

View file

@ -12,10 +12,16 @@ struct lcColor
quint32 Code;
int Group;
bool Translucent;
bool Adjusted;
lcVector4 Value;
lcVector4 Edge;
char Name[LC_MAX_COLOR_NAME];
char SafeName[LC_MAX_COLOR_NAME];
lcColor()
{
Translucent = false;
Adjusted = false;
}
};
enum

View file

@ -45,12 +45,14 @@ lcAutomateEdgeColorDialog::lcAutomateEdgeColorDialog(QWidget* Parent, bool ShowH
EdgeSettingsLayout->addWidget(ResetPartEdgeContrastButton,0,3);
}
QLabel* PartColorValueLDIndexLabel = new QLabel(tr("Light/Dark Value:"), this);
QLabel* PartColorValueLDIndexLabel = new QLabel(tr(ShowHighContrastDialog ? "Light/Dark Value:" : "Saturation:"), this);
PartColorValueLDIndex = new QLabel(this);
PartColorValueLDIndexSlider = new QSlider(Qt::Horizontal, this);
PartColorValueLDIndexSlider->setRange(0, 100);
PartColorValueLDIndexSlider->setValue(mPartColorValueLDIndex * 100);
PartColorValueLDIndexSlider->setToolTip(tr("Set to classify where color values are light or dark - e.g. Dark Bluish Gray (72) is light at 0.39."));
PartColorValueLDIndexSlider->setToolTip(tr(ShowHighContrastDialog ?
"Set to classify where color values are light or dark - e.g. Dark Bluish Gray (72) is light at 0.39." :
"Set to specify amount of edge color tint or shade from the saturation adjusted part color"));
connect(PartColorValueLDIndexSlider, SIGNAL(valueChanged(int)), this, SLOT(SliderValueChanged(int)));
emit PartColorValueLDIndexSlider->valueChanged(PartColorValueLDIndexSlider->value());

View file

@ -7,6 +7,7 @@
#define LC_RTOD (static_cast<float>(180 / M_PI))
#define LC_PI (static_cast<float>(M_PI))
#define LC_2PI (static_cast<float>(2 * M_PI))
#define LC_EPSILON (static_cast<float>(0.5f / 255.0f))
#define LC_RGB(r,g,b) LC_RGBA(r,g,b,255)
#define LC_RGBA(r,g,b,a) ((quint32)(((quint8) (r) | ((quint16) (g) << 8)) | (((quint32) (quint8) (b)) << 16) | (((quint32) (quint8) (a)) << 24)))
@ -15,7 +16,7 @@
#define LC_RGBA_BLUE(rgba) ((quint8)(((rgba) >> 16) & 0xff))
#define LC_RGBA_ALPHA(rgba) ((quint8)(((rgba) >> 24) & 0xff))
#define LC_SRGB_TO_LINEAR(v) (powf(v, 2.2f))
#define LC_LINEAR_TO_SRGB(v) (powf(v, 1.0f / 2.2f))
#define LC_LINEAR_TO_SRGB(v) (powf(v, 1.0f / 2.2f))
inline quint32 lcRGBAFromQColor(const QColor& Color)
{
@ -605,6 +606,24 @@ inline lcVector4& operator/=(lcVector4& a, float b)
return a;
}
inline lcVector3& operator+=(lcVector3& a, float b)
{
a.x += b;
a.y += b;
a.z += b;
return a;
}
inline lcVector3& operator-=(lcVector3& a, float b)
{
a.x -= b;
a.y -= b;
a.z -= b;
return a;
}
inline quint32 lcPackNormal(const lcVector3& Normal)
{
quint32 Packed = 0;
@ -648,7 +667,7 @@ inline lcVector4 lcVector4FromColor(quint32 Color)
inline quint32 lcColorFromVector3(const lcVector3& Color)
{
return LC_RGB(Color[0] * 255, Color[1] * 255, Color[2] * 255);
return LC_RGB(roundf(Color[0] * 255), roundf(Color[1] * 255), roundf(Color[2] * 255));
}
inline float lcLuminescenceFromSRGB(lcVector4& Value)
@ -660,6 +679,11 @@ inline float lcLuminescenceFromSRGB(lcVector4& Value)
return 0.2126f * r + 0.7152f * g + 0.0722f * b;
}
inline float lcLuminescenceFromlRGB(lcVector3& Value)
{
return 0.2126f * Value[0] + 0.7152f * Value[1] + 0.0722f * Value[2];
}
inline lcVector3 lcMul(const lcVector3& a, const lcMatrix33& b)
{
return b.r[0] * a[0] + b.r[1] * a[1] + b.r[2] * a[2];
@ -2063,3 +2087,153 @@ bool SphereIntersectsVolume(const Vector3& Center, float Radius, const Vector4*
return true;
}*/
inline lcVector3 lc_RGB2hSL(lcVector3 rgb)
{
int Mi;
float M, m, C, h, S, L; // h is H/60
Mi = (rgb[0] >= rgb[1]) ? 0 : 1;
Mi = (rgb[Mi] >= rgb[2]) ? Mi : 2;
M = rgb[Mi];
m = (rgb[0] < rgb[1]) ? rgb[0] : rgb[1];
m = (m < rgb[2]) ? m : rgb[2];
C = M - m;
L = (M + m) / 2.0f;
if (C < LC_EPSILON) // C == 0.0
{
h = 0.0f;
}
else if (Mi == 0) // M == R
{
h = 0.0f + (rgb[1] - rgb[2]) / C;
}
else if (Mi == 1) // M == G
{
h = 2.0f + (rgb[2] - rgb[0]) / C;
}
else // M = B
{
h = 4.0f + (rgb[0] - rgb[1]) / C;
}
h = (h < 0.0) ? h + 6.0f : h;
h = (h >= 6.0) ? h - 6.0f : h;
S = ((L < (LC_EPSILON / 2.0f)) || (L > (1.0f -(LC_EPSILON / 2.0f))))
? 0.0f : (2.0f * (M - L)) / (1.0f - fabs((2.0f * L) - 1.0f)) ;
return lcVector3(h, S, L);
}
inline lcVector3 lc_hSL2RGB(lcVector3 hSL)
{
lcVector3 rgb;
float h, S, L, C, X, m;
h = hSL[0];
S = hSL[1];
L = hSL[2];
C = (1.0f - fabs(2.0f * L - 1.0f)) * S;
X = C * (1.0f - fabs(fmodf(h, 2.0f) - 1.0f));
if (h < 1.0f)
{
rgb = lcVector3(C, X, 0.0f);
}
else if (h < 2.0f)
{
rgb = lcVector3(X, C, 0.0f);
}
else if (h < 3.0f)
{
rgb = lcVector3(0.0f, C, X);
}
else if (h < 4.0f)
{
rgb = lcVector3(0.0f, X, C);
}
else if (h < 5.0f)
{
rgb = lcVector3(X, 0.0f, C);
}
else
{
rgb = lcVector3(C, 0.0f, X);
}
m = L - C / 2.0f;
rgb += m;
return rgb;
}
inline lcVector4 lcAlgorithmicEdgeColor(const lcVector4& Value, const float ValueLum, const float EdgeLum, const float Contrast, const float Saturation)
{
float y1, yt;
float y0 = ValueLum;
float ye = EdgeLum;
float cont = Contrast;
float sat = Saturation;
lcVector3 rgb, hSL, rgb1, rgbf;
// Determine luma target
if (ye < y0)
{
// Light base color
yt = y0 - cont * y0;
}
else
{
// Dark base color
yt = y0 + cont * (1.0f - y0);
}
// Get base color in hSL
rgb = lcVector3(LC_SRGB_TO_LINEAR(Value[0]), LC_SRGB_TO_LINEAR(Value[1]), LC_SRGB_TO_LINEAR(Value[2]));
hSL = lc_RGB2hSL(lcVector3(Value[0], Value[1], Value[2]));
// Adjust saturation
sat = 4.0f * sat - 2.0f;
if (sat < 0.0f)
{
sat = -sat;
hSL[0] = (hSL[0] < 3.0f) ? hSL[0] + 3.0f : hSL[0] - 3.0f;
}
sat = (sat > 2.0f) ? 2.0f : sat;
if (sat > 1.0f)
{
// Supersaturate
sat -= 1.0f;
hSL[1] += sat * (1.0f - hSL[1]);
}
else
{
// Desaturate
hSL[1] *= sat;
}
// Adjusted color to RGB
rgb1 = lc_hSL2RGB(lcVector3(hSL[0], hSL[1], 0.5f));
// Fix adjusted color luma to target value
y1 = lcLuminescenceFromlRGB(rgb1);
if (yt < y1)
{
// Make darker via scaling
rgbf = (yt/y1) * rgb1;
}
else
{
// Make lighter via scaling anti-color
rgbf = lcVector3(1.0f, 1.0f, 1.0f) - rgb1;
rgbf *= (1.0f - yt) / (1.0f - y1);
rgbf = lcVector3(1.0f, 1.0f, 1.0f) - rgbf;
}
return lcVector4(LC_LINEAR_TO_SRGB(rgbf[0]), LC_LINEAR_TO_SRGB(rgbf[1]), LC_LINEAR_TO_SRGB(rgbf[2]), 1.0f);
}