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

Author Topic: Color::makeColorHSV(int hue, float sat, float val)  (Read 6902 times)

0 Members and 1 Guest are viewing this topic.

Tenry

  • Full Member
  • ***
  • Posts: 120
  • Experienced Programmer
    • View Profile
    • Simon-Burchert.com
Color::makeColorHSV(int hue, float sat, float val)
« on: March 17, 2012, 02:04:21 pm »
What about an hsv color function? It could be very useful to generate random colors with a certain saturation and value, or fading from one color (hue) to another.

// hue: 0-360°; sat: 0.f-1.f; val: 0.f-1.f
sf::Color hsv(int hue, float sat, float val)
{
  hue %= 360;
  while(hue<0) hue += 360;

  if(sat<0.f) sat = 0.f;
  if(sat>1.f) sat = 1.f;

  if(val<0.f) val = 0.f;
  if(val>1.f) val = 1.f;

  int h = hue/60;
  float f = float(hue)/60-h;
  float p = val*(1.f-sat);
  float q = val*(1.f-sat*f);
  float t = val*(1.f-sat*(1-f));

  switch(h)
  {
    default:
    case 0:
    case 6: return sf::Color(val*255, t*255, p*255);
    case 1: return sf::Color(q*255, val*255, p*255);
    case 2: return sf::Color(p*255, val*255, t*255);
    case 3: return sf::Color(p*255, q*255, val*255);
    case 4: return sf::Color(t*255, p*255, val*255);
    case 5: return sf::Color(val*255, p*255, q*255);
  }
}
« Last Edit: March 26, 2012, 08:07:35 pm by Shy Guy »
Please note that my previous display name was "Shy Guy".

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Color::makeColorHSV(int hue, float sat, float val)
« Reply #1 on: March 17, 2012, 03:06:33 pm »
I think it has already been discussed.

I have no strong argument against it, it's just that I don't want to add such features now ;)
Laurent Gomila - SFML developer

Tenry

  • Full Member
  • ***
  • Posts: 120
  • Experienced Programmer
    • View Profile
    • Simon-Burchert.com
Color::makeColorHSV(int hue, float sat, float val)
« Reply #2 on: March 17, 2012, 04:37:31 pm »
Quote from: "Laurent"
I think it has already been discussed.

I have no strong argument against it, it's just that I don't want to add such features now ;)

Hm, I have searched for "hsv" before without any results :/

Such a method could be easily implemented, there's already the code above. And I would do it as a static method returning an sf::Color.

But I hope you'll add such feature soon :)
Please note that my previous display name was "Shy Guy".

Haikarainen

  • Guest
Color::makeColorHSV(int hue, float sat, float val)
« Reply #3 on: March 17, 2012, 08:44:18 pm »
Well SFML is a multimedia library, HSV support should really, really be implemented if you think about it. I like how Visual Studio stuff have this kinda helpers with it. I see huge potential in it and it has my vote!

Lupinius

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
Color::makeColorHSV(int hue, float sat, float val)
« Reply #4 on: March 18, 2012, 01:29:54 am »
HSV is crucial when you try do to any random color creation. My solutions always end up being messy and I'd like a native way of doing this as well.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Color::makeColorHSV(int hue, float sat, float val)
« Reply #5 on: March 18, 2012, 09:08:59 am »
Well, this is a minor issue, given that anyone can add this very simple function to his own code.
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Color::makeColorHSV(int hue, float sat, float val)
« Reply #6 on: March 18, 2012, 09:41:04 am »
Code: [Select]
// hue: 0-360°; sat: 0.f-1.f; val: 0.f-1.f You could also check those preconditions with assert. Using if, you are only wasting time, because in most cases, the passed values are correct anyway. Otherwise, the caller can still use modulo, std::min() or std::max() and pass the right values directly. The other advantage is that assert is able to catch logic errors instead of "justifying" them.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: