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

Author Topic: change a color of the Sprite  (Read 17897 times)

0 Members and 1 Guest are viewing this topic.

MichX

  • Newbie
  • *
  • Posts: 9
    • View Profile
change a color of the Sprite
« on: February 07, 2014, 08:40:00 pm »
Hi, is it possible to change a color of the Sprite that based on texture loaded from file?
I tried
Code: [Select]
sprite.setColor(sf::Color(sf::Color::White));but it doesn't work ;/

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: change color of Sprite
« Reply #1 on: February 07, 2014, 08:42:40 pm »
It multiplies the colour you give it with the colours in the texture. White would mean that it allows all colours to pass through unmodified. Other colours tint the texture. Imagine holding different coloured glass/plastic over the texture image; white would represent clear.

EDIT: Also, you wouldn't be putting an sf::Color as a paramter to sf::Color.

Try this:
sprite.setColor(sf::Color::Red);
« Last Edit: February 07, 2014, 08:51:03 pm by Golden Eagle »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

MichX

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: change color of Sprite
« Reply #2 on: February 07, 2014, 09:04:21 pm »
It multiplies the colour you give it with the colours in the texture. White would mean that it allows all colours to pass through unmodified. Other colours tint the texture. Imagine holding different coloured glass/plastic over the texture image; white would represent clear.

EDIT: Also, you wouldn't be putting an sf::Color as a paramter to sf::Color.

Try this:
sprite.setColor(sf::Color::Red);
Thanks @Golden Eagle but it still doesn't work ;/
My code looks like below:
Code: [Select]
sf::Texture texture;
texture.loadFromFile("ball.png");
sf::Sprite sprite;
sprite.setTexture(texture);
sprite.setPosition(100, 25);
sprite.setColor(sf::Color::Red);
And Sprite changed a color but not for Red but almost black. I tried set meny of colors but it changed for black only.
I afraid it's not allowed to manipulate of color of a file ;/
« Last Edit: February 07, 2014, 09:08:15 pm by MichX »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: change a color of the Sprite
« Reply #3 on: February 07, 2014, 09:06:39 pm »
You need to be more specific about what doesn't work.

Other than that refer to the link below.

http://www.sfml-dev.org/tutorials/2.1/graphics-sprite.php#ok-can-i-have-my-sprite-now
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

MichX

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: change a color of the Sprite
« Reply #4 on: February 07, 2014, 09:50:15 pm »
Thank's to You both! It works! I did green image and its color make problems... Now, when i copy image of dark ball everythink seems good ;-)
best regards!

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: change color of Sprite
« Reply #5 on: February 07, 2014, 09:50:47 pm »
And Sprite changed a color but not for Red but almost black. I tried set meny of colors but it changed for black only.
I afraid it's not allowed to manipulate of color of a file ;/
I would assume that your original image is red if it's dark when tinted red and black when tinted with other colours?
I don't think I explained the multiply thing very well. I'll try again:
Each pixel's colour in the texture/image is represented by three values - red, green, blue. The colour that you specify in sprite.setColor() instructs SFML which colours to allow to be seen.

Assume that one of the sprite's pixels is yellow.

// sf::Color(<red value>, <green value>, <blue value>)

// yellow: allows the red and green components of the yellow to be shown
// therefore, yellow is displayed
sprite.setColor(sf::Color(255, 255, 0));

// red: allows the red component of the yellow to be shown
// therefore, red is displayed
sprite.setColor(sf::Color(255, 0, 0));

// green: allows the green component of the yellow to be shown
// therefore, green is displayed
sprite.setColor(sf::Color(0, 255, 0));

// white: allows the red, green, and blue components of the yellow to be shown
// therefore, yellow is displayed (there is no blue component in yellow)
sprite.setColor(sf::Color(255, 255, 255));

// magenta: allows the red and blue component of the yellow to be shown
// therefore, red is displayed (there is no blue component in yellow)
sprite.setColor(sf::Color(255, 255, 0));

// blue: allows the blue component of the yellow to be shown
// therefore, black is displayed (there is no blue component in yellow)
sprite.setColor(sf::Color(0, 0, 255));

// black: does not allow any components to be shown
// therefore, black is displayed
sprite.setColor(sf::Color(0, 0, 0));
 

EDIT: shortened lines in code
« Last Edit: February 07, 2014, 09:55:31 pm by Golden Eagle »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything