@@ -2,15 +2,21 @@ local files = require "files"
22local guide = require " parser.guide"
33
44local colorPattern = string.rep (' %x' , 8 )
5+ local hex6Pattern = string.format (" ^#%s" , string.rep (' %x' , 6 ))
56--- @param source parser.object
67--- @return boolean
78local function isColor (source )
89 --- @type string
910 local text = source [1 ]
10- if text :len () ~ = 8 then
11- return false
11+ if text :len () = = 8 then
12+ return text : match ( colorPattern )
1213 end
13- return text :match (colorPattern )
14+
15+ if text :len () == 7 then
16+ return text :match (hex6Pattern )
17+ end
18+
19+ return false
1420end
1521
1622
@@ -25,6 +31,16 @@ local function textToColor(colorText)
2531 }
2632end
2733
34+ --- @param colorText string
35+ --- @return Color
36+ local function hexTextToColor (colorText )
37+ return {
38+ alpha = 255 ,
39+ red = tonumber (colorText :sub (2 , 3 ), 16 ) / 255 ,
40+ green = tonumber (colorText :sub (4 , 5 ), 16 ) / 255 ,
41+ blue = tonumber (colorText :sub (6 , 7 ), 16 ) / 255 ,
42+ }
43+ end
2844
2945--- @param color Color
3046--- @return string
@@ -63,10 +79,12 @@ local function colors(uri)
6379 --- @type string
6480 local colorText = source [1 ]
6581
82+ local color = colorText :match (colorPattern ) and textToColor (colorText ) or hexTextToColor (colorText )
83+
6684 colorValues [# colorValues + 1 ] = {
6785 start = source .start + 1 ,
6886 finish = source .finish - 1 ,
69- color = textToColor ( colorText )
87+ color = color
7088 }
7189 end
7290 end )
0 commit comments