summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2022-07-26 00:00:36 +0800
committer最萌小汐 <sumneko@hotmail.com>2022-07-26 00:00:36 +0800
commit7be64fa405a547b380fea6a80891a6debc64ca3b (patch)
treef5cad1170ca08ccacb05808a70823e72a86829e3
parent4529b3118d10a82ba2e0cfa201bc380067c18212 (diff)
downloadlua-language-server-7be64fa405a547b380fea6a80891a6debc64ca3b.zip
cleanup codes of `core.color`
-rw-r--r--script/core/color.lua30
1 files changed, 16 insertions, 14 deletions
diff --git a/script/core/color.lua b/script/core/color.lua
index f5c792a3..2cbcce11 100644
--- a/script/core/color.lua
+++ b/script/core/color.lua
@@ -19,9 +19,9 @@ end
local function textToColor(colorText)
return {
alpha = tonumber(colorText:sub(1, 2), 16) / 255,
- red = tonumber(colorText:sub(3, 4), 16) / 255,
+ red = tonumber(colorText:sub(3, 4), 16) / 255,
green = tonumber(colorText:sub(5, 6), 16) / 255,
- blue = tonumber(colorText:sub(7, 8), 16) / 255,
+ blue = tonumber(colorText:sub(7, 8), 16) / 255,
}
end
@@ -29,11 +29,12 @@ end
---@param color Color
---@return string
local function colorToText(color)
- return (''
- .. string.format('%02x', math.tointeger(color.alpha * 255))
- .. string.format('%02x', math.tointeger(color.red * 255))
- .. string.format('%02x', math.tointeger(color.green * 255))
- .. string.format('%02x', math.tointeger(color.blue * 255))):upper()
+ return string.format('%02X%02X%02X%02X'
+ , math.floor(color.alpha * 255)
+ , math.floor(color.red * 255)
+ , math.floor(color.green * 255)
+ , math.floor(color.blue * 255)
+ )
end
---@class Color
@@ -48,29 +49,30 @@ end
---@field finish integer
---@async
-function colors(uri)
+local function colors(uri)
local state = files.getState(uri)
local text = files.getText(uri)
if not state or not text then
return nil
end
---@type ColorValue[]
- local colors = {}
+ local colorValues = {}
guide.eachSource(state.ast, function (source) ---@async
if source.type == 'string' and isColor(source) then
---@type string
local colorText = source[1]
-
- colors[#colors+1] = {
- start = source.start + 1,
+
+ colorValues[#colorValues+1] = {
+ start = source.start + 1,
finish = source.finish - 1,
- color = textToColor(colorText)
+ color = textToColor(colorText)
}
end
end)
- return colors
+ return colorValues
end
+
return {
colors = colors,
colorToText = colorToText