Skip to content

Commit 7be64fa

Browse files
committed
cleanup codes of core.color
1 parent 4529b31 commit 7be64fa

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

script/core/color.lua

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,22 @@ end
1919
local function textToColor(colorText)
2020
return {
2121
alpha = tonumber(colorText:sub(1, 2), 16) / 255,
22-
red = tonumber(colorText:sub(3, 4), 16) / 255,
22+
red = tonumber(colorText:sub(3, 4), 16) / 255,
2323
green = tonumber(colorText:sub(5, 6), 16) / 255,
24-
blue = tonumber(colorText:sub(7, 8), 16) / 255,
24+
blue = tonumber(colorText:sub(7, 8), 16) / 255,
2525
}
2626
end
2727

2828

2929
---@param color Color
3030
---@return string
3131
local function colorToText(color)
32-
return (''
33-
.. string.format('%02x', math.tointeger(color.alpha * 255))
34-
.. string.format('%02x', math.tointeger(color.red * 255))
35-
.. string.format('%02x', math.tointeger(color.green * 255))
36-
.. string.format('%02x', math.tointeger(color.blue * 255))):upper()
32+
return string.format('%02X%02X%02X%02X'
33+
, math.floor(color.alpha * 255)
34+
, math.floor(color.red * 255)
35+
, math.floor(color.green * 255)
36+
, math.floor(color.blue * 255)
37+
)
3738
end
3839

3940
---@class Color
@@ -48,29 +49,30 @@ end
4849
---@field finish integer
4950

5051
---@async
51-
function colors(uri)
52+
local function colors(uri)
5253
local state = files.getState(uri)
5354
local text = files.getText(uri)
5455
if not state or not text then
5556
return nil
5657
end
5758
---@type ColorValue[]
58-
local colors = {}
59+
local colorValues = {}
5960

6061
guide.eachSource(state.ast, function (source) ---@async
6162
if source.type == 'string' and isColor(source) then
6263
---@type string
6364
local colorText = source[1]
64-
65-
colors[#colors+1] = {
66-
start = source.start + 1,
65+
66+
colorValues[#colorValues+1] = {
67+
start = source.start + 1,
6768
finish = source.finish - 1,
68-
color = textToColor(colorText)
69+
color = textToColor(colorText)
6970
}
7071
end
7172
end)
72-
return colors
73+
return colorValues
7374
end
75+
7476
return {
7577
colors = colors,
7678
colorToText = colorToText

0 commit comments

Comments
 (0)