Overview
Comment: | fix(color): Actually get hsl brightening working Reimplemented based on the algorithms in Computer Graphics: Principles and Practice. Only lightens based on luminosity right now, which makes beautifully saturated, but not pastel colors, so some tweaking might be recommended. |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | glowpelt/hsl |
Files: | files | file ages | folders |
SHA3-256: |
794d5b023ab5b43a9c04b00165c61e8e |
User & Date: | glowpelt on 2020-08-14 06:17:34 |
Other Links: | branch diff | manifest | tags |
Context
2020-08-16
| ||
02:05 | changes, merges, additions galore check-in: 82178e0a16 user: lexi tags: trunk | |
2020-08-14
| ||
06:17 | fix(color): Actually get hsl brightening working Reimplemented based on the algorithms in Computer Graphics: Principles and Practice. Only lightens based on luminosity right now, which makes beautifully saturated, but not pastel colors, so some tweaking might be recommended. Leaf check-in: 794d5b023a user: glowpelt tags: glowpelt/hsl | |
2020-08-13
| ||
05:11 | feat(color): Change color lightening to use HSL Change color lightening, including the readable utility, to use HSL. This is because the earlier implementation was broken and hacky, and using HSL is a way to implement this in a much more natural-feeling way (being closer to percieved lightness), especially for the current uses. Add a color:to_hsl() function to make this easier, as well as a from_hsl utility that is only in color, for now, but maybe should be exposed as an alternate constructor? check-in: 0a49ac4849 user: glowpelt tags: glowpelt/hsl | |
Changes
Modified lib/color.lua from [1e12e4a44b] to [ef48edf309].
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 .. 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 ... 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
}; construct = function(r,g,b,a) local clip = function(v) return math.max(0,math.min(255,v)) end; local from_hsl = function(hsl, alpha) -- convert from a hsl table and alpha value to a color local weird = function(n) -- Yeah... this is a really weird function, only named f local k = math.fmod(n + hsl.hue/(math.pi/6),12) local a = hsl.saturation * math.min(hsl.luminosity, 1 - hsl.luminosity) return hsl.luminosity * math.max(-1, math.min(k-3,9-k,1)) end return color(clip(weird(0)*255), clip(weird(8)*255), clip(weird(4)*255), alpha) end; local warp = function(f) return function(self, ...) local n = color(self) f(n, ...) return n end; ................................................................................ end; luminosity = function(self) return (self.red + self.green + self.blue) / 3 end; to_hsl = function(self) -- https://en.wikipedia.org/wiki/HSL_and_HSV -- www.niwa.nu/2013/05/math-behind-colorspace-conversions-rgb-hsl/ -- https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.211.6425 -- https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.413.9004 -- We need the rgb between 0 and 1 local rgb = { r = self.red/255, g = self.green/255, b = self.blue/255 } -- First, the hue. -- This version of the calculation can be up to 1.12deg off at the right few colors -- but is overall very close, easier to implement, and runs much faster -- TODO: consider memoizing something to do with this all? local alpha = 0.5 * (2*rgb.r - rgb.g - rgb.b) local beta = (math.sqrt(3)/2)*(rgb.g - rgb.b) local hue = math.atan2(beta, alpha) -- Next the luminosity/lightness. This one's easy enough local luminosity = 0.5*(math.max(rgb.r, rgb.g, rgb.b) + math.min(rgb.r, rgb.g, rgb.b)) -- Finally, saturation local saturation = 0 -- If luminosity isn't essentially 1 or 0 if math.abs(luminosity - 1) < 1e-18 or math.abs(luminosity) < 1e-18 then -- need the chroma local chroma = math.max(rgb.r,rgb.g,rgb.b) - math.min(rgb.r,rgb.g,rgb.b) saturation = chroma / (1 - math.abs(2*luminosity - 1)) end return { hue = hue, saturation = saturation, luminosity = luminosity } end; readable = function(self, target) target = target or 200 local hsl = self:to_hsl() hsl.luminosity = target return from_hsl(hsl, self.alpha) end; bg = function(self, text) return text .. minetest.get_background_escape_sequence(self:hex()) ................................................................................ end); brighten = function(self, fac) -- Use HSL to brighten -- To HSL local hsl = self:to_hsl() -- Do the calculation, clamp to 0-1 instead of the clamp fn hsl.luminosity = math.max(math.min(hsl.luminosity * fac, 0), 1) -- Turn back into RGB color return from_hsl(hsl, self.alpha) end; darken = warp(function(new, fac) -- TODO: is there any point to this being different than brighten? Probably especially not now. new.red = clip(new.red - (new.red * fac)) new.blue = clip(new.blue - (new.blue * fac)) |
| | | | | | > > > > > | < > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | | < < > | > | | | < < < | < < | > > | > > > | | | | | > > > > > > > > | > > > > > > > > > | | > > |
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 ... 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 ... 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
}; construct = function(r,g,b,a) local clip = function(v) return math.max(0,math.min(255,v)) end; local from_hsl = function(hsl, alpha) -- Based on the algorithm in Computer Graphics: Principles and Practice, by -- James D. Foley et. al., 2nd ed., p. 596 -- Degree version, though radian is more natural, I don't want to translate it yet local h = hsl.hue local s = hsl.saturation local l = hsl.luminosity local value = function(n1, n2, hue) if hue > 360 then hue = hue - 360 elseif hue < 0 then hue = hue + 360 end if hue < 60 then return n1 + (n2 - n1) * hue/60 elseif hue < 180 then return n2 elseif hue < 240 then return n1 + (n2 - n1) * (240 - hue)/60 else return n1 end end local m2 if l < 0.5 then m2 = l * (1 + s) else m2 = l + s - l * s end local m1 = 2 * l - m2 if s == 0 then -- Achromatic, there is no hue -- In book this errors if hue is not undefined, but we set hue to 0 in this case, not nil or something, so return color(l, l, l, alpha) else -- Chromatic case, so there is a hue return color( clip(value(m1, m2, h + 120)*255), clip(value(m1, m2, h)*255), clip(value(m1, m2, h - 120)*255), alpha ) end end; local warp = function(f) return function(self, ...) local n = color(self) f(n, ...) return n end; ................................................................................ end; luminosity = function(self) return (self.red + self.green + self.blue) / 3 end; to_hsl = function(self) -- Based on the algorithm in Computer Graphics: Principles and Practice, by -- James D. Foley et. al., 2nd ed., p. 595 -- We need the rgb between 0 and 1 local r = self.red/255 local g = self.green/255 local b = self.blue/255 local max = math.max(r, g, b) local min = math.min(r, g, b) local luminosity = (max + min)/2 local hue = 0 local saturation = 0 if max == min then -- Achromatic case, because r=g=b saturation = 0 hue = 0 -- Undefined, so just replace w/ 0 for usability else -- Chromatic case if luminosity <= 0.5 then saturation = (max - min)/(max + min) else saturation = (max - min)/(2 - max - min) end -- Next calculate the hue local delta = max - min if r == max then hue = (g - b)/delta elseif g == max then hue = 2 + (b - r)/delta else -- blue must be max, so no point in checking hue = 4 + (r - g)/delta end hue = hue * 60 -- degrees --hue = hue * (math.pi / 3) -- for hue in radians instead of degrees if hue < 0 then hue = hue + 2 * math.pi end end print("r"..self.red.."g"..self.green.."b"..self.blue.." is h"..hue.."s"..saturation.."l"..luminosity) local temp = from_hsl({hue=hue,saturation=saturation,luminosity=luminosity},self.alpha) print("back is r"..temp.red.."g"..temp.green.."b"..temp.blue) return { hue = hue, saturation = saturation, luminosity = luminosity } end; readable = function(self, target) target = target or 0.5 local hsl = self:to_hsl() hsl.luminosity = target return from_hsl(hsl, self.alpha) end; bg = function(self, text) return text .. minetest.get_background_escape_sequence(self:hex()) ................................................................................ end); brighten = function(self, fac) -- Use HSL to brighten -- To HSL local hsl = self:to_hsl() -- Do the calculation, clamp to 0-1 instead of the clamp fn hsl.luminosity = math.min(math.max(hsl.luminosity * fac, 0), 1) -- Turn back into RGB color local t = from_hsl(hsl, self.alpha) print("brighten is r"..t.red.."g"..t.green.."b"..t.blue) return from_hsl(hsl, self.alpha) end; darken = warp(function(new, fac) -- TODO: is there any point to this being different than brighten? Probably especially not now. new.red = clip(new.red - (new.red * fac)) new.blue = clip(new.blue - (new.blue * fac)) |