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

From rgb to xterm256 #13

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

nanobowers
Copy link
Contributor

Hello @mrkn , I needed some methods for converting an rgb value into the closest xterm256 color - this package previously only contained the methods for xterm256->to_rgb. I added support as a class method: Colors::Xterm256.from_rgb(rgb), but can move to an instance method on Colors::RGB (e.g. Colors::RGB#to_xterm256) if that is more appropriate.

I plan to use the two way conversion in unicode_plot.rb.

@mrkn
Copy link
Member

mrkn commented May 30, 2021

Thank you for your contribution. But, I cannot merge this now because I still consider that how to find the closest color.
I suspect that Euclidean distance in RGB color space is not appropriate as the distance function to find the closest color.

I guess that one candidate is LUV color space with the appropriate reference white point.

I need more time for studying color theory to decide the appropriate method.

@nanobowers
Copy link
Contributor Author

Hello @mrkn , thank you for the consideration.

I based the xterm256 approximation on the algorithm found in tmux:
https://github.com/tmux/tmux/blob/master/colour.c

Per the description below, the xterm256 color palette is extremely coarse. Because of that, I was hoping that it would be close enough:

         * Convert an RGB triplet to the xterm(1) 256 colour palette.
         *
         * xterm provides a 6x6x6 colour cube (16 - 231) and 24 greys (232 - 255). We
         * map our RGB colour to the closest in the cube, also work out the closest
         * grey, and use the nearest of the two.
         *
         * Note that the xterm has much lower resolution for darker colours (they are
         * not evenly spread out), so our 6 levels are not evenly spread: 0x0, 0x5f
         * (95), 0x87 (135), 0xaf (175), 0xd7 (215) and 0xff (255). Greys are more
         * evenly spread (8, 18, 28 ... 238).

@mrkn
Copy link
Member

mrkn commented May 31, 2021

I know tmux uses RGB color space to determine the closest color, and I don't want to follow tmux.

In unicode_plot, I would like to derive the closest color using a distance space where the distance between two colors that the human eye perceives as close is small. I guess CIELUV colorspace is more appropriate than the other color spaces.

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

Successfully merging this pull request may close these issues.

2 participants