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

Author Topic: Problem with sf::Color  (Read 4601 times)

0 Members and 1 Guest are viewing this topic.

declan

  • Newbie
  • *
  • Posts: 34
    • View Profile
Problem with sf::Color
« on: August 09, 2010, 05:09:15 pm »
Hey guys. I have a black background to my app. At some point, I draw a rainbow strip at the bottom. So my code works, but it's giving me the obnoxious error of:

Code: [Select]
flowfield.cpp:476: warning: large integer implicitly truncated to unsigned type

I suspect (in fact, know) that it's because I'm doing this:

Code: [Select]
sf::Color(*gridptr,*(gridptr+1),*(gridptr+2),500)

While the fourth argument should only be going from 0-255, according to the documentation for this. But when I set it to 255, it's too dark! I can barely see it. What can I do?

Thanks!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Problem with sf::Color
« Reply #1 on: August 09, 2010, 05:52:37 pm »
If the documentation says it's between 0 and 255, trust it ;)

It's stored in a 8-bits unsigned integer, so you can technically not set a value outside this range.

If you want it to be more transparent, use a smaller value than 255.
Laurent Gomila - SFML developer

declan

  • Newbie
  • *
  • Posts: 34
    • View Profile
Problem with sf::Color
« Reply #2 on: August 09, 2010, 07:41:15 pm »
Haha, I believe it...but then why is it giving me a much brighter color when I set it to 500? I want it to be brighter. Let me get a couple screenshots.

declan

  • Newbie
  • *
  • Posts: 34
    • View Profile
Problem with sf::Color
« Reply #3 on: August 09, 2010, 07:46:38 pm »
Goddamnit! I took a few screenshots and in the process, found the solution, but I'm still confused. For 500, it's really bright, for 400, it's dimmer, 300, still dimmer...then 200, brighter, so I tried 255, and it's as bright as I want! But I swear I tried 255 before. GRRRR. All warnings are gone, anyway. Thanks!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Problem with sf::Color
« Reply #4 on: August 09, 2010, 08:40:33 pm »
Unsigned integer types "warp" on overflow in C++, so your 500 was internally translated to 244. But do you understand what this component means, and what value to use to get what you want? It's like you're trying random values until you get what you want ;)
Laurent Gomila - SFML developer

declan

  • Newbie
  • *
  • Posts: 34
    • View Profile
Problem with sf::Color
« Reply #5 on: August 09, 2010, 08:54:17 pm »
Haha, well I assumed that 0 would be completely dim, 255 would be really bright. I'm not exactly sure why I was using 500 to begin with... I only noticed because I upgraded gcc and this newer version seems to care about this, while the older one didn't.

Is there another function for defining colors? Because in my program, using HSL is a whole lot more convenient than RGB. I made a little function to switch it, but I'm just curious.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Problem with sf::Color
« Reply #6 on: August 09, 2010, 09:02:24 pm »
No, colors in SFML are always RGBA. So you must use your conversion function ;)
Laurent Gomila - SFML developer

declan

  • Newbie
  • *
  • Posts: 34
    • View Profile
Problem with sf::Color
« Reply #7 on: August 09, 2010, 09:16:11 pm »
Ok. I'll post it here in case anyone ever wants it, not that it's very complicated to make:

Code: [Select]

//This function converts HSL (Hue, saturation, lightness) to RGB.
//H goes from 0 to 360.
//S goes from 0 to 1.
//L goes from 0 to 1.
//The RGB values in the returned pointer go from 0 to 255.
//The returned pointer is 3 doubles long, *(ptr)=R, *(ptr+1)=G, *(ptr+2)=B.

double* HSLtoRGB(double H, double S, double L){
double * HSLptr;
try{
HSLptr = new double[3];
}
catch(bad_alloc&){
cout << endl << "Error in HSLtoRGB when allocating memory." << endl;
}
double C = 2*L*S;
double Hprime = H/60.0;
double a = Hprime - 2*floor(Hprime/2.0);
double X = C*(1-fabs(a-1));
double C_0 = C*255;
double X_0 = X*255;

if(Hprime>=0 && Hprime<1){
*(HSLptr+0) = C_0;
*(HSLptr+1) = X_0;
*(HSLptr+2) = 0;
         }
         if(Hprime>=1 && Hprime<2){
*(HSLptr+0) = X_0;
*(HSLptr+1) = C_0;
*(HSLptr+2) = 0;
         }      
         if(Hprime>=2 && Hprime<3){
*(HSLptr+0) = 0;
*(HSLptr+1) = C_0;
*(HSLptr+2) = X_0;
         }      
         if(Hprime>=3 && Hprime<4){
*(HSLptr+0) = 0;
*(HSLptr+1) = X_0;
*(HSLptr+2) = C_0;
         }      
         if(Hprime>=4 && Hprime<5){
*(HSLptr+0) = X_0;
*(HSLptr+1) = 0;
*(HSLptr+2) = C_0;
         }      
         if(Hprime>=5 && Hprime<6){
*(HSLptr+0) = C_0;
*(HSLptr+1) = 0;
*(HSLptr+2) = X_0;
         }    

return HSLptr;


} //HSLtoRGB


Hmm, the tags made my indentation a little weird.

 

anything