SFML community forums

Help => Graphics => Topic started by: pm123 on July 26, 2020, 08:56:02 pm

Title: sf::Color multiplication not working
Post by: pm123 on July 26, 2020, 08:56:02 pm
i: 80
start: 255 0 255
dec1: 0 0 1
icolor: 80 80 80
mult: 0 0 0
answer: 255 0 255

    sf::Color icolor(i,i,i);
    cout << "i: " << i << endl;
    cout << "start:"<< " " << static_cast<int>(map.start.r) << " " << static_cast<int>(map.start.g) << " " << static_cast<int>(map.start.b) << endl;
    cout << "dec1:"<< " " << static_cast<int>(map.decrement1.r) << " " << static_cast<int>(map.decrement1.g) << " " << static_cast<int>(map.decrement1.b) << endl;
    cout << "icolor:"<< " " << static_cast<int>(icolor.r) << " " << static_cast<int>(icolor.g) << " " << static_cast<int>(icolor.b) << endl;
    sf::Color mult = icolor*map.decrement1;
    cout << "mult:"<< " " << static_cast<int>(mult.r) << " " << static_cast<int>(mult.g) << " " << static_cast<int>(mult.b) << endl;
    sf::Color answer = map.start - icolor*map.decrement1;
    cout << "answer:"<< " " << static_cast<int>(answer.r) << " " << static_cast<int>(answer.g) << " " << static_cast<int>(answer.b) << endl;
Title: Re: sf::Color multiplication not working
Post by: aSpookyMan on July 27, 2020, 06:09:03 am
Just multiply each element individually like so:
sf::Color c1(100, 100, 100);
sf::Color c2(2, 2, 2);
sf::Color c3(c1.r * c2.r, c1.g * c2.g, c1.b * c1.b);
 
But if you're going to do this often, I suggest you do some operator overloading.
Title: Re: sf::Color multiplication not working
Post by: Laurent on July 27, 2020, 08:23:23 am
Quote
Just multiply each element individually like so:
Not like so. You must then divide by 255 to bring it back to range [0 .. 255]. Or normalize components in [0 .. 1], if you need to do a lot of these multiplications.
Title: Re: sf::Color multiplication not working
Post by: pm123 on July 27, 2020, 01:13:57 pm
Yes, thats what I ended up doing. But the docs say it should work. Also had to watch out for subtracting the transparency value.
Title: Re: sf::Color multiplication not working
Post by: Laurent on July 27, 2020, 03:23:37 pm
Indeed, if this operator is already defined in SFML, it should work as expected. And it looks like it does, what do you think is wrong in your example?
Title: Re: sf::Color multiplication not working
Post by: pm123 on July 27, 2020, 04:32:44 pm
dec1: 0 0 1
icolor: 80 80 80
mult: 0 0 0

mult=dec1*icolor produces zeroes instead of 0,0,80
Title: Re: sf::Color multiplication not working
Post by: Laurent on July 27, 2020, 05:16:21 pm
(1 * 80) / 255 = 0, not 80.

You should get more familiar with how color multiplication works ;)
Title: Re: sf::Color multiplication not working
Post by: pm123 on July 27, 2020, 07:05:21 pm
Ah ok, I though taking modulo 256 would make something range 0->255 but I guess that's not the standard.
So when multiplying I actually want my decr to be 255 and not 1.
Title: Re: sf::Color multiplication not working
Post by: Laurent on July 27, 2020, 08:29:26 pm
So, multiply by 255.

To make color multiplication more intuitive, normalize the components: convert them from [0 .. 255] to [0 .. 1] (ie. divide by 255). Then you can multiply using standard maths conventions.