Welcome,
Guest
. Please
login
or
register
. Did you miss your
activation email?
French forum
Home
Help
Search
Login
Register
SFML community forums
»
Help
»
Graphics
»
sf::Color multiplication not working
Print
Pages: [
1
]
Author
Topic: sf::Color multiplication not working (Read 2781 times)
0 Members and 1 Guest are viewing this topic.
pm123
Newbie
Posts: 4
sf::Color multiplication not working
«
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;
Logged
aSpookyMan
Newbie
Posts: 7
Re: sf::Color multiplication not working
«
Reply #1 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.
Logged
Laurent
Administrator
Hero Member
Posts: 32498
Re: sf::Color multiplication not working
«
Reply #2 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.
Logged
Laurent Gomila - SFML developer
pm123
Newbie
Posts: 4
Re: sf::Color multiplication not working
«
Reply #3 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.
Logged
Laurent
Administrator
Hero Member
Posts: 32498
Re: sf::Color multiplication not working
«
Reply #4 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?
Logged
Laurent Gomila - SFML developer
pm123
Newbie
Posts: 4
Re: sf::Color multiplication not working
«
Reply #5 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
Logged
Laurent
Administrator
Hero Member
Posts: 32498
Re: sf::Color multiplication not working
«
Reply #6 on:
July 27, 2020, 05:16:21 pm »
(1 * 80) / 255 = 0, not 80.
You should get more familiar with how color multiplication works
Logged
Laurent Gomila - SFML developer
pm123
Newbie
Posts: 4
Re: sf::Color multiplication not working
«
Reply #7 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.
Logged
Laurent
Administrator
Hero Member
Posts: 32498
Re: sf::Color multiplication not working
«
Reply #8 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.
Logged
Laurent Gomila - SFML developer
Print
Pages: [
1
]
SFML community forums
»
Help
»
Graphics
»
sf::Color multiplication not working