From a31361fe05e4419cb058ae04fcc2f2b26cc6314d Mon Sep 17 00:00:00 2001 From: Kirill Polyakov Date: Tue, 26 Mar 2024 19:38:12 +0300 Subject: [PATCH] Added a method for equating any one to the nearest of the given ones --- .../3dParty/html/css/src/StyleProperties.cpp | 49 ++++++++++++++++++- Common/3dParty/html/css/src/StyleProperties.h | 1 + .../html/css/src/xhtml/CDocumentStyle.cpp | 2 +- .../html/css/src/xhtml/CXmlElement.cpp | 11 +++-- 4 files changed, 55 insertions(+), 8 deletions(-) diff --git a/Common/3dParty/html/css/src/StyleProperties.cpp b/Common/3dParty/html/css/src/StyleProperties.cpp index adf65ad7c0..d9df7d923e 100644 --- a/Common/3dParty/html/css/src/StyleProperties.cpp +++ b/Common/3dParty/html/css/src/StyleProperties.cpp @@ -621,9 +621,9 @@ namespace NSCSS TRGB oRGB = ConvertHEXtoRGB(*pValue); return RGB_TO_INT(oRGB.uchRed, oRGB.uchGreen, oRGB.uchBlue); } + default: + return 0; } - - return 0; } double CColor::ToDouble() const @@ -641,6 +641,47 @@ namespace NSCSS } } + std::wstring CColor::EquateToColor(const std::vector& arColors) const + { + if (arColors.empty()) + return L"none"; + + TRGB oColor; + + switch(m_oValue.m_enType) + { + case ColorRGB: oColor = *static_cast(m_oValue.m_pColor); break; + case ColorHEX: oColor = ConvertHEXtoRGB(*static_cast(m_oValue.m_pColor)); break; + default: return L"none"; + } + + TRGB oTempColor; + std::wstring wsSelectedColor; + double dMinDistance = DBL_MAX; + double dDistance; + + for (const std::wstring& wsColor : arColors) + { + oTempColor = ConvertHEXtoRGB(wsColor); + dDistance = sqrt(pow(oTempColor.uchRed - oColor.uchRed, 2) + pow(oTempColor.uchGreen - oColor.uchGreen, 2) + pow(oTempColor.uchBlue - oColor.uchBlue, 2)); + + if (dDistance < dMinDistance) + { + dMinDistance = dDistance; + wsSelectedColor = wsColor; + } + } + + std::map::const_iterator oIter = + std::find_if(NSConstValues::COLORS.begin(), NSConstValues::COLORS.end(), [&wsSelectedColor](const std::pair& oPair) + { return wsSelectedColor == oPair.second; }); + + if (NSConstValues::COLORS.end() == oIter) + return std::wstring(); + + return oIter->first; + } + TRGB CColor::ToRGB() const { switch(m_oValue.m_enType) @@ -802,6 +843,8 @@ namespace NSCSS break; } + default: + break; } return true; @@ -878,6 +921,8 @@ namespace NSCSS wsValue = L"rotate("; break; } + default: + break; } return wsValue; diff --git a/Common/3dParty/html/css/src/StyleProperties.h b/Common/3dParty/html/css/src/StyleProperties.h index eac50a2463..9e843e874c 100644 --- a/Common/3dParty/html/css/src/StyleProperties.h +++ b/Common/3dParty/html/css/src/StyleProperties.h @@ -237,6 +237,7 @@ namespace NSCSS int ToInt() const override; double ToDouble() const override; std::wstring ToWString() const override; + std::wstring EquateToColor(const std::vector& arColors) const; TRGB ToRGB() const; }; diff --git a/Common/3dParty/html/css/src/xhtml/CDocumentStyle.cpp b/Common/3dParty/html/css/src/xhtml/CDocumentStyle.cpp index b785cb16c6..6d6f59a66a 100644 --- a/Common/3dParty/html/css/src/xhtml/CDocumentStyle.cpp +++ b/Common/3dParty/html/css/src/xhtml/CDocumentStyle.cpp @@ -419,7 +419,7 @@ namespace NSCSS if (oStyle.m_oText.GetDecoration().m_oLine.Underline()) oXmlElement.AddPropertiesInR(RProperties::R_U, (!oStyle.m_oText.GetDecoration().m_oStyle.Empty()) ? oStyle.m_oText.GetDecoration().m_oStyle.ToWString() : L"single"); - oXmlElement.AddPropertiesInR(RProperties::R_Highlight, oStyle.m_oBackground.GetColor().ToWString()); + oXmlElement.AddPropertiesInR(RProperties::R_Highlight, oStyle.m_oBackground.GetColor().EquateToColor({L"000000", L"0000FF", L"00FFFF", L"00FF00", L"FF00FF", L"FF0000", L"FFFF00", L"FFFFFF", L"00008B", L"008B8B", L"006400", L"8B008B", L"8B0000", L"8B8000", L"A9A9A9", L"D3D3D3"})); oXmlElement.AddPropertiesInR(RProperties::R_Color, oStyle.m_oText.GetColor().ToWString()); oXmlElement.AddPropertiesInR(RProperties::R_RFonts, oStyle.m_oFont.GetFamily().ToWString()); oXmlElement.AddPropertiesInR(RProperties::R_I, oStyle.m_oFont.GetStyle().ToWString()); diff --git a/Common/3dParty/html/css/src/xhtml/CXmlElement.cpp b/Common/3dParty/html/css/src/xhtml/CXmlElement.cpp index dd7607bc31..e25590e434 100644 --- a/Common/3dParty/html/css/src/xhtml/CXmlElement.cpp +++ b/Common/3dParty/html/css/src/xhtml/CXmlElement.cpp @@ -448,11 +448,12 @@ std::wstring CXmlElement::ConvertRStyle(bool bIsLite) const sRStyle += L""; break; } -// case CSSProperties::RunnerProperties::R_Highlight: -// { -// sRStyle += L""; -// break; -// } + case CSSProperties::RunnerProperties::R_Highlight: + { + if (!oItem.second.empty()) + sRStyle += L""; + break; + } case CSSProperties::RunnerProperties::R_SmallCaps: { if (oItem.second == L"smallCaps")