βDehexβ, from Wiktionary:
To remove a hex (a spell, especially an evil spell).
An R package containing simple functions and a Shiny app to help me train myself to βreadβ a colour from its hex code. Iβm colourblind (a deuteranope) so this might be a useful skill. Read the accompanying blog post for more info.
The DeSandro method
David DeSandro gave a talk at dotCSS 2018 about βreadingβ colour hex codes by eye to get a colour like βdark saturated orangeβ (thanks MaΓ«lle).
There are five steps:
- Simplify from a six- to a three-digit hex code
- Create an RGB bar chart from the short hex code
- Assess hue from the chart βshapeβ
- Assess saturation from the RGB range
- Assess lightness from the RGB total
This package contains functions that guide you through that process and can βsolveβ the hex code for you. Iβve also put them in a Shiny app thatβs bundled with the package.
Install
You can install the development version from GitHub.
remotes::install_github("matt-dray/dehex")
App
The package contains a Shiny app that generates a random colour hex code
and walks you through the steps of DeSandroβs method using functions
from the {dehex} package. You can run it with dh_app()
. It requires
the {shiny} and {bslib} packages, which can both be installed with
install.packages()
. Hereβs a preview:
Functions
Three-digit shorthand
First, convert a full six-character hex code to its three-character shortcode. This takes the first value from each pair of characters.
long <- "#6BCFFA"
short <- dehex::dh_shorten(long)
short
# [1] "#6CF"
You could also generate a random one with dh_random()
.
Bar chart
Use dh_graph()
to print to the console a bar chart that shows the
decimal values of red (R), green (G) and blue (B) for your shortened hex
code.
dehex::dh_graph(short)
# #6CF
# R ββββββββββββββββ H 1
# G ββββββββββββββββ H 2
# B ββββββββββββββββ H 3
# S ββββββββββββββββ
# L ββββββββββββββββ
The RGB columns are printed in colour in RStudio, thanks to the
{crayon} package. (You can turn this
off by setting the argument crayon = FALSE
.)
Note that the chart is adorned by extra information that tells you
something about the hue (H, i.e. the relative βrankβ of each RGB
channel), saturation (S, i.e. the RGB range) and lightness (L, i.e. the
RGB average). You can remove these guides by setting the adorn_*
arguments to FALSE
.
The idea is to compare this to a set of guides that provide rough categorisations of hue, saturation and lightness to generate an English phrase.
Guides
Hue from shape
To assess the hue of your hex code, compare its RGB profile from
dh_graph()
to the guide provided by dh_guide("H")
. The exact amounts
donβt matter; itβs the relative values of RGB that we care about. For
this reason, the end of each bar shows you the relative rank()
of each
channel (the smallest value is ranked β1β, a tie between the two
smallest gives them both a rank of β1.5β, etc).
Well, technically my ranking system here has a wider tolerance for ties. So RGB values of 15, 6, and 5 will result in a tie between green and blue because their difference is below a certain threshold (2, but subject to testing).
Click to see the hue guides
dehex::dh_guide("H")
# red
# R ββββββββββββββββ H 3
# G ββββββββββββββββ H 1.5
# B ββββββββββββββββ H 1.5
#
# green
# R ββββββββββββββββ H 1.5
# G ββββββββββββββββ H 3
# B ββββββββββββββββ H 1.5
#
# blue
# R ββββββββββββββββ H 1.5
# G ββββββββββββββββ H 1.5
# B ββββββββββββββββ H 3
#
# yellow
# R ββββββββββββββββ H 2.5
# G ββββββββββββββββ H 2.5
# B ββββββββββββββββ H 1
#
# cyan
# R ββββββββββββββββ H 1
# G ββββββββββββββββ H 2.5
# B ββββββββββββββββ H 2.5
#
# magenta
# R ββββββββββββββββ H 2.5
# G ββββββββββββββββ H 1
# B ββββββββββββββββ H 2.5
#
# orange
# R ββββββββββββββββ H 3
# G ββββββββββββββββ H 2
# B ββββββββββββββββ H 1
#
# chartreuse
# R ββββββββββββββββ H 2
# G ββββββββββββββββ H 3
# B ββββββββββββββββ H 1
#
# aquamarine
# R ββββββββββββββββ H 1
# G ββββββββββββββββ H 3
# B ββββββββββββββββ H 2
#
# azure
# R ββββββββββββββββ H 1
# G ββββββββββββββββ H 2
# B ββββββββββββββββ H 3
#
# violet
# R ββββββββββββββββ H 2
# G ββββββββββββββββ H 1
# B ββββββββββββββββ H 3
#
# rose
# R ββββββββββββββββ H 3
# G ββββββββββββββββ H 1
# B ββββββββββββββββ H 2
#
# grey
# R ββββββββββββββββ H 2
# G ββββββββββββββββ H 2
# B ββββββββββββββββ H 2
Saturation from range
To assess the saturation of your hex code, compare its RGB profile from
dh_graph()
to the guide provided by dh_guide("S")
. A larger RGB
range means a more saturated colour.
Click to see the saturation guides
dehex::dh_guide("S")
# saturated
# R ββββββββββββββββ
# G ββββββββββββββββ
# B ββββββββββββββββ
# S ββββββββββββββββ
#
# washed
# R ββββββββββββββββ
# G ββββββββββββββββ
# B ββββββββββββββββ
# S ββββββββββββββββ
#
# muted
# R ββββββββββββββββ
# G ββββββββββββββββ
# B ββββββββββββββββ
# S ββββββββββββββββ
#
# grey
# R ββββββββββββββββ
# G ββββββββββββββββ
# B ββββββββββββββββ
# S ββββββββββββββββ
Lightness from total
To assess the lightness of your hex code, compare its RGB profile from
dh_graph()
to the guide provided by dh_guide("L")
. A higher total
RGB means itβs lighter in colour, but Iβve chosen to show the mean value
as a guide.
Click to see the lightness guides
dehex::dh_guide("L")
# light
# R ββββββββββββββββ
# G ββββββββββββββββ
# B ββββββββββββββββ
# L ββββββββββββββββ
#
# middle
# R ββββββββββββββββ
# G ββββββββββββββββ
# B ββββββββββββββββ
# L ββββββββββββββββ
#
# dark
# R ββββββββββββββββ
# G ββββββββββββββββ
# B ββββββββββββββββ
# L ββββββββββββββββ
The solution
Luckily, dh_solve()
returns the βanswerβ for your colour as a text
string. So, for our input #6CF, the βanswerβ is:
dehex::dh_solve(short)
# [1] "light washed azure"
Of course, you could just use this function to get a simple way of communicating colour from hex codes without learning how to do it by βreadingβ a hex code yourself.
You can also ask to return the relevant bar charts that best describe the hue, saturation and lightness that led to the result.
dehex::dh_solve(short, graphs = TRUE)
# input code: #6CF
# R ββββββββββββββββ H 1
# G ββββββββββββββββ H 2
# B ββββββββββββββββ H 3
# S ββββββββββββββββ
# L ββββββββββββββββ
#
# hue: azure
# R ββββββββββββββββ H 1
# G ββββββββββββββββ H 2
# B ββββββββββββββββ H 3
#
# saturation: washed
# R ββββββββββββββββ
# G ββββββββββββββββ
# B ββββββββββββββββ
# S ββββββββββββββββ
#
# lightness: light
# R ββββββββββββββββ
# G ββββββββββββββββ
# B ββββββββββββββββ
# L ββββββββββββββββ
# [1] "light washed azure"
And if youβre wondering what the colour actually is, you can either set
swatch = TRUE
in dh_solve()
, or use dh_swatch()
to generate a plot
filled with that colour.
dehex::dh_swatch(short)
Code of Conduct
Please note that the {dehex} project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.