Discrete Color bar settings

AFNI version info (afni -ver):
macos_10.12_local: Mar 5 2024 (Version AFNI_24.0.10

Hi,
Is there any way to change the colors or add more colors in the discrete colorbar?
The custom color palette can be chosen only from the continious '**' setting and does not affect the discrete settings.

Best,
Xin

Hi, Xin-

AFNI recognizes those specially named colors, and in many cases hex-defined values (like "#000000", etc.) and even the fancy matplotlib-color names ("dodgerblue", etc. here).

At present, I don't think you can create an arbitrarily colorized discrete colorbar there. But, you can create your own colorbar, and you can make it in a couple different ways so it could be either discrete or smooth. In your case, if you want 7 color blocks with some non-available colors, you could make a 256-valued colorbar with colors defined in chunks, and load either load it in through the GUI (button clicking) or via the AFNI_COLORSCALE_xx environment variable, which is described here. Personally, the latter seems nicer to me. NB: the description of ways to add your own colorbars has some helpful suggestions for continuous ones, as well.

So, if you wanted a discrete colorbar of 7 colors, you could make it in the following kind of file:

  • top line: A name for the colorbar (no spaces)
  • 256 lines of the hex values you want
Discrete-cbar-A
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffff00
#ffaa00
#ff0000
#aa00aa
#aa00aa
#aa00aa
#aa00aa
#aa00aa
#aa00aa
#aa00aa
#aa00aa
#aa00aa
#aa00aa
#aa00aa
#aa00aa
#aa00aa
#aa00aa
#aa00aa
#aa00aa
#aa00aa
#aa00aa
#ff0000
#ff0000
#ff0000
#ff0000
#ff0000
#ff0000
#ff0000
#ff0000
#ff0000
#ff0000
#ff0000
#ff0000
#ff0000
#ff0000
#ff0000
#ff0000
#ff0000
#ff0000
#ff0000
#ff0000
#ff0000
#ff0000
#ff0000
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#ff6347
#ff6347
#ff6347
#ff6347
#ff6347
#ff6347
#ff6347
#ff6347
#ff6347
#ff6347
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371

... and here's another one, using some matplotlib colorbar names:

Discrete-cbar-B
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
dodgerblue
#ffaa00
#ff0000
#aa00aa
#aa00aa
#aa00aa
#aa00aa
#aa00aa
#aa00aa
#aa00aa
#aa00aa
#aa00aa
#aa00aa
#aa00aa
#aa00aa
#aa00aa
#aa00aa
#aa00aa
#aa00aa
#aa00aa
#aa00aa
#ff0000
#ff0000
#ff0000
#ff0000
#ff0000
#ff0000
#ff0000
#ff0000
#ff0000
#ff0000
#ff0000
#ff0000
#ff0000
#ff0000
#ff0000
#ff0000
#ff0000
#ff0000
#ff0000
#ff0000
#ff0000
#ff0000
#ff0000
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#0000ff
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#6a5acd
#ff6347
#ff6347
#ff6347
#ff6347
#ff6347
#ff6347
#ff6347
#ff6347
#ff6347
#ff6347
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371
#3cb371

Then, open up your ~/.afnirc file for setting environment variables, and add pointers to each of those files using the full path, which on my system meant I put (starting enumeration at "04" because I had some previously existing colorbars):

AFNI_COLORSCALE_04 = /home/ptaylor/AFNI_data6/FT_analysis/FT/cbar4.txt
AFNI_COLORSCALE_05 = /home/ptaylor/AFNI_data6/FT_analysis/FT/cbar5.txt

Then, when I open up AFNI, and have an overlay, I can right click on the colorbar and select one of my fancy new colorbars, finding the name of each I had put in the top line. Those look like this here:


(I see now that I clicked "Pos?" on one of those images, so the ranges of values are different, but the colorbars work the same.)

So, the math part of this would be calculating what fraction of 256 lines of colors you want to be a given color. The ranges can be whatever you want. Frankly, though, I would find this easier than moving the little ranges around in the discrete-bar definer.

How does that seem?

--pt

Oh, and here's another way to go---use @chauffeur_afni. Example D provides a path forward for specifying a sequence of values and colors to make a discrete colorbar. So, the comments for the example are worth noting:

  D) Fun way to enter a colorbar-- note all the '-cbar*' options
     working together here, and the way they are used to make a
     discrete cbar.  (You might also enjoy the '-colorscale_idx_file ..'
     option, as another way to enter your own colorbar; colors
     entered there are *not* discrete regions, but get blended
     together.)
     NB 1: now you can replace the "" with the keyword EMPTY, to make
           scripting easier (needing to keep quotes around can be a pain).
     NB 2: the string following cbar probably can*not* be split into
           multiple lines with continuation-of-line chars.  Mi dispiace.

... and then run the command (this works "as is" in the AFNI_data6/FT_analysis/FT directory of the AFNI Bootcamp data):

    @chauffeur_afni                       \
        -ulay FT_anat+orig.               \
        -olay FT_anat+orig.               \
        -func_range_perc 95               \
        -prefix AAA                       \
        -pbar_saveim BBB.jpg              \
        -pbar_posonly                     \
        -cbar_ncolors 6                   \
        -cbar_topval ""                   \
        -cbar "1000=yellow 800=cyan 600=rbgyr20_10 400=rbgyr20_08 200=rbgyr20_05 100=rbgyr20_03 0=none"

... which produces the following montaged images, including the colorbar itself:




BBB

Here again you could use the matplotlib-named colors from above ("dodgerblue", "chartreuse", etc.).

--pt

Hi PT,

Thanks for your detailed explanation!

I suppose the @chauffeur_afni function with cbar option does not accept the hex code mapping. The help page:

-cbar CCC. CCC can be any of the cbars in the standard AFNI list.

The first solution works well.

Best,
Xin

Hi, Xin-

Actually, the -cbar option does helpfully take both Matplotlib colornames and hex values. I will update the program help.

--pt

Hi PT,

I attempted several different -cbar inputs, using both matplotlib's color maps and hex codes. However, the output always using the default color bar in AFNI.

APR_all_overlap_LD.nii contains integer values ranging from 1 to 7 which corresponds to three conditions and their overlap.

@chauffeur_afni                       \
        -ulay ${dir}/MNI152_T1_2009c+tlrc               \
        -olay ${dir}/APR_all_overlap_LD.nii               \
        -prefix test                       \
        -pbar_saveim test.jpg              \
        -pbar_posonly                     \
        -cbar_ncolors 7                   \
        -cbar_topval ""                   \
        -cbar "0=none 150=forestgreen 300=royalblue 300=red 450=cornflowerblue 600=yellow 750=mediumpurple 1000=darkorange"

Best,
Xin

Hi, Xin-

There seemed to be 2 issues there:

  1. It appears you should always count down in descending order of numbers, like in the example.
  2. The number "300" appeared twice, and seemed to confuse the colorbar generator.

When I made those adjustments (I changed one 300->350, arbitrarily):

@chauffeur_afni                       \
        -ulay FT_anat+orig             \
        -olay FT_anat+orig              \
        -prefix TEST3                       \
        -pbar_saveim TEST3.jpg              \
        -pbar_posonly                     \
        -cbar_ncolors 7                   \
        -cbar_topval ""                   \
        -cbar "1000=darkorange 750=mediumpurple  600=yellow 450=cornflowerblue  350=red 300=royalblue 150=forestgreen  0=none "

... it seemed to work fine:


TEST3

--pt

Setting up the discrete color palettes in .afnirc is described in the manuscript from the ancient seers -- AFNI file: README.setup

1 Like

Wow! So excited! This is the first time I met you!

It works well ! I've added some cool colors for further use!
Thanks!

Best,
Xin

1 Like

Hi PT,

Thanks for your help!
Everything works fine now~

Best,
Xin