From 722b870f98ddc61d4a1a2ac27eae8662a021e58e Mon Sep 17 00:00:00 2001 From: Erlend <49862976+Erb3@users.noreply.github.com> Date: Wed, 17 May 2023 14:02:21 +0200 Subject: [PATCH 1/6] fix #1367: Add `colors.fromBlit` --- .../data/computercraft/lua/rom/apis/colors.lua | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) 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 cff3b0c0d..99621f933 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 @@ -363,3 +363,20 @@ function toBlit(color) return color_hex_lookup[color] or string.format("%x", math.floor(math.log(color) / math.log(2))) end + +--- Converts the given paint/blit hex character (0-9a-f) to a color. +-- +-- This is equivalent to converting the hex character to decimal and then 2 ^ decimal +-- +-- @tparam string hex The paint/blit hex character to convert +-- @treturn number The color + +function fromBlit(hex) + expect(1, hex, "string") + + if hex:find("[^0-9a-f]") or hex == "" then + return + end + + return math.floor(2 ^ tonumber(hex, 16)) +end From 090d17c52871ba183df91aa3fbec6aff88a74f8c Mon Sep 17 00:00:00 2001 From: Erlend <49862976+Erb3@users.noreply.github.com> Date: Wed, 17 May 2023 14:43:08 +0200 Subject: [PATCH 2/6] Add tests for colors.fromBlit --- .../resources/test-rom/spec/apis/colors_spec.lua | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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 d22865661..abbcb37ef 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 @@ -93,4 +93,18 @@ describe("The colors library", function() expect(colors.toBlit(16385)):eq("e") end) end) + + describe("colors.fromBlit", function() + it("validates arguments", function() + expect.error(colors.fromBlit, nil):eq("bad argument #1 (string expected, got nil)") + expect(colors.fromBlit("")):eq(nil) + expect(colors.fromBlit("not hex")):eq(nil) + end) + + it("converts all colors", function() + for i = 0, 15 do + expect(colors.fromBlit(string.format("%x", i))):eq(2 ^ i) + end + end) + end) end) From 98ee37719d432d542797634dc7eb2873f64055ce Mon Sep 17 00:00:00 2001 From: Erlend <49862976+Erb3@users.noreply.github.com> Date: Wed, 17 May 2023 20:09:59 +0200 Subject: [PATCH 3/6] Improve documentation for colors.fromBlit and colors.toBlit --- .../data/computercraft/lua/rom/apis/colors.lua | 13 +++++++++++++ 1 file changed, 13 insertions(+) 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 99621f933..388b087e1 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 @@ -357,6 +357,12 @@ end -- -- @tparam number color The color to convert. -- @treturn string The blit hex code of the color. +-- @usage +-- ```lua +-- colors.toBlit(colors.red) +-- -- => "c" +-- ``` +-- @see colors.fromBlit -- @since 1.94.0 function toBlit(color) expect(1, color, "number") @@ -370,6 +376,13 @@ end -- -- @tparam string hex The paint/blit hex character to convert -- @treturn number The color +-- @usage +-- ```lua +-- colors.fromBlit("e") +-- -- => 16384 +-- ``` +-- @see colors.toBlit +-- @since 1.105.0 function fromBlit(hex) expect(1, hex, "string") From 25ab7d0dcb500e44cdbacbc73887b0718030d2a3 Mon Sep 17 00:00:00 2001 From: Erlend <49862976+Erb3@users.noreply.github.com> Date: Wed, 17 May 2023 20:16:47 +0200 Subject: [PATCH 4/6] Improve test for colors.fromBlit to show inverse --- .../core/src/test/resources/test-rom/spec/apis/colors_spec.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 abbcb37ef..e6ff9f2d6 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 @@ -103,7 +103,7 @@ describe("The colors library", function() it("converts all colors", function() for i = 0, 15 do - expect(colors.fromBlit(string.format("%x", i))):eq(2 ^ i) + expect(colors.fromBlit(colors.toBlit(2 ^ i))):eq(2 ^ i) end end) end) From 952674a161a30f09743f7c080cc6160dbd141d39 Mon Sep 17 00:00:00 2001 From: Erlend <49862976+Erb3@users.noreply.github.com> Date: Wed, 17 May 2023 20:20:53 +0200 Subject: [PATCH 5/6] Improve argument validation in colors.fromBlit --- .../data/computercraft/lua/rom/apis/colors.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) 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 388b087e1..f582ff10e 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 @@ -372,7 +372,7 @@ end --- Converts the given paint/blit hex character (0-9a-f) to a color. -- --- This is equivalent to converting the hex character to decimal and then 2 ^ decimal +-- This is equivalent to converting the hex character to a number and then 2 ^ decimal -- -- @tparam string hex The paint/blit hex character to convert -- @treturn number The color @@ -387,9 +387,9 @@ end function fromBlit(hex) expect(1, hex, "string") - if hex:find("[^0-9a-f]") or hex == "" then - return - end + if #hex ~= 1 then return nil end + local value = tonumber(hex, 16) + if not value then return nil end - return math.floor(2 ^ tonumber(hex, 16)) + return 2 ^ value end From 90177c9f8bc2035d82b08158ea3e7696f7012c45 Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Wed, 17 May 2023 22:21:38 +0100 Subject: [PATCH 6/6] Convert to block comments And remove empty line between block and definition --- .../computercraft/lua/rom/apis/colors.lua | 53 ++++++++++--------- 1 file changed, 27 insertions(+), 26 deletions(-) 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 f582ff10e..5494cc1ac 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 @@ -351,39 +351,40 @@ for i = 0, 15 do color_hex_lookup[2 ^ i] = string.format("%x", i) end ---- Converts the given color to a paint/blit hex character (0-9a-f). --- --- This is equivalent to converting floor(log_2(color)) to hexadecimal. --- --- @tparam number color The color to convert. --- @treturn string The blit hex code of the color. --- @usage --- ```lua --- colors.toBlit(colors.red) --- -- => "c" --- ``` --- @see colors.fromBlit --- @since 1.94.0 +--[[- Converts the given color to a paint/blit hex character (0-9a-f). + +This is equivalent to converting floor(log_2(color)) to hexadecimal. + +@tparam number color The color to convert. +@treturn string The blit hex code of the color. +@usage +```lua +colors.toBlit(colors.red) +-- => "c" +``` +@see colors.fromBlit +@since 1.94.0 +]] function toBlit(color) expect(1, color, "number") return color_hex_lookup[color] or string.format("%x", math.floor(math.log(color) / math.log(2))) end ---- Converts the given paint/blit hex character (0-9a-f) to a color. --- --- This is equivalent to converting the hex character to a number and then 2 ^ decimal --- --- @tparam string hex The paint/blit hex character to convert --- @treturn number The color --- @usage --- ```lua --- colors.fromBlit("e") --- -- => 16384 --- ``` --- @see colors.toBlit --- @since 1.105.0 +--[[- Converts the given paint/blit hex character (0-9a-f) to a color. +This is equivalent to converting the hex character to a number and then 2 ^ decimal + +@tparam string hex The paint/blit hex character to convert +@treturn number The color +@usage +```lua +colors.fromBlit("e") +-- => 16384 +``` +@see colors.toBlit +@since 1.105.0 +]] function fromBlit(hex) expect(1, hex, "string")