From 5caab66d5867aa6f7fc838becbd4bf684790f66b Mon Sep 17 00:00:00 2001 From: zorg Date: Mon, 11 Dec 2023 03:32:14 +0100 Subject: [PATCH 1/6] Add to colour.lua Added RGB-HSV, HSV-RGB, HSV-HSL, HSL-HSV conversion functions. --- colour.lua | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/colour.lua b/colour.lua index db97ee7..6f661d1 100644 --- a/colour.lua +++ b/colour.lua @@ -173,7 +173,71 @@ function colour.rgb_to_hsl(r, g, b) return h / 6, s, l end ---todo: hsv +--convert hsv to rgb +--all components are 0-1, hue is fraction of a turn rather than degrees or radians +function colour.hsv_to_rgb(h, s, v) + --wedge slice + local w = (math.wrap(h, 0, 1) * 6) + --chroma + local c = v * s + --secondary + local x = c * (1 - math.abs(w % 2 - 1)) + --match value + local m = v - c + --per-wedge logic + local r, g, b = m, m, m + if w < 1 then + r = r + c + g = g + x + elseif w < 2 then + r = r + x + g = g + c + elseif w < 3 then + g = g + c + b = b + x + elseif w < 4 then + g = g + x + b = b + c + elseif w < 5 then + b = b + c + r = r + x + else + b = b + x + r = r + c + end + return r, g, b +end + +--convert rgb to hsv +function colour.rgb_to_hsv(r, g, b) + local max, min = math.max(r, g, b), math.min(r, g, b) + if max == min then return 0, 0, min end + local v, d = max, max - min + local s = (max == 0) and 0 or (d / max) + local h --depends on below + if max == r then + h = (g - b) / d + if g < b then h = h + 6 end + elseif max == g then + h = (b - r) / d + 2 + else + h = (r - g) / d + 4 + end + return h / 6, s, v +end + +--conversion between hsl and hsv +function colour.hsl_to_hsv(h, s, l) + local v = l + s * math.min(l, 1 - l) + local s = (v == 0) and 0 or (2 * (1 - l / v)) + return h, s, v +end + +function colour.hsv_to_hsl(h, s, v) + local l = v * (1 - s / 2) + local s = (l == 0 or l == 1) and 0 or ((v - l) / math.min(l, 1 - l)) + return h, s, l +end --oklab https://bottosson.github.io/posts/oklab/ function colour.oklab_to_rgb(l, a, b) From 77199cde0b6a3658d203788e8f75d61b91cf267d Mon Sep 17 00:00:00 2001 From: zorg Date: Mon, 11 Dec 2023 04:37:55 +0100 Subject: [PATCH 2/6] Update colour.lua *To Appease A Linter.* --- colour.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/colour.lua b/colour.lua index 6f661d1..9276daa 100644 --- a/colour.lua +++ b/colour.lua @@ -229,13 +229,13 @@ end --conversion between hsl and hsv function colour.hsl_to_hsv(h, s, l) local v = l + s * math.min(l, 1 - l) - local s = (v == 0) and 0 or (2 * (1 - l / v)) + s = (v == 0) and 0 or (2 * (1 - l / v)) return h, s, v end function colour.hsv_to_hsl(h, s, v) local l = v * (1 - s / 2) - local s = (l == 0 or l == 1) and 0 or ((v - l) / math.min(l, 1 - l)) + s = (l == 0 or l == 1) and 0 or ((v - l) / math.min(l, 1 - l)) return h, s, l end From f29882c2ee5bf5e92280621b11022a4a453de52d Mon Sep 17 00:00:00 2001 From: Bill Hilbert Date: Tue, 12 Dec 2023 21:53:36 -0500 Subject: [PATCH 3/6] add meta functions for mathmatical operators --- vec2.lua | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/vec2.lua b/vec2.lua index a0dd66e..191cf6a 100644 --- a/vec2.lua +++ b/vec2.lua @@ -453,6 +453,29 @@ function vec2:maxcomp() return math.max(self.x, self.y) end +-- meta functions for mathmatical operations +function vec2.__add(a, b) + return vec2(a.x + b.x, a.y + b.y) +end + +function vec2.__sub(a, b) + return vec2(a.x - b.x, a.y - b.y) +end + +function vec2.__mul(a, b) + if type(a) == "number" then + return vec2(a * b.x, a * b.y) + elseif type(b) == "number" then + return vec2(a.x * b, a.y * b) + else + return vec2(a.x * b.x, a.y * b.y) + end +end + +function vec2.__div(a, b) + return vec2(a.x / b, a.y / b) +end + -- mask out min component, with preference to keep x function vec2:major_inplace() if self.x > self.y then From 01e871c415d6d136585749645a9950d767c6c473 Mon Sep 17 00:00:00 2001 From: Bill Hilbert Date: Tue, 12 Dec 2023 22:22:13 -0500 Subject: [PATCH 4/6] state_machine: prev_state_name --- state_machine.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/state_machine.lua b/state_machine.lua index c5c28af..1b14844 100644 --- a/state_machine.lua +++ b/state_machine.lua @@ -33,6 +33,7 @@ local state_machine = class({ function state_machine:new(states, start_in_state) self.states = states or {} self.current_state_name = "" + self.prev_state_name = "" self.reset_state_name = start_in_state or "" self:reset() end @@ -151,6 +152,7 @@ end function state_machine:set_state(name, reset) if self.current_state_name ~= name or reset then self:_call("exit") + self.prev_state_name = self.current_state_name self.current_state_name = name self:_call_and_transition("enter", self) end From ce0205b42ad6e39745d27c5a26d9d83749d3b781 Mon Sep 17 00:00:00 2001 From: Bill Hilbert Date: Wed, 13 Dec 2023 10:16:24 -0500 Subject: [PATCH 5/6] rollback state_machine commit --- state_machine.lua | 2 -- 1 file changed, 2 deletions(-) diff --git a/state_machine.lua b/state_machine.lua index 1b14844..c5c28af 100644 --- a/state_machine.lua +++ b/state_machine.lua @@ -33,7 +33,6 @@ local state_machine = class({ function state_machine:new(states, start_in_state) self.states = states or {} self.current_state_name = "" - self.prev_state_name = "" self.reset_state_name = start_in_state or "" self:reset() end @@ -152,7 +151,6 @@ end function state_machine:set_state(name, reset) if self.current_state_name ~= name or reset then self:_call("exit") - self.prev_state_name = self.current_state_name self.current_state_name = name self:_call_and_transition("enter", self) end From 2d0abbd021a6190d4cb5571ba067e5e8bd1f076e Mon Sep 17 00:00:00 2001 From: Bill Hilbert Date: Wed, 13 Dec 2023 11:10:20 -0500 Subject: [PATCH 6/6] use existing methods --- vec2.lua | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/vec2.lua b/vec2.lua index 191cf6a..6ad17d3 100644 --- a/vec2.lua +++ b/vec2.lua @@ -455,25 +455,29 @@ end -- meta functions for mathmatical operations function vec2.__add(a, b) - return vec2(a.x + b.x, a.y + b.y) + return a:vector_add_inplace(b) end function vec2.__sub(a, b) - return vec2(a.x - b.x, a.y - b.y) + return a:vector_sub_inplace(b) end function vec2.__mul(a, b) - if type(a) == "number" then - return vec2(a * b.x, a * b.y) - elseif type(b) == "number" then - return vec2(a.x * b, a.y * b) - else - return vec2(a.x * b.x, a.y * b.y) - end + if type(a) == "number" then + return b:scalar_mul_inplace(a) + elseif type(b) == "number" then + return a:scalar_mul_inplace(b) + else + return a:vector_mul_inplace(b) + end end function vec2.__div(a, b) - return vec2(a.x / b, a.y / b) + if type(b) == "number" then + return a:scalar_div_inplace(b) + else + return a:vector_div_inplace(b) + end end -- mask out min component, with preference to keep x