atom-pigments
atom-pigments copied to clipboard
Define my own colours?
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?
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?
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
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.
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.
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?
No...?
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 :(
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.
Then why are the colours you helped me define (LIGHTRED and BK_DARKGREY) highlighting to the left of an assignment?
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
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.
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) ->
I already tried that. It didn't help at all.
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 I'd like to config pigments to work with my swift code.
Hi @hex2010, let me a few hours and I might be able to post you a snippet to make it work.
96 hours are still a few hours? :wink:
Closing due to inactivity for an extended period.
Um....you're closing an issue simply because nobody has bothered to fix the damn thing? That's stupid.
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.
Having the ability to be able to add custom colors in a simple way would indeed be nice !
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... :/
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](https://user-images.githubusercontent.com/33870508/40844270-c5c63cd4-65b3-11e8-90e9-9e1d35fa0266.png)
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 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)
Thanks @C-Bouthoorn :) I'll be sure to change the name. Thanks for looking into it 👍
@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.