leocad/common/lc_colors.cpp

413 lines
10 KiB
C++
Raw Normal View History

2013-08-09 06:57:18 +02:00
#include "lc_global.h"
#include "lc_colors.h"
#include "lc_file.h"
#include "lc_library.h"
2021-01-24 12:53:02 +01:00
#include "lc_application.h"
2013-08-09 06:57:18 +02:00
#include <float.h>
2019-07-05 02:06:26 +02:00
std::vector<lcColor> gColorList;
2013-08-09 06:57:18 +02:00
lcColorGroup gColorGroups[LC_NUM_COLORGROUPS];
int gEdgeColor;
int gDefaultColor;
static void GetToken(char*& Ptr, char* Token)
{
while (*Ptr && *Ptr <= 32)
Ptr++;
while (*Ptr > 32)
*Token++ = *Ptr++;
*Token = 0;
}
static void lcAdjustStudStyleColors(std::vector<lcColor>& Colors, lcStudStyle StudStyle)
2013-08-09 06:57:18 +02:00
{
2021-01-26 01:52:49 +01:00
const lcPreferences& Preferences = lcGetPreferences();
2021-01-26 23:58:02 +01:00
if (!Preferences.mAutomateEdgeColor && !lcIsHighContrast(StudStyle))
return;
const lcVector4 Edge = lcVector4FromColor(Preferences.mPartEdgeColor);
const lcVector4 DarkEdge = lcVector4FromColor(Preferences.mDarkEdgeColor);
const lcVector4 BlackEdge = lcVector4FromColor(Preferences.mBlackEdgeColor);
2021-01-26 01:52:49 +01:00
const float ContrastControl = Preferences.mPartEdgeContrast;
2021-02-15 19:43:23 +01:00
const float LightDarkControl = Preferences.mAutomateEdgeColor ? Preferences.mPartColorValueLDIndex : LC_SRGB_TO_LINEAR(Preferences.mPartColorValueLDIndex);
for (lcColor& Color : Colors)
2021-01-24 12:53:02 +01:00
{
2021-11-15 03:34:24 +01:00
const lcVector3 LinearColor = lcSRGBToLinear(lcVector3(Color.Value));
2021-02-07 19:14:54 +01:00
const float ValueLuminescence = lcLuminescence(LinearColor);
2021-01-24 12:53:02 +01:00
if (Preferences.mAutomateEdgeColor)
{
if (Color.Adjusted)
continue;
2021-01-24 12:53:02 +01:00
2021-02-07 19:14:54 +01:00
const float EdgeLuminescence = lcLuminescence(lcSRGBToLinear(lcVector3(Color.Edge)));
2021-01-24 12:53:02 +01:00
2021-02-07 19:14:54 +01:00
Color.Edge = lcAlgorithmicEdgeColor(LinearColor, ValueLuminescence, EdgeLuminescence, ContrastControl, LightDarkControl);
Color.Adjusted = true;
2021-01-24 12:53:02 +01:00
}
else
{
if (Color.Code == LC_STUD_CYLINDER_COLOR_CODE)
continue;
else if (Preferences.mBlackEdgeColorEnabled && Color.Code == 0)
Color.Edge = BlackEdge;
else if (Preferences.mDarkEdgeColorEnabled && ValueLuminescence < LightDarkControl)
Color.Edge = DarkEdge;
else if (Preferences.mPartEdgeColorEnabled)
Color.Edge = Edge;
}
2021-01-20 11:56:30 +01:00
}
2021-01-22 22:40:16 +01:00
}
static std::vector<lcColor> lcParseColorFile(lcFile& File)
2021-01-22 22:40:16 +01:00
{
char Line[1024], Token[1024];
std::vector<lcColor> Colors;
lcColor Color;
2013-08-09 06:57:18 +02:00
while (File.ReadLine(Line, sizeof(Line)))
{
char* Ptr = Line;
GetToken(Ptr, Token);
if (strcmp(Token, "0"))
continue;
GetToken(Ptr, Token);
strupr(Token);
if (strcmp(Token, "!COLOUR"))
continue;
2017-12-13 07:17:14 +01:00
Color.Code = ~0U;
2013-08-09 06:57:18 +02:00
Color.Translucent = false;
2023-08-10 06:22:52 +02:00
Color.Chrome = false;
Color.Rubber = false;
Color.Group = LC_COLORGROUP_SOLID;
2013-08-09 06:57:18 +02:00
Color.Value[0] = FLT_MAX;
Color.Value[1] = FLT_MAX;
Color.Value[2] = FLT_MAX;
Color.Value[3] = 1.0f;
Color.Edge[0] = FLT_MAX;
Color.Edge[1] = FLT_MAX;
Color.Edge[2] = FLT_MAX;
Color.Edge[3] = 1.0f;
GetToken(Ptr, Token);
strncpy(Color.Name, Token, sizeof(Color.Name));
Color.Name[LC_MAX_COLOR_NAME - 1] = 0;
strncpy(Color.SafeName, Color.Name, sizeof(Color.SafeName));
for (char* Underscore = strchr((char*)Color.Name, '_'); Underscore; Underscore = strchr(Underscore, '_'))
*Underscore = ' ';
for (GetToken(Ptr, Token); Token[0]; GetToken(Ptr, Token))
{
strupr(Token);
if (!strcmp(Token, "CODE"))
{
GetToken(Ptr, Token);
Color.Code = atoi(Token);
}
else if (!strcmp(Token, "VALUE"))
{
GetToken(Ptr, Token);
if (Token[0] == '#')
Token[0] = ' ';
int Value;
2017-04-03 02:15:09 +02:00
if (sscanf(Token, "%x", &Value) != 1)
Value = 0;
2013-08-09 06:57:18 +02:00
Color.Value[2] = (float)(Value & 0xff) / 255.0f;
Value >>= 8;
Color.Value[1] = (float)(Value & 0xff) / 255.0f;
Value >>= 8;
Color.Value[0] = (float)(Value & 0xff) / 255.0f;
}
else if (!strcmp(Token, "EDGE"))
{
GetToken(Ptr, Token);
if (Token[0] == '#')
Token[0] = ' ';
int Value;
2017-04-03 02:15:09 +02:00
if (sscanf(Token, "%x", &Value) != 1)
Value = 0;
2013-08-09 06:57:18 +02:00
Color.Edge[2] = (float)(Value & 0xff) / 255.0f;
Value >>= 8;
Color.Edge[1] = (float)(Value & 0xff) / 255.0f;
Value >>= 8;
Color.Edge[0] = (float)(Value & 0xff) / 255.0f;
}
else if (!strcmp(Token, "ALPHA"))
{
GetToken(Ptr, Token);
int Value = atoi(Token);
Color.Value[3] = (float)(Value & 0xff) / 255.0f;
if (Value != 255)
Color.Translucent = true;
if (Value == 128)
Color.Group = LC_COLORGROUP_TRANSLUCENT;
2013-08-09 06:57:18 +02:00
else if (Value != 0)
Color.Group = LC_COLORGROUP_SPECIAL;
2013-08-09 06:57:18 +02:00
}
2023-08-10 06:22:52 +02:00
else if (!strcmp(Token, "CHROME"))
{
Color.Chrome = true;
Color.Group = LC_COLORGROUP_SPECIAL;
}
else if (!strcmp(Token, "RUBBER"))
{
Color.Rubber = true;
Color.Group = LC_COLORGROUP_SPECIAL;
}
else if (!strcmp(Token, "PEARLESCENT") || !strcmp(Token, "MATTE_METALIC") || !strcmp(Token, "METAL") || !strcmp(Token, "LUMINANCE"))
2013-08-09 06:57:18 +02:00
{
Color.Group = LC_COLORGROUP_SPECIAL;
2013-08-09 06:57:18 +02:00
}
else if (!strcmp(Token, "MATERIAL"))
{
Color.Group = LC_COLORGROUP_SPECIAL;
2013-08-09 06:57:18 +02:00
break; // Material is always last so ignore it and the rest of the line.
}
}
2017-12-13 07:17:14 +01:00
if (Color.Code == ~0U || Color.Value[0] == FLT_MAX)
2013-08-09 06:57:18 +02:00
continue;
if (Color.Edge[0] == FLT_MAX)
{
Color.Edge[0] = 33.0f / 255.0f;
Color.Edge[1] = 33.0f / 255.0f;
Color.Edge[2] = 33.0f / 255.0f;
}
2021-01-22 22:40:16 +01:00
bool Duplicate = false;
2013-08-09 06:57:18 +02:00
2019-07-05 02:06:26 +02:00
for (lcColor& ExistingColor : Colors)
2013-08-09 06:57:18 +02:00
{
2019-07-05 02:06:26 +02:00
if (ExistingColor.Code == Color.Code)
2013-08-09 06:57:18 +02:00
{
2019-07-05 02:06:26 +02:00
ExistingColor = Color;
2021-01-22 22:40:16 +01:00
Duplicate = true;
2013-08-09 06:57:18 +02:00
break;
}
}
if (!Duplicate)
Colors.push_back(Color);
}
2013-08-09 06:57:18 +02:00
return Colors;
}
2013-08-09 06:57:18 +02:00
bool lcLoadColorFile(lcFile& File, lcStudStyle StudStyle)
{
std::vector<lcColor> Colors = lcParseColorFile(File);
const bool Valid = !Colors.empty();
if (Valid)
lcAdjustStudStyleColors(Colors, StudStyle);
2013-08-09 06:57:18 +02:00
bool FoundMain = false, FoundEdge = false, FoundStudCylinder = false, FoundNoColor = false;
for (const lcColor& Color : Colors)
{
switch (Color.Code)
2021-01-22 22:40:16 +01:00
{
case 16:
FoundMain = true;
break;
case 24:
FoundEdge = true;
break;
case LC_STUD_CYLINDER_COLOR_CODE:
FoundStudCylinder = true;
break;
2021-02-01 02:32:38 +01:00
case LC_COLOR_NOCOLOR:
FoundNoColor = true;
break;
2021-01-22 22:40:16 +01:00
}
}
if (!FoundMain)
{
lcColor MainColor;
MainColor.Code = 16;
MainColor.Translucent = false;
2023-08-10 06:22:52 +02:00
MainColor.Chrome = false;
MainColor.Rubber = false;
MainColor.Group = LC_COLORGROUP_SOLID;
MainColor.Value[0] = 1.0f;
MainColor.Value[1] = 1.0f;
MainColor.Value[2] = 0.5f;
MainColor.Value[3] = 1.0f;
MainColor.Edge[0] = 0.2f;
MainColor.Edge[1] = 0.2f;
MainColor.Edge[2] = 0.2f;
MainColor.Edge[3] = 1.0f;
strcpy(MainColor.Name, "Main Color");
strcpy(MainColor.SafeName, "Main_Color");
Colors.push_back(MainColor);
}
2021-01-22 22:40:16 +01:00
if (!FoundEdge)
{
lcColor EdgeColor;
EdgeColor.Code = 24;
EdgeColor.Translucent = false;
2023-08-10 06:22:52 +02:00
EdgeColor.Chrome = false;
EdgeColor.Rubber = false;
EdgeColor.Group = LC_NUM_COLORGROUPS;
EdgeColor.Value[0] = 0.5f;
EdgeColor.Value[1] = 0.5f;
EdgeColor.Value[2] = 0.5f;
EdgeColor.Value[3] = 1.0f;
EdgeColor.Edge[0] = 0.2f;
EdgeColor.Edge[1] = 0.2f;
EdgeColor.Edge[2] = 0.2f;
EdgeColor.Edge[3] = 1.0f;
strcpy(EdgeColor.Name, "Edge Color");
strcpy(EdgeColor.SafeName, "Edge_Color");
Colors.push_back(EdgeColor);
}
2013-08-09 06:57:18 +02:00
if (!FoundStudCylinder)
{
2021-01-26 01:52:49 +01:00
const lcPreferences& Preferences = lcGetPreferences();
2021-01-26 16:42:19 +01:00
lcColor StudCylinderColor;
2021-01-26 01:52:49 +01:00
StudCylinderColor.Code = LC_STUD_CYLINDER_COLOR_CODE;
2021-01-26 16:42:19 +01:00
StudCylinderColor.Translucent = false;
2023-08-10 06:22:52 +02:00
StudCylinderColor.Chrome = false;
StudCylinderColor.Rubber = false;
2021-01-26 16:42:19 +01:00
StudCylinderColor.Group = LC_NUM_COLORGROUPS;
StudCylinderColor.Value = lcVector4FromColor(Preferences.mStudCylinderColor);
StudCylinderColor.Edge = lcVector4FromColor(Preferences.mPartEdgeColor);
strcpy(StudCylinderColor.Name, "Stud Cylinder Color");
strcpy(StudCylinderColor.SafeName, "Stud_Cylinder_Color");
2021-01-26 16:42:19 +01:00
Colors.push_back(StudCylinderColor);
2013-08-09 06:57:18 +02:00
}
2021-02-01 02:32:38 +01:00
if (!FoundNoColor)
{
lcColor NoColor;
NoColor.Code = LC_COLOR_NOCOLOR;
NoColor.Translucent = false;
2023-08-10 06:22:52 +02:00
NoColor.Chrome = false;
NoColor.Rubber = false;
2021-02-01 02:32:38 +01:00
NoColor.Group = LC_NUM_COLORGROUPS;
NoColor.Value[0] = 0.5f;
NoColor.Value[1] = 0.5f;
NoColor.Value[2] = 0.5f;
NoColor.Value[3] = 1.0f;
NoColor.Edge[0] = 0.2f;
NoColor.Edge[1] = 0.2f;
NoColor.Edge[2] = 0.2f;
NoColor.Edge[3] = 1.0f;
strcpy(NoColor.Name, "No Color");
strcpy(NoColor.SafeName, "No_Color");
Colors.push_back(NoColor);
}
for (lcColor& Color : gColorList)
Color.Group = LC_NUM_COLORGROUPS;
for (int GroupIdx = 0; GroupIdx < LC_NUM_COLORGROUPS; GroupIdx++)
gColorGroups[GroupIdx].Colors.clear();
gColorGroups[0].Name = QApplication::tr("Solid", "Colors");
gColorGroups[1].Name = QApplication::tr("Translucent", "Colors");
gColorGroups[2].Name = QApplication::tr("Special", "Colors");
for (lcColor& Color : Colors)
{
int ColorIndex;
2013-08-09 06:57:18 +02:00
for (ColorIndex = 0; ColorIndex < static_cast<int>(gColorList.size()); ColorIndex++)
if (gColorList[ColorIndex].Code == Color.Code)
break;
2013-08-09 06:57:18 +02:00
2021-01-23 19:52:45 +01:00
if (ColorIndex == static_cast<int>(gColorList.size()))
gColorList.push_back(Color);
else
gColorList[ColorIndex] = Color;
2013-08-09 06:57:18 +02:00
if (Color.Group != LC_NUM_COLORGROUPS)
gColorGroups[Color.Group].Colors.push_back(ColorIndex);
if (Color.Code == 16)
gDefaultColor = ColorIndex;
else if (Color.Code == 24)
gEdgeColor = ColorIndex;
}
2021-01-22 22:40:16 +01:00
return Valid;
2013-08-09 06:57:18 +02:00
}
void lcLoadDefaultColors(lcStudStyle StudStyle)
2013-08-09 06:57:18 +02:00
{
2020-12-11 23:14:54 +01:00
lcDiskFile ConfigFile(":/resources/ldconfig.ldr");
2017-02-05 04:37:40 +01:00
2020-12-11 23:14:54 +01:00
if (ConfigFile.Open(QIODevice::ReadOnly))
2021-01-22 22:40:16 +01:00
lcLoadColorFile(ConfigFile, StudStyle);
2013-08-09 06:57:18 +02:00
}
2017-12-02 21:22:04 +01:00
int lcGetColorIndex(quint32 ColorCode)
2013-08-09 06:57:18 +02:00
{
2019-07-09 03:34:34 +02:00
for (size_t ColorIdx = 0; ColorIdx < gColorList.size(); ColorIdx++)
2013-08-09 06:57:18 +02:00
if (gColorList[ColorIdx].Code == ColorCode)
2019-07-09 17:47:15 +02:00
return (int)ColorIdx;
2013-08-09 06:57:18 +02:00
lcColor Color;
Color.Code = ColorCode;
Color.Translucent = false;
2023-08-10 06:22:52 +02:00
Color.Chrome = false;
Color.Rubber = false;
2013-08-09 06:57:18 +02:00
Color.Edge[0] = 0.2f;
Color.Edge[1] = 0.2f;
Color.Edge[2] = 0.2f;
Color.Edge[3] = 1.0f;
if (ColorCode & LC_COLOR_DIRECT)
{
Color.Value[0] = (float)((ColorCode & 0xff0000) >> 16) / 255.0f;
2021-01-22 22:40:16 +01:00
Color.Value[1] = (float)((ColorCode & 0x00ff00) >> 8) / 255.0f;
Color.Value[2] = (float)((ColorCode & 0x0000ff) >> 0) / 255.0f;
2013-08-09 06:57:18 +02:00
Color.Value[3] = 1.0f;
sprintf(Color.Name, "Color %06X", ColorCode & 0xffffff);
sprintf(Color.SafeName, "Color_%06X", ColorCode & 0xffffff);
}
else
{
Color.Value[0] = 0.5f;
Color.Value[1] = 0.5f;
Color.Value[2] = 0.5f;
Color.Value[3] = 1.0f;
sprintf(Color.Name, "Color %03d", ColorCode);
sprintf(Color.SafeName, "Color_%03d", ColorCode);
}
2019-07-05 02:06:26 +02:00
gColorList.push_back(Color);
return (int)gColorList.size() - 1;
2013-08-09 06:57:18 +02:00
}