atom-pigments icon indicating copy to clipboard operation
atom-pigments copied to clipboard

Pattern for array

Open i5ar opened this issue 8 years ago • 1 comments

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.

i5ar avatar Aug 25 '16 23:08 i5ar

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.

abe33 avatar Aug 26 '16 08:08 abe33