SFML community forums

Help => General => Topic started by: lafoniz on April 17, 2016, 11:24:53 pm

Title: How to create a proper sf::Color instance from std::string variable?
Post by: lafoniz on April 17, 2016, 11:24:53 pm
Hello, I need to perform a task which will look similar to this:

std::string colorInfo = "255.255.255.255";
std::vector<std::string> RGBA = splitStringByCharacter(colorInfo, '.');
sf::Color color;
color.r ?= RGBA[0];
color.g ?= RGBA[1];
color.b ?= RGBA[2];
color.a ?= RGBA[3];

?=
This sign was written intentionally, because this is the thing I want to ask about. RGBA is a vector of std::string, however any member variable of color is of sf::Uint8 type. How should I proceed with that kind of conversion to make sure data won't be lost or corrupted?
Title: Re: How to create a proper sf::Color instance from std::string variable?
Post by: Hapax on April 17, 2016, 11:34:04 pm
The string needs to be parsed. C++ provides a number of ways to do this.

The tasks you'll need to achieve:
Title: Re: How to create a proper sf::Color instance from std::string variable?
Post by: lafoniz on April 18, 2016, 12:03:28 am
The string needs to be parsed. C++ provides a number of ways to do this.

The tasks you'll need to achieve:
  • split the string into sections (known as tokens) based on the . character,
  • convert those tokens (at this point, they are strings holding the values) into integers,
  • assign the converted tokens (now integer values) to the sf::Color component.

I'm very sorry, but I'm not asking for how to parse advice, I'm asking on advice how to transfer my data from std::string to sf::Uint8 where std::string variable is already carrying proper piece of string. It is described in my "code", hope it's clear and understandable.
Title: Re: How to create a proper sf::Color instance from std::string variable?
Post by: Hapax on April 18, 2016, 12:22:14 am
I was describing the whole process as an overview.

The problem you seem to be having is with task 2.
Again, there are a few ways to do this.
Maybe std::stoi (http://www.cplusplus.com/reference/string/stoi/) will give you the results that you are hoping for.
Title: Re: How to create a proper sf::Color instance from std::string variable?
Post by: eXpl0it3r on April 18, 2016, 09:10:22 am
Otherwise you can use a stringstream and output it sf::Uint8