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

Heime <[email protected]> Sun, 19 Jul 2026 11:27:13 +0000
Newsgroups gmane.emacs.help
Message-ID <Qn5jqpVdu1C_IC0v14vl_jKfaQI9kbpjRVL0DrV-Zab2G-ng1PhranusJjKCun59lgf8dUqaNezWhXDFjjeuk_J4gT19M1FdKdpQJiN21Gs=@protonmail.com>
On Sunday, July 19th, 2026 at 4:27 PM, Eli Zaretskii <[email protected]> wrote:

> > 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]> w=
rote:
> >
> > > > 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]=
g> 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 d=
etermine
> > > > > > 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, whi=
ch 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?  Wh=
y
> > > 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 ra=
tio
> > is appropriate for the user.
>=20
> 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.
>=20
> > > > 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 nam=
es
> > > you see in the window produced by list-colors-display on a typical GU=
I
> > > 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 col=
our
> > capabilities?
>=20
> Yes.  And because Emacs automatically approximates every color as best
> as possible for each terminal.
>=20
> > I have a difficulty.  How can I define a face that would be supported
> > by Emacs on some terminal?
>=20
> 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.
=20
Would you suggest for me to define a face using a colour name from
list-colors-display on an Emacs GUI frame?   What does one do when
the name is not in the list returned by defined-colors.

You were correct in the statement that using colours is complicated.
Initially, I had the plan to define actual colours for each terminal
capability, detect which to use and apply accordingly during the Emacs
initialisation phase.