Skip to content
This repository was archived by the owner on Nov 13, 2024. It is now read-only.

Commit 78fc5d7

Browse files
committed
Add shorthand hex recognition
1 parent a94dc44 commit 78fc5d7

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

lua/color-picker/utils/window.lua

+9-1
Original file line numberDiff line numberDiff line change
@@ -568,11 +568,12 @@ end --}}}
568568

569569
local function detect_colors(str) --{{{
570570
local hex_pattern = "#%x%x%x%x%x%x%x?%x?"
571+
local short_hex_pattern = "#%x%x%x"
571572
local rgb_pattern = "rgba?%(%s*%d+%s*,%s*%d+%s*,%s*%d+%s*.*%)"
572573
local hsl_pattern = "hsla?%(%s*%d+%s*,%s*%d+%s*%%*,%s*%d+%s*%%*.*%)"
573574

574575
local results = {}
575-
local patterns = { hex_pattern, rgb_pattern, hsl_pattern }
576+
local patterns = { hex_pattern, short_hex_pattern, rgb_pattern, hsl_pattern }
576577

577578
for _, pattern in ipairs(patterns) do
578579
local start_index = 1
@@ -633,13 +634,15 @@ end --}}}
633634
local function sandwich_processor(str) --{{{
634635
local hex_capture_pattern = "#(%x%x%x%x%x%x)"
635636
local hexa_capture_pattern = "#(%x%x%x%x%x%x)(%x%x)"
637+
local short_hex_capture_pattern = "#(%x%x%x)"
636638
local rgba_capture_pattern = "rgba%(%s*(%d+)%s*,%s*(%d+)%s*,%s*(%d+)%s*,?%s*(%d+%.?%d*)%s*%)"
637639
local hsla_capture_pattern = "hsla%(%s*(%d+)%s*,%s*(%d+)%s*%%*,%s*(%d+)%s*%%,?%s*(%d+%.?%d*)%s*%)"
638640
local rgb_capture_pattern = "rgb%(%s*(%d+)%s*,%s*(%d+)%s*,%s*(%d+)%s*,?%s*%)"
639641
local hsl_capture_pattern = "hsl%(%s*(%d+)%s*,%s*(%d+)%s*%%*,%s*(%d+)%s*%%,?%s*%)"
640642

641643
local _, _, hex_val, hex_trans = string.find(str, hexa_capture_pattern)
642644
local _, _, hex = string.find(str, hex_capture_pattern)
645+
local _, _, short_hex = string.find(str, short_hex_capture_pattern)
643646
local _, _, ra, ga, ba, rgba = string.find(str, rgba_capture_pattern)
644647
local _, _, ha, sa, la, hsla = string.find(str, hsla_capture_pattern)
645648
local _, _, r, g, b = string.find(str, rgb_capture_pattern)
@@ -648,6 +651,11 @@ local function sandwich_processor(str) --{{{
648651
return { "hex", hex_val, hex_trans }
649652
elseif hex then
650653
return { "hex", hex }
654+
elseif short_hex then
655+
local red = short_hex:sub(1, 1)
656+
local green = short_hex:sub(2, 2)
657+
local blue = short_hex:sub(3, 3)
658+
return { "hex", red .. red .. green .. green .. blue .. blue }
651659
elseif ra then
652660
return { "rgb", tonumber(ra), tonumber(ga), tonumber(ba), tonumber(rgba) }
653661
elseif ha then

0 commit comments

Comments
 (0)