1 /* 2 Copyright (c) 2013-2021 Timur Gafarov 3 4 Boost Software License - Version 1.0 - August 17th, 2003 5 6 Permission is hereby granted, free of charge, to any person or organization 7 obtaining a copy of the software and accompanying documentation covered by 8 this license (the "Software") to use, reproduce, display, distribute, 9 execute, and transmit the Software, and to prepare derivative works of the 10 Software, and to permit third-parties to whom the Software is furnished to 11 do so, all subject to the following: 12 13 The copyright notices in the Software and this entire statement, including 14 the above license grant, this restriction and the following disclaimer, 15 must be included in all copies of the Software, in whole or in part, and 16 all derivative works of the Software, unless such copies or derivative 17 works are solely in the form of machine-executable object code generated by 18 a source language processor. 19 20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 DEALINGS IN THE SOFTWARE. 27 */ 28 29 /** 30 * HSV color space 31 * 32 * Copyright: Timur Gafarov 2013-2021. 33 * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 * Authors: Timur Gafarov 35 */ 36 module dlib.image.hsv; 37 38 import std.algorithm; 39 import dlib.math.utils; 40 import dlib.image.color; 41 42 /// HSV color channel 43 enum HSVAChannel 44 { 45 H = 0, 46 S = 1, 47 V = 2, 48 A = 3 49 } 50 51 /** 52 * HSV floating-point color representation 53 */ 54 struct ColorHSVAf 55 { 56 union 57 { 58 struct 59 { 60 float h; 61 float s; 62 float v; 63 float a; 64 } 65 float[4] arrayof; 66 } 67 68 this(float h, float s, float v, float a) 69 { 70 this.h = h; 71 this.s = s; 72 this.v = v; 73 this.a = a; 74 } 75 76 this(Color4f c) 77 { 78 a = c.a; 79 80 float cmin, cmax, delta; 81 cmin = min(c.r, c.g, c.b); 82 cmax = max(c.r, c.g, c.b); 83 84 v = cmax; 85 delta = cmax - cmin; 86 87 if (cmax > 0.0f) 88 s = delta / cmax; 89 else 90 { 91 // r = g = b = 0 92 // s = 0, h is undefined 93 s = 0.0f; 94 h = float.nan; 95 return; 96 } 97 98 if (c.r >= cmax) 99 h = (c.g - c.b) / delta; 100 else 101 { 102 if (c.g >= cmax) 103 h = 2.0f + (c.b - c.r) / delta; 104 else 105 h = 4.0f + (c.r - c.g) / delta; 106 } 107 108 h *= 60.0f; 109 110 if (h < 0.0f) 111 h += 360.0f; 112 } 113 114 Color4f rgba() 115 { 116 Color4f res; 117 118 res.a = a; 119 120 if (s <= 0.0f) 121 { 122 res.r = res.g = res.b = v; 123 return res; 124 } 125 126 float hh = h; 127 128 if (hh >= 360.0f) 129 hh = 0.0f; 130 hh /= 60.0f; 131 132 int i = cast(int)hh; 133 float ff = hh - i; 134 float p = v * (1.0f - s); 135 float q = v * (1.0f - (s * ff)); 136 float t = v * (1.0f - (s * (1.0f - ff))); 137 138 switch(i) 139 { 140 case 0: res.r = v; res.g = t; res.b = p; break; 141 case 1: res.r = q; res.g = v; res.b = p; break; 142 case 2: res.r = p; res.g = v; res.b = t; break; 143 case 3: res.r = p; res.g = q; res.b = v; break; 144 case 4: res.r = t; res.g = p; res.b = v; break; 145 case 5: 146 default: res.r = v; res.g = p; res.b = q; break; 147 } 148 149 return res; 150 } 151 152 void shiftHue(float degrees) 153 { 154 h += degrees; 155 while (h >= 360.0f) 156 h -= 360.0f; 157 while (h < 0.0f) 158 h += 360.0f; 159 } 160 161 void shiftSaturation(float val) 162 { 163 s += val; 164 s = clamp(s, 0.0f, 1.0f); 165 } 166 167 void scaleSaturation(float val) 168 { 169 s *= val; 170 s = clamp(s, 0.0f, 1.0f); 171 } 172 173 void shiftValue(float val) 174 { 175 v += val; 176 v = clamp(v, 0.0f, 1.0f); 177 } 178 179 void scaleValue(float val) 180 { 181 v *= val; 182 v = clamp(v, 0.0f, 1.0f); 183 } 184 185 bool hueInRange(float hue2, float tmin, float tmax) 186 { 187 if (h == hue2) 188 return true; 189 190 float h1 = hue2 + tmin; 191 while (h1 >= 360.0f) 192 h1 -= 360.0f; 193 while (h1 < 0.0f) 194 h1 += 360.0f; 195 196 float h2 = hue2 + tmax; 197 while (h2 >= 360.0f) 198 h2 -= 360.0f; 199 while (h1 < 0.0f) 200 h2 += 360.0f; 201 202 return (h1 > h2)? 203 (h > h1 || h < h2): 204 (h > h1 && h < h2); 205 } 206 } 207 208 /// RGBA from HSV[A] 209 Color4f hsv(float h, float s, float v, float a = 1.0f) 210 { 211 return ColorHSVAf(h, s, v, a).rgba; 212 }