|
1 | 1 | local files = require "files" |
2 | 2 | local guide = require "parser.guide" |
3 | 3 |
|
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 | + |
6 | 11 | ---@param source parser.object |
7 | | ----@return boolean |
8 | | -local function isColor(source) |
| 12 | +---@return ColorMode | false |
| 13 | +local function getColorMode(source) |
9 | 14 | ---@type string |
10 | 15 | local text = source[1] |
11 | | - if text:len() == 8 then |
12 | | - return text:match(colorPattern) |
13 | | - end |
14 | 16 |
|
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 |
17 | 21 | end |
18 | 22 |
|
19 | 23 | return false |
20 | 24 | end |
21 | 25 |
|
| 26 | +local textToColor = {} |
22 | 27 |
|
23 | 28 | ---@param colorText string |
24 | 29 | ---@return Color |
25 | | -local function textToColor(colorText) |
| 30 | +function textToColor.argb8(colorText) |
26 | 31 | return { |
27 | 32 | alpha = tonumber(colorText:sub(1, 2), 16) / 255, |
28 | 33 | red = tonumber(colorText:sub(3, 4), 16) / 255, |
|
33 | 38 |
|
34 | 39 | ---@param colorText string |
35 | 40 | ---@return Color |
36 | | -local function hexTextToColor(colorText) |
| 41 | +function textToColor.hexrgb6(colorText) |
37 | 42 | return { |
38 | | - alpha = 255, |
| 43 | + alpha = 1, |
39 | 44 | red = tonumber(colorText:sub(2, 3), 16) / 255, |
40 | 45 | green = tonumber(colorText:sub(4, 5), 16) / 255, |
41 | 46 | blue = tonumber(colorText:sub(6, 7), 16) / 255, |
42 | 47 | } |
43 | 48 | end |
44 | 49 |
|
| 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 | + |
45 | 61 | ---@param color Color |
46 | 62 | ---@return string |
47 | 63 | local function colorToText(color) |
@@ -75,17 +91,19 @@ local function colors(uri) |
75 | 91 | local colorValues = {} |
76 | 92 |
|
77 | 93 | 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) |
83 | 100 |
|
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 |
89 | 107 | end |
90 | 108 | end) |
91 | 109 | return colorValues |
|
0 commit comments