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

Define my own colours?

Open stugol opened this issue 8 years ago • 26 comments

In a project I'm writing, there are constants such as LIGHTRED, DARKBLUE etc, that are not highlighted because they're not standard colour names. How can I specify colours for these, and have Pigments highlight them?

stugol avatar Jan 23 '16 01:01 stugol

It depends on how you defines these colors. Do you mind giving me more details about how these colors are defined so that I can see which method would be best in your case?

abe33 avatar Jan 24 '16 19:01 abe33

Sure. In this particular case, it's Ruby and/or Crystal code:

module FMT
    NORMAL      =   "\e[00m";
    WHITE       =   "\e[1;37m";     LIGHTGREY       =   "\e[0;37m";     DARKGREY    = "\e[1;30m"
    RED         =   "\e[0;31m";     LIGHTRED        =   "\e[1;31m";
    GREEN       =   "\e[0;32m";     LIGHTGREEN      =   "\e[0;32m";
    BLUE        =   "\e[1;34m";     DARKBLUE        =   "\e[0;34m";
    CYAN        =   "\e[1;36m";     DARKCYAN        =   "\e[0;36m";
    MAGENTA     =   "\e[1;35m";     PURPLE          =   "\e[0;35m";
    YELLOW      =   "\e[0;33m";     LIGHTYELLOW     =   "\e[1;33m";

    BK_DARKGREY =   "\e[100m"

    CODES = {
        R: RED, Y: YELLOW, G: GREEN, B: DARKBLUE, M: MAGENTA, C: DARKCYAN, W: WHITE, K: DARKGREY, N: NORMAL,
        r: LIGHTRED, y: LIGHTYELLOW, g: LIGHTGREEN, b: BLUE, P: PURPLE, c: CYAN, k: LIGHTGREY,
    }.map { |k, v| { k.to_s => v } }.join
end

stugol avatar Jan 24 '16 19:01 stugol

Putting the following code in your init script should fix it:


atom.packages.serviceHub.consume 'pigments.api', '1.0.0', (pigmentsAPI) ->
  colorRegexp = "\\b(LIGHTRED|BK_DARKGREY)\\b(?![-\\.:=\\(])"
  registry = pigmentsAPI.getProject().getColorExpressionsRegistry()

  registry.createExpression 'my-custom-colors', colorRegexp, ['*'], (match, expression, context) ->
    colors =
      LIGHTRED: "#FF6666"
      BK_DARKGREY: "#333"

    [_,name] = match

    @colorExpression = @name = name
    @hex = colors[name].replace('#','')

It's a bit more complicated that what I originally thought but it does the trick. I'll think of a better way to extend pigments as for the moment expression callback function cannot reuse their scope.

abe33 avatar Jan 24 '16 20:01 abe33

Thanks. There are a couple of problems, however.

Firstly, BK_DARKGREY renders in a midnight blue unless I replace its definition with '#333333'. Secondly, only some of the colours are being shown, even normal ones.

Screenshot

stugol avatar Jan 24 '16 20:01 stugol

Secondly, only some of the colours are being shown, even normal ones.

This is not what I got when I tried, I had all the normal colors higlighted, except LIGHTRED and BK_DARKGREY, did you by change ignore some scopes?

abe33 avatar Jan 24 '16 21:01 abe33

No...?

stugol avatar Jan 24 '16 21:01 stugol

The problem occurs when the words are preceded by tabs. The three that display correctly in the screenshot had spaces as well as tabs. I've corrected that now, and now I have no colouration whatsoever in the first list :(

stugol avatar Jan 24 '16 21:01 stugol

Actually, the colors should not be highlighted when on the left side of an affection, hence the (?![-\\.:=\\(]) in the expression above which matches only when the color is not followed by ., :, = or `(. The idea here is that in most cases you don't really care about having the name of a color highlighted when defining it since the right part of the affectation should already display the color, but you probably want to have the color highlighted in the places where it's used.

The problem occurs when the words are preceded by tabs.

Yeah I just noticed that (I'm never used tabs and no one seems to have reported this issue before), I'll push a fix for that in the next version.

abe33 avatar Jan 24 '16 22:01 abe33

Then why are the colours you helped me define (LIGHTRED and BK_DARKGREY) highlighting to the left of an assignment?

stugol avatar Jan 24 '16 22:01 stugol

Oh right, that's because I omitted to put a space character match in the expression, the negative lookahead should look like this instead: (?![ \\t]*[-\\.:=\\(])

Like in the color expression for SVG colors: https://github.com/abe33/atom-pigments/blob/master/lib/color-expressions.coffee#L291

abe33 avatar Jan 25 '16 00:01 abe33

I have another question. DARKGREY isn't dark enough. How can I modify the actual colour shown?

Adding an entry to the init script didn't help, because the colour is already defined.

stugol avatar Jan 25 '16 03:01 stugol

Adding an entry to the init script didn't help, because the colour is already defined.

You still can, using the init script, expressions are sorted using a priority value, in that case you can pass a numeric value before the scopes to make the expressions appears before the SVG one:

registry.createExpression 'my-custom-colors', colorRegexp, 1, ['*'], (match, expression, context) ->

abe33 avatar Jan 25 '16 08:01 abe33

I already tried that. It didn't help at all.

stugol avatar Jan 25 '16 18:01 stugol

You're right, looks like it's missing something, I got the proper color when followed with a ) but not when followed with a space, let me sometime to think about a better solution. Maybe I'll add a way to directly define custom colors for a project instead of having to add something in an init script.

abe33 avatar Jan 26 '16 12:01 abe33

@abe33 I'd like to config pigments to work with my swift code.

rgba

hex2010 avatar Nov 02 '16 16:11 hex2010

Hi @hex2010, let me a few hours and I might be able to post you a snippet to make it work.

abe33 avatar Nov 07 '16 16:11 abe33

96 hours are still a few hours? :wink:

Cxarli avatar Nov 11 '16 17:11 Cxarli

Closing due to inactivity for an extended period.

Cxarli avatar Sep 22 '17 19:09 Cxarli

Um....you're closing an issue simply because nobody has bothered to fix the damn thing? That's stupid.

Sod-Almighty avatar Sep 22 '17 23:09 Sod-Almighty

Yeah, I'm sorry. Thought this was a bug, but it's indeed an improvement/feature request. I'll reopen it. In general, bug that didn't get any attention for a long while, are usually fixed already, but someone forgot to close it. As I'm trying to clear up this repo a bit during @abe33's absence, I closed many.

Cxarli avatar Sep 23 '17 22:09 Cxarli

Having the ability to be able to add custom colors in a simple way would indeed be nice !

X-Raym avatar Apr 11 '18 06:04 X-Raym

According to https://github.com/abe33/atom-pigments/blob/master/docs/extending-pigments.md, it seems possible to add custom colors, but the documentation doesn't tells anything about where (in which files) to put these functions... :/

X-Raym avatar Apr 11 '18 06:04 X-Raym

Hi,

I'm trying to make a custom skin for Steam, and they define rgba colors as "{r} {g} {b} {a}".

I copied the regex from another issue.

My question is, how do I only make the text inside the quotes render with the colors? Now the double quotes get included as well.

screen shot 2018-06-01 at 15 50 49

Current init.coffee

i = '\\d+' # matches integers
d = "\\.#{i}" # matches decimals
f = "(?:#{i}#{d}|#{i}|#{d})" # matches floats
c = '\\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})\\]"
  regexpString: "\"(#{i})#{c}(#{i})#{c}(#{i})#{c}(#{f})\""
  scopes: ['styles', 'layout']
  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]
    console.log match

MindTooth avatar Jun 01 '18 13:06 MindTooth

@MindTooth Hmm, there doesn't seem to be any way to currently do this in Pigments. The easiest way would be to exclude the quotation marks from the regexpString

Also hint: replace name with your own name. I'm not sure if it will break anything, but it can't hurt to change it to custom:steam-skin (for example)

Cxarli avatar Jun 01 '18 16:06 Cxarli

Thanks @C-Bouthoorn :) I'll be sure to change the name. Thanks for looking into it 👍

MindTooth avatar Jun 01 '18 16:06 MindTooth

@MindTooth

Maybe this can help ? https://github.com/abe33/atom-pigments/blob/master/docs/extending-pigments.md

it is not related to this issue tough. This is for Color Names, not color expression.

X-Raym avatar Jun 01 '18 16:06 X-Raym