diff --git a/projects/core/src/main/resources/data/computercraft/lua/rom/apis/colors.lua b/projects/core/src/main/resources/data/computercraft/lua/rom/apis/colors.lua index b98ff8d75..f013a29e1 100644 --- a/projects/core/src/main/resources/data/computercraft/lua/rom/apis/colors.lua +++ b/projects/core/src/main/resources/data/computercraft/lua/rom/apis/colors.lua @@ -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 diff --git a/projects/core/src/main/resources/data/computercraft/lua/rom/apis/window.lua b/projects/core/src/main/resources/data/computercraft/lua/rom/apis/window.lua index 7be7aebaf..5b5678d63 100644 --- a/projects/core/src/main/resources/data/computercraft/lua/rom/apis/window.lua +++ b/projects/core/src/main/resources/data/computercraft/lua/rom/apis/window.lua @@ -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 diff --git a/projects/core/src/test/resources/test-rom/spec/apis/colors_spec.lua b/projects/core/src/test/resources/test-rom/spec/apis/colors_spec.lua index 46c77dcf8..8f50b0d56 100644 --- a/projects/core/src/test/resources/test-rom/spec/apis/colors_spec.lua +++ b/projects/core/src/test/resources/test-rom/spec/apis/colors_spec.lua @@ -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) diff --git a/projects/core/src/test/resources/test-rom/spec/apis/window_spec.lua b/projects/core/src/test/resources/test-rom/spec/apis/window_spec.lua index 89e4e2993..4ad3d797c 100644 --- a/projects/core/src/test/resources/test-rom/spec/apis/window_spec.lua +++ b/projects/core/src/test/resources/test-rom/spec/apis/window_spec.lua @@ -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()