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

listing colortable for "export" #16

Closed
teuben opened this issue Jul 21, 2020 · 4 comments · Fixed by #21
Closed

listing colortable for "export" #16

teuben opened this issue Jul 21, 2020 · 4 comments · Fixed by #21
Labels
enquiry This issue involves an enquiry or question

Comments

@teuben
Copy link

teuben commented Jul 21, 2020

I use RGB text tables in NEMO, 3 columns, and I believe ds9 as well. So for the community it would be useful to have some kind of script that does:
mkrgbtable cmr.rainforest > rainforest.rgb
I know they are also deep inside the python tree. I wrote something that might be of some use to clean up a but and feel feel to adopt that one. Mine is called mklut in NEMO, e.g..
https://github.com/teuben/nemo/blob/master/data/lut/mklut
As you will see from mine, I also support the generic MPL names, as well as yours, in the cmr. namespace.

@1313e
Copy link
Owner

1313e commented Jul 22, 2020

Hi @teuben,

great to see you like the CMasher colormaps.

To obtain the RGB values of a colormap, you can simply do np.array(cmap.colors) as long as cmap is an instance of ListedColormap (all colormaps in CMasher are).
This will return the RGB tuples with normalized values of all colors in that colormap.
Another thing you can do (which works for all colormaps) is cmap(np.arange(cmap.N))[:, :3], which does the same thing.
In case you want the 8-bit RGB values instead, you can use np.array(np.rint(rgb*255), dtype=int) with rgb being the array of RGB tuples obtained using either method above.

Having said that, CMasher provides source files for all colormaps in this directory.
Every colormap has its own directory here with source files.
One of those files includes a text-file that contains the 8-bit RGB values used for that colormap, which are determined using the functions I mentioned above.
Of course, MPL colormaps do not have this, so you would have to calculate them yourself.

However, I do have a question: What is the purpose of the file you linked?
It looks like you can give it the name of a colormap and it will search MPL for that colormap (and afterward it searches CMasher).
Then it prints out all the normalized RGB values of that colormap (btw, this can be done more easily by using print(cmap(np.arange(cmap.N))[:, :3]))?
I am guessing it is more or less a look-up table for colormap data?

PS: Checking for the name of an MPL colormap first and then checking if CMasher has one with that name, is a bit dangerous.
This is because MPL and CMasher can have colormaps with the same name, like ocean (this is the only one right now, but there might be more in the future).

@teuben
Copy link
Author

teuben commented Jul 22, 2020

the reason for the script is that I have a non-python programs that then can use this table. For example for this script you can read that example rainforest.rgb into ds9 using color -> colormap parameters -> file -> load colormap
Either native MPL or your CMR. I can't use the pythonic output from print(), i needed a simple 3 column ascii file.

PS The way I wrote the script, I believe "ocean" will catch the native one, if the user would want the cmr.ocean, they would need to supply the full name.

@1313e
Copy link
Owner

1313e commented Jul 22, 2020

the reason for the script is that I have a non-python programs that then can use this table. For example for this script you can read that example rainforest.rgb into ds9 using color -> colormap parameters -> file -> load colormap
Either native MPL or your CMR. I can't use the pythonic output from print(), i needed a simple 3 column ascii file.

If you want a simple 3 column ASCII file, then simply do the following:

rgb = cmap(np.arange(cmap.N))[:, :3]
np.savetxt("path/to/file.txt", rgb, fmt='%.8f')

or (if you want 8-bit RGB values):

rgb = cmap(np.arange(cmap.N))[:, :3]
rgb_8bit = np.rint(rgb*255)
np.savetxt("path/to/file.txt", rgb_8bit, fmt='%i')

In case you specifically want it printed, replace "path/to/file.txt" with sys.stdout and it will print it to the console in the same format as would be saved to file.

As I am already providing the RGB values of all colormaps in CMasher in separate text files (both with normalized floats and 8-bit values), I am not entirely sure what more I can do.

PS The way I wrote the script, I believe "ocean" will catch the native one, if the user would want the cmr.ocean, they would need to supply the full name.

Correct, which is why I am warning about it.
If you accept that one can say 'rainforest' instead of 'cmr.rainforest', they might expect that 'ocean' will give them 'cmr.ocean', even though it won't in this case.

@1313e 1313e added the enquiry This issue involves an enquiry or question label Jul 30, 2020
@1313e 1313e mentioned this issue Jul 30, 2020
5 tasks
@1313e
Copy link
Owner

1313e commented Aug 3, 2020

@teuben In the cli_tools branch, I am working on adding CLI tools to CMasher.
It will also be possible to obtain the RGB values of any colormap available in MPL (which, currently, will be MPL; CMasher; Cmocean; palettable; and colorcet, if they are installed) directly from the command-line with cmr rgbtable <cmap_name>.

@1313e 1313e linked a pull request Aug 11, 2020 that will close this issue
@1313e 1313e closed this as completed in #21 Aug 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enquiry This issue involves an enquiry or question
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants