|
19 | 19 | local function textToColor(colorText) |
20 | 20 | return { |
21 | 21 | 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, |
23 | 23 | 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, |
25 | 25 | } |
26 | 26 | end |
27 | 27 |
|
28 | 28 |
|
29 | 29 | ---@param color Color |
30 | 30 | ---@return string |
31 | 31 | 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 | + ) |
37 | 38 | end |
38 | 39 |
|
39 | 40 | ---@class Color |
|
48 | 49 | ---@field finish integer |
49 | 50 |
|
50 | 51 | ---@async |
51 | | -function colors(uri) |
| 52 | +local function colors(uri) |
52 | 53 | local state = files.getState(uri) |
53 | 54 | local text = files.getText(uri) |
54 | 55 | if not state or not text then |
55 | 56 | return nil |
56 | 57 | end |
57 | 58 | ---@type ColorValue[] |
58 | | - local colors = {} |
| 59 | + local colorValues = {} |
59 | 60 |
|
60 | 61 | guide.eachSource(state.ast, function (source) ---@async |
61 | 62 | if source.type == 'string' and isColor(source) then |
62 | 63 | ---@type string |
63 | 64 | local colorText = source[1] |
64 | | - |
65 | | - colors[#colors+1] = { |
66 | | - start = source.start + 1, |
| 65 | + |
| 66 | + colorValues[#colorValues+1] = { |
| 67 | + start = source.start + 1, |
67 | 68 | finish = source.finish - 1, |
68 | | - color = textToColor(colorText) |
| 69 | + color = textToColor(colorText) |
69 | 70 | } |
70 | 71 | end |
71 | 72 | end) |
72 | | - return colors |
| 73 | + return colorValues |
73 | 74 | end |
| 75 | + |
74 | 76 | return { |
75 | 77 | colors = colors, |
76 | 78 | colorToText = colorToText |
|
0 commit comments