| 303 | //////////////////////////////////////////////////////////////////////////////// |
| 304 | |
| 305 | void |
| 306 | pcl::ihs::InputDataProcessing::RGBToHSV(const unsigned char r, |
| 307 | const unsigned char g, |
| 308 | const unsigned char b, |
| 309 | float& h, |
| 310 | float& s, |
| 311 | float& v) const |
| 312 | { |
| 313 | const unsigned char max = std::max(r, std::max(g, b)); |
| 314 | const unsigned char min = std::min(r, std::min(g, b)); |
| 315 | |
| 316 | v = static_cast<float>(max) / 255.f; |
| 317 | |
| 318 | if (max == 0) // division by zero |
| 319 | { |
| 320 | s = 0.f; |
| 321 | h = 0.f; // h = -1.f; |
| 322 | return; |
| 323 | } |
| 324 | |
| 325 | const float diff = static_cast<float>(max - min); |
| 326 | s = diff / static_cast<float>(max); |
| 327 | |
| 328 | if (min == max) // diff == 0 -> division by zero |
| 329 | { |
| 330 | h = 0; |
| 331 | return; |
| 332 | } |
| 333 | |
| 334 | if (max == r) |
| 335 | h = 60.f * (static_cast<float>(g - b) / diff); |
| 336 | else if (max == g) |
| 337 | h = 60.f * (2.f + static_cast<float>(b - r) / diff); |
| 338 | else |
| 339 | h = 60.f * (4.f + static_cast<float>(r - g) / diff); // max == b |
| 340 | |
| 341 | if (h < 0.f) |
| 342 | h += 360.f; |
| 343 | } |
| 344 | |
| 345 | //////////////////////////////////////////////////////////////////////////////// |