Skip to content

Commit 35a37a0

Browse files
committed
feat: support rgb6 format
1 parent 759e8fb commit 35a37a0

File tree

1 file changed

+40
-22
lines changed

1 file changed

+40
-22
lines changed

script/core/color.lua

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
local files = require "files"
22
local guide = require "parser.guide"
33

4-
local colorPattern = string.rep('%x', 8)
5-
local hex6Pattern = string.format("^#%s", string.rep('%x', 6))
4+
---@enum (key) ColorMode
5+
local colorPattern = {
6+
argb8 = "^%x%x%x%x%x%x%x%x$",
7+
hexrgb6 = "^#%x%x%x%x%x%x$",
8+
rgb6 = "^%x%x%x%x%x%x$",
9+
}
10+
611
---@param source parser.object
7-
---@return boolean
8-
local function isColor(source)
12+
---@return ColorMode | false
13+
local function getColorMode(source)
914
---@type string
1015
local text = source[1]
11-
if text:len() == 8 then
12-
return text:match(colorPattern)
13-
end
1416

15-
if text:len() == 7 then
16-
return text:match(hex6Pattern)
17+
for k,v in next,colorPattern do
18+
if text:match(v) then
19+
return k
20+
end
1721
end
1822

1923
return false
2024
end
2125

26+
local textToColor = {}
2227

2328
---@param colorText string
2429
---@return Color
25-
local function textToColor(colorText)
30+
function textToColor.argb8(colorText)
2631
return {
2732
alpha = tonumber(colorText:sub(1, 2), 16) / 255,
2833
red = tonumber(colorText:sub(3, 4), 16) / 255,
@@ -33,15 +38,26 @@ end
3338

3439
---@param colorText string
3540
---@return Color
36-
local function hexTextToColor(colorText)
41+
function textToColor.hexrgb6(colorText)
3742
return {
38-
alpha = 255,
43+
alpha = 1,
3944
red = tonumber(colorText:sub(2, 3), 16) / 255,
4045
green = tonumber(colorText:sub(4, 5), 16) / 255,
4146
blue = tonumber(colorText:sub(6, 7), 16) / 255,
4247
}
4348
end
4449

50+
---@param colorText string
51+
---@return Color
52+
function textToColor.rgb6(colorText)
53+
return {
54+
alpha = 1,
55+
red = tonumber(colorText:sub(1, 2), 16) / 255,
56+
green = tonumber(colorText:sub(3, 4), 16) / 255,
57+
blue = tonumber(colorText:sub(5, 6), 16) / 255,
58+
}
59+
end
60+
4561
---@param color Color
4662
---@return string
4763
local function colorToText(color)
@@ -75,17 +91,19 @@ local function colors(uri)
7591
local colorValues = {}
7692

7793
guide.eachSource(state.ast, function (source) ---@async
78-
if source.type == 'string' and isColor(source) then
79-
---@type string
80-
local colorText = source[1]
81-
82-
local color = colorText:match(colorPattern) and textToColor(colorText) or hexTextToColor(colorText)
94+
if source.type == 'string' then
95+
local colorMode = getColorMode(source)
96+
if colorMode then
97+
---@type string
98+
local colorText = source[1]
99+
local color = textToColor[colorMode](colorText)
83100

84-
colorValues[#colorValues+1] = {
85-
start = source.start + 1,
86-
finish = source.finish - 1,
87-
color = color
88-
}
101+
colorValues[#colorValues+1] = {
102+
start = source.start + 1,
103+
finish = source.finish - 1,
104+
color = color,
105+
}
106+
end
89107
end
90108
end)
91109
return colorValues

0 commit comments

Comments
 (0)