atom-pigments
atom-pigments copied to clipboard
Pattern for array
I'm using a library that defines RGB colors in arrays: "rgba(100, 160, 200, 0.5)"
becomes simply [100, 160, 200, 0.5]
.
Using arrays or lists is a common and flexible way to work with colors.
Is it possible to define new patterns using regex that can be changed in the settings?
Thank you.
Hi @i5ar, did you took a look at the extending pigments documentation? It's not really through a setting but by putting this into your init script (or by bundling it into a package) you'll get what you need:
i = '\\d+' # matches integers
d = "\\.#{i}" # matches decimals
f = "(?:#{i}#{d}|#{i}|#{d})" # matches floats
c = '\\s*,\\s*' # matches commas between values
# Calls the pigments service with the expression definition
atom.packages.serviceHub.provide 'pigments.expressions.colors', '1.0.0', {
name: 'custom:rgba-as-array'
regexpString: "\\[(#{i})#{c}(#{i})#{c}(#{i})#{c}(#{f})\\]"
scopes: ['*']
handle: (match, expression, context) ->
[_,r,g,b,a] = match
r = context.readInt(r)
g = context.readInt(g)
b = context.readInt(b)
a = context.readFloat(a)
@rgba = [r,g,b,a]
}
You might probably want to limit the expression to a more specific scope that *
. Scopes being here the file extensions where the expression will be available.