1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-07-08 21:12:53 +00:00

Fix 0 being treated as a valid colour (#2211)

This commit is contained in:
SpartanSoftware 2025-05-31 02:46:03 -05:00 committed by GitHub
parent ee3b1343b5
commit 876fd8ddb8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 13 additions and 2 deletions

View File

@ -371,7 +371,7 @@ function toBlit(color)
local hex = color_hex_lookup[color]
if hex then return hex end
if color < 0 or color > 0xffff then error("Colour out of range", 2) end
if color < 1 or color > 0xffff then error("Colour out of range", 2) end
return string.format("%x", math.floor(math.log(color, 2)))
end

View File

@ -65,7 +65,7 @@ local function parse_color(color)
return expect(1, color, "number")
end
if color < 0 or color > 0xffff then error("Colour out of range", 3) end
if color < 1 or color > 0xffff then error("Colour out of range", 3) end
return 2 ^ math.floor(math.log(color, 2))
end

View File

@ -94,6 +94,7 @@ describe("The colors library", function()
end)
it("errors on out-of-range colours", function()
expect.error(colors.toBlit, 0):eq("Colour out of range")
expect.error(colors.toBlit, -120):eq("Colour out of range")
expect.error(colors.toBlit, 0x10000):eq("Colour out of range")
end)

View File

@ -59,6 +59,16 @@ describe("The window library", function()
expect.error(w.setTextColour, nil):eq("bad argument #1 (number expected, got nil)")
expect.error(w.setTextColour, -5):eq("Colour out of range")
expect.error(w.setTextColour, 0):eq("Colour out of range")
expect.error(w.setTextColour, 0x10000):eq("Colour out of range")
end)
it("accepts valid colours", function()
local w = mk()
for i = 0, 15 do
w.setBackgroundColour(2 ^ i)
expect(w.getBackgroundColour()):eq(2 ^ i)
end
end)
it("supports invalid combined colours", function()