Re: Retrieving color names and hex values using list-colors-display for coding purposes

Eli Zaretskii <[email protected]> Sun, 19 Jul 2026 07:27:12 +0300
Newsgroups gmane.emacs.help
Message-ID <[email protected]>
> Date: Sat, 18 Jul 2026 22:21:48 +0000
> From: Heime <[email protected]>
> Cc: [email protected], [email protected], [email protected]
> 
> On Saturday, July 18th, 2026 at 7:29 PM, Eli Zaretskii <[email protected]> wrote:
> 
> > > Date: Sat, 18 Jul 2026 01:11:38 +0000
> > > From: Heime <[email protected]>
> > > Cc: [email protected], [email protected], [email protected]
> > >
> > > On Saturday, July 18th, 2026 at 3:38 AM, Eli Zaretskii <[email protected]> wrote:
> > >
> > > > > list-colors-display is an interactive function in facemenu.el, which is
> > > > > part of GNU Emacs.  It should look at the range of values and determine
> > > > > whether #RRRRGGGGBBBB should be used as display.
> > > >
> > > > Not necessarily.  It works with a list of 256 colors, so the full list
> > > > available on the platform is not relevant.
> > >
> > > I can take the RGB values, for instance 60000, get 60000/65536, which is
> > > 0.915, and reduce to 256 colours via 0.915*256 to get 234.
> > > Such a mapping from 16-bit values to 8-bit values should be ok.
> > 
> > Not necessarily.  But why should you care about such conversions?  Why
> > not let Emacs do it for you when necessary?
> 
> What will Emacs do?   I only need the rgb or hex values (in range 0-1) 
> to decide whether the contrast ratio is suitable to use.  I do not want 
> just to select a colour name and apply without ensuring the contrast ratio
> is appropriate for the user.

If you need to decide whether two colors are sufficiently distinct,
use the color-cie-de2000 function.  You can see an example of its
usage in shr-color.el:shr-color-visible.  There's no need to worry
about how many digits are in the hex representation when you use that.

> > > Could I rely on the colour names from (defined-colors).
> > 
> > That will give you the colors supported directly by the platform, not
> > the full list of colors you could potentially use.  
> 
> > For example, if the terminal only supports 16 colors, most of the names 
> > you see in the window produced by list-colors-display on a typical GUI 
> > frame will not be in the list returned by defined-colors.
> 
> Is it because the GUI Emacs and Terminal Emacs do not have the same colour
> capabilities?

Yes.  And because Emacs automatically approximates every color as best
as possible for each terminal.

> I have a difficulty.  How can I define a face that would be supported 
> by Emacs on some terminal?

Any color definition of a face will always be supported on all
terminals.  You don't need to worry about that, as Emacs solves the
problem automatically, by choosing the closest color supported by the
terminal.