ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
HSV.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 // ##########################################################################
11 // # #
12 // # CLOUDVIEWER PLUGIN: ColorimetricSegmenter #
13 // # #
14 // # This program is free software; you can redistribute it and/or modify #
15 // # it under the terms of the GNU General Public License as published by #
16 // # the Free Software Foundation; version 2 of the License. #
17 // # #
18 // # This program is distributed in the hope that it will be useful, #
19 // # but WITHOUT ANY WARRANTY; without even the implied warranty of #
20 // # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
21 // # GNU General Public License for more details. #
22 // # #
23 // # COPYRIGHT: Tri-Thien TRUONG, Ronan COLLIER, Mathieu LETRONE #
24 // # #
25 // ##########################################################################
26 
27 // qCC_db
28 #include <ecvColorTypes.h>
29 
31 struct Hsv {
33  Hsv() : h(0), s(0), v(0) {}
34 
36  Hsv(const ecvColor::Rgb& rgb) {
37  float r = rgb.r / 255.0f;
38  float g = rgb.g / 255.0f;
39  float b = rgb.b / 255.0f;
40  float maxComp = std::max(std::max(r, g), b);
41  float minComp = std::min(std::min(r, g), b);
42  float deltaComp = maxComp - minComp;
43 
44  float hue = 0;
45  if (deltaComp != 0) {
46  if (r == maxComp) {
47  hue = (g - b) / deltaComp;
48  } else {
49  if (g == maxComp) {
50  hue = 2 + (b - r) / deltaComp;
51  } else {
52  hue = 4 + (r - g) / deltaComp;
53  }
54  }
55  hue *= 60;
56  if (hue < 0) hue += 360;
57  }
58 
59  h = (static_cast<uint16_t>(hue) % 360);
60  s = static_cast<uint16_t>(maxComp == 0 ? 0
61  : (deltaComp / maxComp) * 100);
62  v = static_cast<uint16_t>(maxComp * 100);
63  }
64 
65  // HSV components
66  uint16_t h, s, v;
67 };
RGB color structure.
Definition: ecvColorTypes.h:49
int min(int a, int b)
Definition: cutil_math.h:53
int max(int a, int b)
Definition: cutil_math.h:48
HSV color.
Definition: HSV.h:31
Hsv(const ecvColor::Rgb &rgb)
Constrctor from a RGB color.
Definition: HSV.h:36
uint16_t v
Definition: HSV.h:66
uint16_t s
Definition: HSV.h:66
Hsv()
Default constrctor.
Definition: HSV.h:33
uint16_t h
Definition: HSV.h:66