sf::Color parseColor(const std::string theValue, const sf::Color theDefault)
{
	sf::Color anResult = theDefault;

	// Buscamos la primera coma
	size_t anComma1Offset = theValue.find_first_of(',');
	if (anComma1Offset != std::string::npos)
	{
		sf::Uint8 anRed = parseUint8(theValue.substr(0, anComma1Offset), theDefault.r);
		// Buscamos la siguiente coma
		size_t anComma2Offset = theValue.find_first_of(',', anComma1Offset + 1);
		if (anComma2Offset != std::string::npos)
		{
			sf::Uint8 anGreen = parseUint8(
				theValue.substr(anComma1Offset + 1, anComma2Offset), theDefault.g);
			// Buscamos la siguiente coma
			size_t anComma3Offset = theValue.find_first_of(',', anComma2Offset + 1);
			if (anComma3Offset != std::string::npos)
			{
				sf::Uint8 anBlue = parseUint8(
					theValue.substr(anComma2Offset + 1, anComma3Offset), theDefault.b);
				sf::Uint8 anAlpha = parseUint8(
					theValue.substr(anComma3Offset + 1), theDefault.a);

				// Tenemos los 4 valores parseados, devolvemos el color que hemos encontrado
				anResult.r = anRed;
				anResult.g = anGreen;
				anResult.b = anBlue;
				anResult.a = anAlpha;
			}
		}
	}

	// Devolvemos el resultado que hemos encontrado o el valor theDefault que se nos ha proporcionado
	return anResult;
}
示例#2
0
  sf::Color parseColor(const std::string theValue, const sf::Color theDefault)
  {
    sf::Color anResult = theDefault;

    // Try to find the first comma
    size_t anComma1Offset = theValue.find_first_of(',');
    if(anComma1Offset != std::string::npos)
    {
      sf::Uint8 anRed = parseUint8(theValue.substr(0,anComma1Offset), theDefault.r);
      // Try to find the next comma
      size_t anComma2Offset = theValue.find_first_of(',',anComma1Offset+1);
      if(anComma2Offset != std::string::npos)
      {
        sf::Uint8 anGreen = parseUint8(
          theValue.substr(anComma1Offset+1,anComma2Offset), theDefault.g);
        // Try to find the next comma
        size_t anComma3Offset = theValue.find_first_of(',',anComma2Offset+1);
        if(anComma3Offset != std::string::npos)
        {
          sf::Uint8 anBlue = parseUint8(
            theValue.substr(anComma2Offset+1,anComma3Offset), theDefault.b);
          sf::Uint8 anAlpha = parseUint8(
            theValue.substr(anComma3Offset+1), theDefault.a);

          // Now that all 4 values have been parsed, return the color found
          anResult.r = anRed;
          anResult.g = anGreen;
          anResult.b = anBlue;
          anResult.a = anAlpha;
        }
      }
    }

    // Return the result found or theDefault assigned above
    return anResult;
  }