Skip to content
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

Color schemes to share #27

Open
Kabouik opened this issue Mar 10, 2020 · 18 comments
Open

Color schemes to share #27

Kabouik opened this issue Mar 10, 2020 · 18 comments

Comments

@Kabouik
Copy link

Kabouik commented Mar 10, 2020

This package is a godsend, I have been wanting this for years and somehow never stumbled upon your repository.

However, I am sure many users are too lazy to set their own color schemes and there are no scheme resources except from those detailed in setOutputColors' help. I am opening this issue so that people can share their own schemes (ideally with a screenshot), hope you'll agree with that but else just close the issue.

Here's my first:

ss-2020-03-10_191116

ss-2020-03-10_191328

The corresponding part in my ~/.Rprofile:

# Colorize outputs in terminal (requires the "colorout" package)
if (!require("pacman")) install.packages("pacman", repos = "https://pbil.univ-lyon1.fr/CRAN/")
pacman::p_load(colorout)

setOutputColors(
				normal = 252,
				negnum = 146,
				zero = 187,
				number = 110,
				date = 61,
				string = 209,
				const = 75,
				warn = c(236, 179),
				error = c(236, 198),
				zero.limit = 0.1,
				verbose = T
				)
@jalvesaq
Copy link
Owner

Thank your for your contribution! Perhaps we could start the colorout's wiki, but I guess that color schemes would be more easily found here. So, we may let this "issue" open.

@kar9222
Copy link

kar9222 commented Mar 14, 2020

@jalvesaq Somehow I randomly found your package and it's such a beauty! I have to say this repo is really a hidden treasure. Thank you for the great work. Performance is great as I havent noticed any difference at all (C is fast!).

Additionally, I customize with colorout::setOutputColors & colourout::addPattern for

  • base R: class for str
  • {data.table} : row id, column class
  • {mlr3} : R6 field name

Click to enlarge image

Imgur

I also shared with the R community with my gist and my tweet here. Feel free to use the codes as an example (though not perfect) for colorout::addPattern.

Another question, I wonder what is the reason behind the lack of regex support? I believe it's for performance?

@jalvesaq
Copy link
Owner

I wonder what is the reason behind the lack of regex support? I believe it's for performance?

It's for speed and to avoid dependencies which could complicate the installation for some people. But the slowness of a regular expression library is just a supposition because I have never tried any.

@eddelbuettel
Copy link

eddelbuettel commented Mar 14, 2020

Great package, @jalvesaq and too bad it'll never make CRAN for Policy violations. I think you should consider a post on r-devel and ask if R Core would like it as a patch. It's clean, simple and useful.

Thanks also to @kar9222 for the tweet and gist. Running your setup now. Being a huge fan of the Nord theme(s) at https://github.com/arcticicestudio/nord (and many sibbling repos in the same org), anybody else up for tweaking a colour setup for that?

@kar9222
Copy link

kar9222 commented Mar 15, 2020

@eddelbuettel You're welcome. I think it's good idea to generate some popular premade themes for the community and they can further customize them in hex color codes with some helpers together with this awesome package.

Yes I fell in love with Nord theme at first sight. However, for me it has low contrast. I tried their higher contrast version but the feeling isn't the same. Additionally, I tried tweaking the contrast, brightness, etc on my own but after tweaking it's not as elegant as the original Nord theme. I learned that somehow you can't tweak colors simply just based on brightness/contrast. On the other hand, Monokai has near-perfect contrast but personally I'm not fond its color combination. So in the end I decided to make my own theme, which is kind of a mix of the elegancy of Nord + (higher) contrast of Monokai for my liking 😂. Feel free to try out, my main color combo: #282C35, #f8f8f2, #CCD9DD, #DCE2FB, #E6DB7F, #B2E3E5, #6E7C96, #909BAF, #444956, #FBE3A5, #E4CAAF. I somehow don't like 'bracket' & 'quote' so I color theme in darker color with minimal contrast as I use a mix of indentation + low contrast bracket/quote.

Anyway, here are the themes for the community.

Theme

  • Premade theme
    • Popular themes like Nord, Monokai
    • Feel free to ping me for additional themes & I'll try my best to generate codes for you, when I'm free 🤣
  • Custom theme
    • Edit premade themes or customize your brand new theme
    • Don't be shy to share with #Rstats community 😎

Premade theme

Pick your theme and copy the codes at the collapsible code section and put them in your .Rprofile

NOTE Focus on the terminal output, as the terminal input is controlled by my terminal.

Nord

Put @ .Rprofile

# General ---------------------------------------- 

colorout::setOutputColors(
  
  index    = '\x1b[38;2;76;86;106m',
  normal   = '\x1b[38;2;216;222;233m',

  number   = '\x1b[38;2;236;239;244m',
  negnum   = '\x1b[38;2;180;142;173m',
  zero     = '\x1b[38;2;136;192;208m', zero.limit = 0.01,
  infinite = '\x1b[38;2;236;239;244m',

  string   = '\x1b[38;2;235;203;139m',
  date     = '\x1b[38;2;236;239;244m',
  const    = '\x1b[38;2;136;192;208m',
  
  true     = '\x1b[38;2;163;190;140m',
  false    = '\x1b[38;2;191;97;106m',

  warn     = '\x1b[38;2;235;203;139m',
  stderror = '\x1b[38;2;191;97;106m', error = '\x1b[38;2;191;97;106m',

  verbose  = FALSE
)

# Custom patterns --------------------------------

# NOTE Do not copy all. Pick what you use/like.

# _ {data.table} ---------------------------------

colorout::addPattern('[0-9]*:',  '\x1b[38;2;143;188;187m')  # Row num
colorout::addPattern('---',      '\x1b[38;2;76;86;106m')  # Row splitter 
colorout::addPattern('<[a-z]*>', '\x1b[38;2;143;188;187m')  # Col class 

# _ `str` ----------------------------------------

# Class
colorout::addPattern(' num ',        '\x1b[38;2;143;188;187m')
colorout::addPattern(' int ',        '\x1b[38;2;143;188;187m')
colorout::addPattern(' chr ',        '\x1b[38;2;143;188;187m')
colorout::addPattern(' Factor ',     '\x1b[38;2;143;188;187m')
colorout::addPattern(' Ord.factor ', '\x1b[38;2;143;188;187m')
colorout::addPattern(' logi ',       '\x1b[38;2;143;188;187m')
colorout::addPattern('function ',    '\x1b[38;2;143;188;187m')
colorout::addPattern(' dbl ',        '\x1b[38;2;143;188;187m')
colorout::addPattern(' lgcl ',       '\x1b[38;2;143;188;187m')
colorout::addPattern(' cplx ',       '\x1b[38;2;143;188;187m')
# Misc
colorout::addPattern('$ ',           '\x1b[38;2;76;86;106m')

# _ `str`, {mlr3} --------------------------------

# R6 field name
colorout::addPattern('* [A-z]*:',                      '\x1b[38;2;235;203;139m')
colorout::addPattern("* [A-z]* [A-z]*:",               '\x1b[38;2;235;203;139m')
colorout::addPattern("* [A-z]* [A-z]* [A-z]*:",        '\x1b[38;2;235;203;139m')
colorout::addPattern("* [A-z]* [A-z]* [A-z]* [A-z]*:", '\x1b[38;2;235;203;139m')
# So on...

...

Imgur

Monokai

Put @ .Rprofile

colorout::setOutputColors(
  
  index    = '\x1b[38;2;76;86;106m',
  normal   = '\x1b[38;2;236;239;244m',

  number   = '\x1b[38;2;236;239;244m',
  negnum   = '\x1b[38;2;253;151;31m',
  zero     = '\x1b[38;2;102;217;239m', zero.limit = 0.01,
  infinite = '\x1b[38;2;236;239;244m',

  string   = '\x1b[38;2;230;219;116m',
  date     = '\x1b[38;2;236;239;244m',
  const    = '\x1b[38;2;253;151;31m',
  
  true     = '\x1b[38;2;166;226;46m',
  false    = '\x1b[38;2;249;38;114m',

  warn     = '\x1b[38;2;230;159;102',
  stderror = '\x1b[38;2;249;38;114m', error = '\x1b[38;2;249;38;114m',

  verbose  = FALSE
)

# NOTE Do not copy all. Pick what you use/like.

# {data.table} -----------------------------------

colorout::addPattern('[0-9]*:',  '\x1b[38;2;117;113;94m')  # Row num
colorout::addPattern('---',      '\x1b[38;2;117;113;94m')  # Row splitter 
colorout::addPattern('<[a-z]*>', '\x1b[38;2;117;113;94m')  # Col class 

# `str` ------------------------------------------

# Class
colorout::addPattern(' num ',        '\x1b[38;2;117;113;94m')
colorout::addPattern(' int ',        '\x1b[38;2;117;113;94m')
colorout::addPattern(' chr ',        '\x1b[38;2;117;113;94m')
colorout::addPattern(' Factor ',     '\x1b[38;2;117;113;94m')
colorout::addPattern(' Ord.factor ', '\x1b[38;2;117;113;94m')
colorout::addPattern(' logi ',       '\x1b[38;2;117;113;94m')
colorout::addPattern('function ',    '\x1b[38;2;117;113;94m')
colorout::addPattern(' dbl ',        '\x1b[38;2;117;113;94m')
colorout::addPattern(' lgcl ',       '\x1b[38;2;117;113;94m')
colorout::addPattern(' cplx ',       '\x1b[38;2;117;113;94m')
# Misc
colorout::addPattern('$ ',           '\x1b[38;2;76;86;106m')

# `str`, {mlr3} ----------------------------------

# R6 field name
colorout::addPattern('* [A-z]*:',                      '\x1b[38;2;174;129;255m')
colorout::addPattern("* [A-z]* [A-z]*:",               '\x1b[38;2;174;129;255m')
colorout::addPattern("* [A-z]* [A-z]* [A-z]*:",        '\x1b[38;2;174;129;255m')
colorout::addPattern("* [A-z]* [A-z]* [A-z]* [A-z]*:", '\x1b[38;2;174;129;255m')
# So on...

...

Imgur

Custom theme

colorout::setOutputColors can be used for full customization without this helper. For more info, see ?colorout::setOutputColors. If you wish to generate ansi color codes with hex color codes, you can use this helper, for convenience. It's not recommended to put these helpers in .Rprofile. Simply use them separately for generating inputs for colorout::setOutputColors. If case you'd use them in .Rprofile, make sure to get rid of all the garbage.

Manually get colors in hex format from your favorite theme (ie Nord) and fill them in named list.

Let's do an example with Nord theme. First, get colors from Nord. Then...

Helper

#' Helper for generating ansi color codes with hex color codes.
#'
#' After generating ansi color codes, feed input to `colorout::setOutputColors`.
#'
#' @param fg Foreground color in hex format (ie '#000000'). Leave blank for default.
#' @param bg Background color in hex format (ie '#000000'). Leave blank for default.
#' @param fo Formatting (see details)
#' 
#' @details
#' Value	Formating
#' 0	No formating
#' 1	Bold or bright
#' 2	Faint
#' 3	Italic or inverse
#' 4	Underline
#' 5	Blink slowly
#' 6	Blink quickly
#' 7	Invert

to_ansi <- function(col, fg = '', bg = '', fo = '') {

  escape <- '\\x1b['
  
  if (fg != '') fg <- {
    rgb_fg <- col2rgb(fg)
    paste0("38;2;", rgb_fg[1], ";", rgb_fg[2], ";", rgb_fg[3])
  }
  if (bg != '') bg <- {
    rgb_bg <- col2rgb(bg)
    paste0(";48;2;", rgb_bg[1], ";", rgb_bg[2], ";", rgb_bg[3])
  }
  fo <- if (fo != '') paste0(';', fo)

  # Use `cat`, not `paste0`, for correctly printing escape char '\x1b['
  cat(col, ': ', escape, fg, bg, fo, 'm', '\n', sep = '')
}

Either

  • generate invididual style one-by-one
    • Useful for editing one/a few colors of premade themes
  • or generate all styles
    • Useful for creating brand new themes

Generate invididual style

to_ansi('white',            "#ECEFF4")                 # white foreground
to_ansi('white_black',      "#ECEFF4", "#4C566A")      # white foreground, black background
to_ansi('white_black_bold', "#ECEFF4", "#4C566A",  1)  # white foreground, black background, bold text
# white:            \x1b[38;2;236;239;244m
# white_black:      \x1b[38;2;236;239;244;48;2;76;86;106m
# white_black_bold: \x1b[38;2;236;239;244;48;2;76;86;106;1m

Then input the color codes (ie \x1b[38;2;236;239;244m) to colorout::setOutputColors

  • manually
  • assign to variables with your R skills & DO NOT forget to rm them to avoid contaminating your environments.

or ...

Generate all styles

# Fill in color names, foreground colors, background colors and formatting (ie bold/italic). For default, leave it blank.
theme <- list(
  colors     = c('white',   'black',   'snow',    'turquoise', 'dark_red', 'dark_green', 'yellow',  'green',   'red',     'yellow_bold'),
  foreground = c("#ECEFF4", "#4C566A", '#D8DEE9', '#88C0D0',   "#B48EAD",  "#8FBCBB",    "#EBCB8B", "#A3BE8C", "#BF616A", "#EBCB8B"),
  background = c("",        "",        "",        "",          "",         "",           "",        "",        "",        ""),
  formatting = c("",        "",        "",        "",          "",         "",           "",        "",        "",        1)
)

# - Manually copy & paste color codes (in character type) to `colorout::setOutputColors`. It's okay because you only set it once-and-for-all. Otherwise, you can assign the colors to variables and remove them after calling `colorout::setOutputColors`
# - or assign them to variables AND make sure to remove them after calling `colorout::setOutputColors` because you don't want them to contaminate your environments.
invisible( mapply(to_ansi, theme[[1]], theme[[2]], theme[[3]], theme[[4]]) )

# white:       \x1b[38;2;236;239;244m
# black:       \x1b[38;2;76;86;106m
# snow:        \x1b[38;2;216;222;233m
# turquoise:   \x1b[38;2;136;192;208m
# dark_red:    \x1b[38;2;180;142;173m
# dark_green:  \x1b[38;2;143;188;187m
# yellow:      \x1b[38;2;235;203;139m
# green:       \x1b[38;2;163;190;140m
# red:         \x1b[38;2;191;97;106m
# yellow_bold: \x1b[38;2;235;203;139;1m

# General ---------------------------------------- 

colorout::setOutputColors(
  
  index    = '\x1b[38;2;76;86;106m',
  normal   = '\x1b[38;2;216;222;233m',

  number   = '\x1b[38;2;236;239;244m',
  negnum   = '\x1b[38;2;180;142;173m',
  zero     = '\x1b[38;2;136;192;208m', zero.limit = 0.01,
  infinite = '\x1b[38;2;236;239;244m',

  string   = '\x1b[38;2;235;203;139m',
  date     = '\x1b[38;2;236;239;244m',
  const    = '\x1b[38;2;136;192;208m',
  
  true     = '\x1b[38;2;163;190;140m',
  false    = '\x1b[38;2;191;97;106m',

  warn     = '\x1b[38;2;235;203;139m',
  stderror = '\x1b[38;2;191;97;106m', error = '\x1b[38;2;191;97;106m',

  verbose  = FALSE
)

# Custom patterns --------------------------------

# NOTE Do not copy all. Pick what you use/like.

# _ {data.table} ---------------------------------

colorout::addPattern('[0-9]*:',  '\x1b[38;2;143;188;187m')  # Row num
colorout::addPattern('---',      '\x1b[38;2;76;86;106m')  # Row splitter 
colorout::addPattern('<[a-z]*>', '\x1b[38;2;143;188;187m')  # Col class 

# _ `str` ----------------------------------------

# Class
colorout::addPattern(' num ',        '\x1b[38;2;143;188;187m')
colorout::addPattern(' int ',        '\x1b[38;2;143;188;187m')
colorout::addPattern(' chr ',        '\x1b[38;2;143;188;187m')
colorout::addPattern(' Factor ',     '\x1b[38;2;143;188;187m')
colorout::addPattern(' Ord.factor ', '\x1b[38;2;143;188;187m')
colorout::addPattern(' logi ',       '\x1b[38;2;143;188;187m')
colorout::addPattern('function ',    '\x1b[38;2;143;188;187m')
colorout::addPattern(' dbl ',        '\x1b[38;2;143;188;187m')
colorout::addPattern(' lgcl ',       '\x1b[38;2;143;188;187m')
colorout::addPattern(' cplx ',       '\x1b[38;2;143;188;187m')
# Misc
colorout::addPattern('$ ',           '\x1b[38;2;76;86;106m')

# _ `str`, {mlr3} --------------------------------

# R6 field name
colorout::addPattern('* [A-z]*:',                      '\x1b[38;2;235;203;139m')
colorout::addPattern("* [A-z]* [A-z]*:",               '\x1b[38;2;235;203;139m')
colorout::addPattern("* [A-z]* [A-z]* [A-z]*:",        '\x1b[38;2;235;203;139m')
colorout::addPattern("* [A-z]* [A-z]* [A-z]* [A-z]*:", '\x1b[38;2;235;203;139m')
# So on...

# Clean up
rm(theme, to_ansi)

Test

Same codes used in screenshots for color validation, if needed.

# {data.table} -----------------------------------

data.table::data.table(
    postive_negative_and_near_zero = sample(c(1, - 1, .005), 200, replace = TRUE),
    true_false_NA                  = sample(c(TRUE, FALSE, NA), 200, replace = TRUE))

# {mlr3} -----------------------------------------

library(mlr3)
task <- tsk('iris')
learner <- lrn('classif.rpart')
learner$train(task, row_ids = 1:120)
learner

mlr3::msr("classif.auc")

# base R `str` -----------------------------------

str(iris)
str(diamonds)

str(list(col_a = rnorm(100), list(
  col_b = "This is string...",
  col_c = c(T, F, T, T, F),
  list(col_d = sum)
)))

@jalvesaq
Copy link
Owner

I think you should consider a post on r-devel and ask if R Core would like it as a patch. It's clean, simple and useful. (@eddelbuettel)

I cant't write the patch anytime soon, but it would be great if someone wanted to use the code of colorout to do that. It's free software!

@sjlva
Copy link

sjlva commented Nov 26, 2020

Gruvbox theme

image

colorout::setOutputColors(
  
  index    = '\x1b[38;2;215;153;33m', #
  normal   = '\x1b[38;2;235;219;178m', #

  number   = '\x1b[38;2;211;134;155m', #
  negnum   = '\x1b[38;2;104;157;106m', #
  zero     = '\x1b[38;2;69;133;136m', zero.limit = 0.01,#
  infinite = '\x1b[38;2;250;189;47m', #

  string   = '\x1b[38;2;184;187;38m', #
  date     = '\x1b[38;2;254;128;25m', #
  const    = '\x1b[38;2;250;189;47m',
  
  true     = '\x1b[38;2;142;192;124m',#
  false    = '\x1b[38;2;251;73;52m',#

  warn     = '\x1b[38;2;250;189;47m', #
  stderror = '\x1b[38;2;204;36;29m', error = '\x1b[38;2;204;36;29m',#

  verbose  = TRUE
)

@kar9222
Copy link

kar9222 commented Nov 26, 2020

Nice theme! Hope more people would discover this hidden gem {colorout}.

@sjlva
Copy link

sjlva commented Dec 30, 2020

Base 16 - Solarized Dark theme

image

  colorout::setOutputColors(
  
  index    = '\x1b[38;2;131;148;150m', 
  normal   = '\x1b[38;2;238;232;213m', 

  number   = '\x1b[38;2;181;137;0m', 
  negnum   = '\x1b[38;2;203;75;22m', 
  zero     = '\x1b[38;2;131;148;150m', zero.limit = 0.01,
  infinite = '\x1b[38;2;211;54;130m', 

  string   = '\x1b[38;2;133;153;0m', 
  date     = '\x1b[38;2;211;54;130m', 
  const    = '\x1b[38;2;108;113;196m',
  
  true     = '\x1b[38;2;133;153;0m',
  false    = '\x1b[38;2;220;50;47m',

  warn     = '\x1b[38;2;181;137;0m', 
  stderror = '\x1b[38;2;211;54;130m', error = '\x1b[38;2;211;54;130m',

  verbose  = TRUE
)

@wmacnair
Copy link

wmacnair commented Feb 5, 2022

Hey, thanks @jalvesaq for the awesome package and others for making the nice colour schemes easily accessible.

I'm trying to use the gruvbox scheme from @sjlva, however I'm getting strange results. I run the call to setOutputColors, and the output both has the wrong colours and unwanted underlines:
image

I'm using:

  • macOS + iTerm2 + zsh
  • R4.1.2, colorout_1.2-2

I've spent a little while getting confused by ANSI code sequences, so I'm wondering whether e.g. zsh or macOS needs a different escape sequence, something like that?

Any thoughts appreciated. Thanks!
Will

@jalvesaq
Copy link
Owner

jalvesaq commented Feb 5, 2022

The color of text output in a terminal emulator can be defined in two different ways: (1) a single number choosing a color from a palette of 8, 16, or 256 colors; (2) the exact amount of red, green, and blue light, know as true color. The gruvbox scheme defines true colors. So, you have to find a way of enabling true color support in your terminal.

@wmacnair
Copy link

wmacnair commented Feb 7, 2022

Thanks for the pointer, I'll look into that.

@armandfavrot
Copy link

Is there anyone using a light theme (light background color) who could share it ?

@aravezskinteeth
Copy link

I wish someone could make one for catppuccin mocha color scheme. I would do it myself, but I'm busy with other things. Also, I'm not experienced with this stuff and would require some days to understand it first.

@jalvesaq
Copy link
Owner

@aravezskinteeth, I will explain one line from the Gruvbox theme above:

  index    = '\x1b[38;2;215;153;33m', #

\x1b[38;2; = Begin of true color definition (foreground)
m = End of true color definition
215 = red color intensity
153 = green color intensity
33 = blue color intensity

The intensity of red, green, and blue colors goes from 0 to 255 which results in about 16 million colors (nicknamed "true color"). You can take a screenshot of something highlighted with your preferred color scheme, open the image in an application such as Gimp, and use the dropper tool to get the red, green, and blue values of each color.

@aravezskinteeth
Copy link

That makes so much more sense. Thank you.

@life00
Copy link

life00 commented Feb 24, 2024

@aravezskinteeth I am using catppuccin and I made a colorscheme from the above examples with a basic ansi+color comments. If you want to change something I recommend you use https://github.com/fidian/ansi to view your terminal color codes. To view how it looks change verbose to true.

colorout::setOutputColors(
  
  index    = 8, # gray
  normal   = 7, # text

  number   = 4, # blue
  negnum   = 16, # orange
  zero     = 6, zero.limit = 1, # light blue
  infinite = 5, # pink

  string   = 3, # yellow
  date     = 7, # text
  const    = 16, # orange,
  
  true     = 2, # green
  false    = 1, # red

  warn     = 16, # orange
  stderror = 1, # red
  error = 1, # red

  verbose  =  FALSE
)

@aravezskinteeth
Copy link

@aravezskinteeth I am using catppuccin and I made a colorscheme from the above examples with a basic ansi+color comments. If you want to change something I recommend you use https://github.com/fidian/ansi to view your terminal color codes. To view how it looks change verbose to true.

colorout::setOutputColors(
  
  index    = 8, # gray
  normal   = 7, # text

  number   = 4, # blue
  negnum   = 16, # orange
  zero     = 6, zero.limit = 1, # light blue
  infinite = 5, # pink

  string   = 3, # yellow
  date     = 7, # text
  const    = 16, # orange,
  
  true     = 2, # green
  false    = 1, # red

  warn     = 16, # orange
  stderror = 1, # red
  error = 1, # red

  verbose  =  FALSE
)

Looks good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants