Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How to create a proper sf::Color instance from std::string variable?  (Read 3172 times)

0 Members and 1 Guest are viewing this topic.

lafoniz

  • Newbie
  • *
  • Posts: 19
    • View Profile
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?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How to create a proper sf::Color instance from std::string variable?
« Reply #1 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:
  • 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.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

lafoniz

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: How to create a proper sf::Color instance from std::string variable?
« Reply #2 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.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How to create a proper sf::Color instance from std::string variable?
« Reply #3 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 will give you the results that you are hoping for.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: How to create a proper sf::Color instance from std::string variable?
« Reply #4 on: April 18, 2016, 09:10:22 am »
Otherwise you can use a stringstream and output it sf::Uint8
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/