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

Author Topic: [Solved]Colouring coloured tiles using sf:color  (Read 2568 times)

0 Members and 1 Guest are viewing this topic.

natchos

  • Jr. Member
  • **
  • Posts: 97
    • View Profile
    • Email
[Solved]Colouring coloured tiles using sf:color
« on: March 09, 2014, 08:25:16 pm »
In my current project I have tiles which represent a certain type of terrain. Each of these types have their own coloured tile image. This all works very well but I also want to be able to represent a water/water depth value on top of a tile if there is water.
I would like to colour the tiles as they are outputted by setting the tiles color with a sf::color value where an higher water depth would mean an stronger blue.

I am currently using the following code:
        if(Displayed_Tiles[x][y].is_Water())
                        {
                                Rendering_Tiles[x][y].setColor(sf::Color(125,125,255/(Displayed_Tiles[x][y].Get_Water_Saturation()),255/2));
                        }

Where Displayed_Tiles
  • [y].Get_Water_Saturation() is an number over 1 which goes down lower as the water depth increases.  This code is called each time I draw the tiles to the screens. After the tiles have been draw I "flash" all Rendering_Tiles which means I reset their colour.

The problem is that it doesn't work, in two ways. The tiles aren't colored blue, no matter the water depth and the tiles color does not change if their water depth is decreased/increased.
Anyone have any clue of why the code isn't working or what steps I should take to remedy it?(Or recommend me a better system to colour the tiles according to a water depth variable?).

Sorry if an topic similar to this has been posted. Searched around on the graphics and general boards but couldn't find anything relevant to this topic.
« Last Edit: March 11, 2014, 10:23:26 pm by natchos »

Lee R

  • Jr. Member
  • **
  • Posts: 86
    • View Profile
Re: Colouring coloured tiles using sf:color
« Reply #1 on: March 09, 2014, 11:32:13 pm »
Dividing 255 by any number less than 1 results in a value above of the range supported by sf::Uint8 (e.g. 255 / 0.5 = 510).

Another thing to consider is that the colour passed to sf::Sprite::setColor is applied to the texture by a multiplication. You should generally think of it as if it were named 'setColorModulation'. The implication is that in order to make the texture appear 'more blue', it actually has to be made 'less red and green'.

sf::Color waterTint(float saturation, sf::Uint8 value = 255)
{
    // Implicit hue of 240.
    return sf::Color(
        static_cast<sf::Uint8>(255.0f * (1.0f - saturation)),
        static_cast<sf::Uint8>(255.0f * (1.0f - saturation)),
        value,
        255
    );
}

int main()
{
    float saturation = 1.0f;
    sf::Color colour = waterTint(saturation);
    return 0;
}
 

This code assumes that 0 = minimum depth, 1 = maximum depth. The colour it generates should be applied directly to the tile being rendered, not to some overlay. It's completely untested but it looks right to me :P I came up with it by playing with the sliders on this site:

http://www.rapidtables.com/convert/color/hsv-to-rgb.htm

EDIT: Although there are blend modes for additive blending.
« Last Edit: March 09, 2014, 11:43:10 pm by Lee R »

natchos

  • Jr. Member
  • **
  • Posts: 97
    • View Profile
    • Email
Re: Colouring coloured tiles using sf:color
« Reply #2 on: March 11, 2014, 10:23:06 pm »
Thanks for the response. I managed to fix it with a somewhat similar setup
Rendering_Tiles[x][y].setColor(sf::Color((1/Displayed_Tiles[x][y].Get_Water_Depth())* 100,(1/Displayed_Tiles[x][y].Get_Water_Depth())* 100,Displayed_Tiles[x][y].Get_Water_Depth()*3,255));
As well as rebinding the background of that specific tile to a white square, as opposed to the the background of the tile type, which was what I used before.
Although the macro is a bit off it hits somewhat close to the mark and provides the ability to distinguish between water depth, which is what was important anyway.