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

Author Topic: Understanding sf::Color  (Read 2740 times)

0 Members and 1 Guest are viewing this topic.

Ettur

  • Newbie
  • *
  • Posts: 6
    • View Profile
Understanding sf::Color
« on: April 27, 2020, 02:16:54 pm »
Need a bit clarification on sf::Color,

I used getPixel from image to save pixel color:

sf::Color color = image.getPixel(280, 275);

now as I understand Color has properties r g b a to get Uint8 as corresponding values..
Then, when i wanted to see what those numbers area i tried cout:

cout << color.r << endl;
cout << color.g << endl;
cout << color.b << endl;

At this point im expecting to see numbers that correspond to R,G,B,A values of that color, however console window shows some weird symbols instead of numbers ???

then I tried to cast them as ints

cout << (int)color.r << endl;
cout << (int)color.g << endl;
cout << (int)color.b << endl;

now I saw numbers, are those numbers now actual rgb values of given pixel?

Then I also noticed sf::Color has toInteger() method.

So I tried:

cout << color.toInteger() << endl;

which does give me a sequence of numbers, HOWEVER, this sequence is vastly different from from individual values I see from (int) cast of r g b values.

pardon if it's something really simple & obvious but help me understand how can I get r g b a numeric values from sf::Color variable.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Understanding sf::Color
« Reply #1 on: April 27, 2020, 03:27:46 pm »
Yes, sf::UInt8 is unsigned char, so it is printed as characters, and you have to cast to integer to get proper numbers.

Color::toInteger() returns the components packed together in a 32-bits integer. You'd have to print as hexadecimal to see the groups of bytes clearly.

To get separate RGB values, stick to the first solution, don't use toInteger().
Laurent Gomila - SFML developer

Ettur

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Understanding sf::Color
« Reply #2 on: April 27, 2020, 05:27:57 pm »
Yes, sf::UInt8 is unsigned char, so it is printed as characters, and you have to cast to integer to get proper numbers.

Color::toInteger() returns the components packed together in a 32-bits integer. You'd have to print as hexadecimal to see the groups of bytes clearly.

To get separate RGB values, stick to the first solution, don't use toInteger().

Thank you!!!