@@ -689,120 +689,18 @@ bool XMLColorToInt(const char* szColor, unsigned long& ulColor)
689689
690690bool XMLColorToInt (const char * szColor, unsigned char & ucRed, unsigned char & ucGreen, unsigned char & ucBlue, unsigned char & ucAlpha)
691691{
692- // If we're empty, let's just stop right away
693- if (!szColor || strlen (szColor) == 0 )
694- return false ;
695-
696- std::vector<SColorRGBA> vecColors;
697- unsigned char ucCount;
698-
699- if (ColorStringToRGBA (szColor, SColorRGBA (ucRed, ucGreen, ucBlue, ucAlpha), vecColors, ucCount))
700- {
701- ucRed = vecColors[0 ].R ;
702- ucGreen = vecColors[0 ].G ;
703- ucBlue = vecColors[0 ].B ;
704- ucAlpha = vecColors[0 ].A ;
705-
706- return true ;
707- }
708-
709- return false ;
710- }
711-
712- bool ColorStringToRGBA (const char * szColor, SColorRGBA defaultColor, std::vector<SColorRGBA>& vecColors, unsigned char & ucCount, bool bIgnoreAlpha)
713- {
714- std::stringstream ss (szColor);
715- SColorRGBA color = defaultColor;
716- bool bPreviousWasHex = false ;
717- unsigned int uiRGBAIndex = 0 ;
718- unsigned long ulColor;
719- unsigned char ucValue;
720- unsigned char ucLength = bIgnoreAlpha ? 3 : 4 ;
721-
722- while (ss.good ())
692+ // Convert it to an integer first
693+ unsigned long ulColor;
694+ if (!XMLColorToInt (szColor, ulColor))
723695 {
724- // Ambiguous value before a comma
725- SString strValue;
726- getline (ss, strValue, ' ,' );
727-
728- // Remove spaces
729- ReplaceOccurrencesInString (strValue, " " , " " );
730-
731- // Is the value looking like a hexadecimal?
732- if (strValue[0 ] == ' #' )
733- {
734- // Try converting it to an integer
735- if (XMLColorToInt (strValue.c_str (), ulColor))
736- {
737- // If a previous RGBA wasn't finished, let's finish it now
738- if (!bPreviousWasHex && uiRGBAIndex != 0 )
739- {
740- vecColors.push_back (color);
741- ucCount += ucLength - uiRGBAIndex;
742- color = defaultColor;
743- }
744-
745- color.R = static_cast <unsigned char >(ulColor);
746- color.G = static_cast <unsigned char >(ulColor >> 8 );
747- color.B = static_cast <unsigned char >(ulColor >> 16 );
748-
749- if (!bIgnoreAlpha)
750- color.A = static_cast <unsigned char >(ulColor >> 24 );
751-
752- bPreviousWasHex = true ;
753- ucCount += ucLength;
754- }
755- else
756- return false ;
757- }
758- // It looks like we have an empty value, let's skip the value but treat it as RGB
759- else if (strValue.empty ())
760- {
761- if (bPreviousWasHex || uiRGBAIndex % ucLength == 0 )
762- {
763- bPreviousWasHex = false ;
764- uiRGBAIndex = 0 ;
765- }
766-
767- uiRGBAIndex++;
768- ucCount++;
769-
770- if (uiRGBAIndex % ucLength != 0 && ss.good ())
771- continue ;
772- }
773- // It looks like a plain number so let's treat it as a RGBA value
774- else if (strValue.find_first_not_of (" 0123456789" ) == std::string::npos)
775- {
776- ucValue = atoi (strValue.c_str ());
777-
778- if (bPreviousWasHex || uiRGBAIndex % ucLength == 0 )
779- {
780- color.R = ucValue;
781- bPreviousWasHex = false ;
782- uiRGBAIndex = 0 ;
783- }
784- else if (uiRGBAIndex % ucLength == 1 )
785- color.G = ucValue;
786- else if (uiRGBAIndex % ucLength == 2 )
787- color.B = ucValue;
788- else if (uiRGBAIndex % ucLength == 3 )
789- color.A = ucValue;
790-
791- uiRGBAIndex++;
792- ucCount++;
793-
794- if (uiRGBAIndex % ucLength != 0 && ss.good ())
795- continue ;
796- }
797- else
798- return false ;
799-
800- // We have a color, so let's push it
801- vecColors.push_back (color);
802- color = defaultColor;
803- uiRGBAIndex = 0 ;
696+ return false ;
804697 }
805698
699+ // Convert it to red, green, blue and alpha
700+ ucRed = static_cast <unsigned char >(ulColor);
701+ ucGreen = static_cast <unsigned char >(ulColor >> 8 );
702+ ucBlue = static_cast <unsigned char >(ulColor >> 16 );
703+ ucAlpha = static_cast <unsigned char >(ulColor >> 24 );
806704 return true ;
807705}
808706
0 commit comments