Function Repository Resource:

HashHue

Source Notebook

Map an expression to a color based on a hash

Contributed by: Bob Sandheinrich

ResourceFunction["HashHue"][expr]

gives a Hue based on the expression expr.

ResourceFunction["HashHue"][expr,type]

uses the specified hash type.

Details and Options

ResourceFunction["HashHue"] accepts the same values of type as Hash. The default is "Expression".
ResourceFunction["HashHue"] always gives the same Hue for the same input expression.
Coloring complex expressions makes differences stand out much faster than viewing them normally.

Examples

Basic Examples (2) 

Get a color for an expression:

In[1]:=
ResourceFunction["HashHue"]["Hello World"]
Out[1]=

Sometime differences in values are hard to see visually:

In[2]:=
values = RandomReal[10^7, WorkingPrecision -> 20] + {0, 0, 0, 10^3, 0,
    0, 0, 0}
Out[2]=

Make different values standout visually:

In[3]:=
Style[#, ResourceFunction["HashHue"][#]] & /@ values
Out[3]=

Scope (3) 

Data with text fields can take up space:

In[4]:=
policeData = ResourceData["Urbana Police Incidents"][
  1 ;; 50, {"TimeOccurred", "AddressStreet", "CrimeDescription"}]
Out[4]=

Instead, visualize the data with colors where identical values always match:

In[5]:=
policeData[All, {"CrimeDescription" -> ResourceFunction["HashHue"]}]
Out[5]=

Create an index for interpreting the color:

In[6]:=
AssociationMap[ResourceFunction["HashHue"], DeleteDuplicates[policeData[All, "CrimeDescription"]]]
Out[6]=

Applications (1) 

Create a docked cell that shows your current Wolfram Language status:

In[7]:=
statusCell[] := Cell[BoxData[
   ToBoxes[Grid[{Style[#, ResourceFunction[
          "HashHue"][#]] & /@ {$VersionNumber, $CloudConnected, $WolframID}}, Spacings -> 5]]], "Output"]
In[8]:=
SetOptions[
 CreateDocument[{TextCell["Live Notebook Status", "Section"]}], DockedCells -> {statusCell[]}]

Version History

  • 1.0.0 – 14 August 2019

Author Notes

Extending this to set all HSB (or RGB) values in a smart way would extend the used color space and increase the typical distance between results.

License Information