Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

支持RRGGBB的6位颜色格式 #3199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
MrZ626 wants to merge 3 commits into LuaLS:master
base: master
Choose a base branch
Loading
from MrZ626:master
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<!-- Add all new changes here. They will be moved under a version at release -->
* `FIX` cannot debug in Linux due to lua-debug expecting host process to have lua54 symbols available
* `FIX` support hex color codes with `#` in `textDocument/documentColor`
* `FIX` support 6-digit hex color codes in `textDocument/documentColor`

## 3.14.0
`2025年4月7日`
Expand Down
62 changes: 40 additions & 22 deletions script/core/color.lua
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
local files = require "files"
local guide = require "parser.guide"

local colorPattern = string.rep('%x', 8)
local hex6Pattern = string.format("^#%s", string.rep('%x', 6))
---@enum (key) ColorMode
local colorPattern = {
argb8 = "^%x%x%x%x%x%x%x%x$",
hexrgb6 = "^#%x%x%x%x%x%x$",
rgb6 = "^%x%x%x%x%x%x$",
}

Comment on lines +5 to +10
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The naming scheme is a bit out of the line when compared to their usual meaning in color science. The number usually means the number of bits for each color channel. So, ARGB8 would mean 4 channels where each has 8 bits, while RGB8 would be 3 channels of 8 bits.

If I understand the code correctly, the rgb6 and argb8 in the code store channels as Lua numbers, so doubles. I believe it would be a bit more consistent with some established conventions to drop the number and only specify the channel names.

Some references:
https://en.wikipedia.org/wiki/Color_depth#True_color_(24-bit)
https://www.khronos.org/opengl/wiki/Image_Format
https://photo.stackexchange.com/questions/90171/what-is-difference-between-8bit-rgb-and-16bit-rgb

---@param source parser.object
---@return boolean
local function isColor(source)
---@return ColorMode | false
local function getColorMode(source)
---@type string
local text = source[1]
if text:len() == 8 then
return text:match(colorPattern)
end

if text:len() == 7 then
return text:match(hex6Pattern)
for k,v in pairs(colorPattern) do
if text:match(v) then
return k
end
end

return false
end

local textToColor = {}

---@param colorText string
---@return Color
local function textToColor(colorText)
function textToColor.argb8(colorText)
return {
alpha = tonumber(colorText:sub(1, 2), 16) / 255,
red = tonumber(colorText:sub(3, 4), 16) / 255,
Expand All @@ -33,15 +38,26 @@ end

---@param colorText string
---@return Color
local function hexTextToColor(colorText)
function textToColor.hexrgb6(colorText)
return {
alpha = 255,
alpha = 1,
red = tonumber(colorText:sub(2, 3), 16) / 255,
green = tonumber(colorText:sub(4, 5), 16) / 255,
blue = tonumber(colorText:sub(6, 7), 16) / 255,
}
end

---@param colorText string
---@return Color
function textToColor.rgb6(colorText)
return {
alpha = 1,
red = tonumber(colorText:sub(1, 2), 16) / 255,
green = tonumber(colorText:sub(3, 4), 16) / 255,
blue = tonumber(colorText:sub(5, 6), 16) / 255,
}
end

---@param color Color
---@return string
local function colorToText(color)
Expand Down Expand Up @@ -75,17 +91,19 @@ local function colors(uri)
local colorValues = {}

guide.eachSource(state.ast, function (source) ---@async
if source.type == 'string' and isColor(source) then
---@type string
local colorText = source[1]

local color = colorText:match(colorPattern) and textToColor(colorText) or hexTextToColor(colorText)
if source.type == 'string' then
local colorMode = getColorMode(source)
if colorMode then
---@type string
local colorText = source[1]
local color = textToColor[colorMode](colorText)

colorValues[#colorValues+1] = {
start = source.start + 1,
finish = source.finish - 1,
color = color
}
colorValues[#colorValues+1] = {
start = source.start + 1,
finish = source.finish - 1,
color = color,
}
end
end
end)
return colorValues
Expand Down

AltStyle によって変換されたページ (->オリジナル) /