From 8541e78cde14b94354be17a94a8a172c4b7f9604 Mon Sep 17 00:00:00 2001 From: kgv Date: Fri, 20 Sep 2019 22:34:58 +0300 Subject: [PATCH] #Quantity_Color::Name() - look for nearest sRGB color rather than nearest linear RGB color. --- src/Quantity/Quantity_Color.cxx | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/Quantity/Quantity_Color.cxx b/src/Quantity/Quantity_Color.cxx index 92f512e846..08ca35c132 100644 --- a/src/Quantity/Quantity_Color.cxx +++ b/src/Quantity/Quantity_Color.cxx @@ -46,15 +46,25 @@ namespace { const char* StringName; NCollection_Vec3 RgbValues; + NCollection_Vec4 sRgbVec4ub; Quantity_NameOfColor EnumName; - Quantity_StandardColor (Quantity_NameOfColor theName, const char* theStringName, const NCollection_Vec3& theVec3) - : StringName (theStringName), RgbValues (theVec3), EnumName (theName) {} + Quantity_StandardColor (Quantity_NameOfColor theName, + const char* theStringName, + const uint32_t theSRgbHex, + const NCollection_Vec3& theVec3) + : StringName (theStringName), + RgbValues (theVec3), + sRgbVec4ub (Standard_Byte((theSRgbHex & 0xff0000) >> 16), + Standard_Byte((theSRgbHex & 0x00ff00) >> 8), + Standard_Byte((theSRgbHex & 0x0000ff)), + 255), + EnumName (theName) {} }; } // Note that HTML/hex sRGB representation is ignored -#define RawColor(theName, theHex, theR, theG, theB) Quantity_StandardColor(Quantity_NOC_##theName, #theName, NCollection_Vec3(theR##f, theG##f, theB##f)) +#define RawColor(theName, theHex, theR, theG, theB) Quantity_StandardColor(Quantity_NOC_##theName, #theName, theHex, NCollection_Vec3(theR##f, theG##f, theB##f)) //! Name list of standard materials (defined within enumeration). static const Quantity_StandardColor THE_COLORS[] = @@ -298,16 +308,19 @@ void Quantity_Color::Delta (const Quantity_Color& theColor, // ======================================================================= Quantity_NameOfColor Quantity_Color::Name() const { - Standard_ShortReal aDist2 = 4.0f; + // it is better finding closest sRGB color (closest to human eye) instead of linear RGB color, + // as enumeration defines color names for human + const NCollection_Vec3 ansRgbVec (Convert_LinearRGB_To_sRGB (myRgb) * 255.0f + NCollection_Vec3 (0.5f)); + Standard_Integer aDist2 = IntegerLast(); Quantity_NameOfColor aResName = Quantity_NOC_BLACK; for (Standard_Integer aColIter = Quantity_NOC_BLACK; aColIter <= Quantity_NOC_WHITE; ++aColIter) { - const Standard_ShortReal aNewDist2 = (myRgb - THE_COLORS[aColIter].RgbValues).SquareModulus(); + const Standard_Integer aNewDist2 = (ansRgbVec - NCollection_Vec3 (THE_COLORS[aColIter].sRgbVec4ub.rgb())).SquareModulus(); if (aNewDist2 < aDist2) { aResName = Quantity_NameOfColor (aColIter); aDist2 = aNewDist2; - if (aNewDist2 == 0.0f) + if (aNewDist2 == 0) { break; } -- 2.39.5