Skip to content

Instantly share code, notes, and snippets.

@kannes
Created September 25, 2018 13:28
Show Gist options
  • Save kannes/ac507b6afe2b19d5eec956ee9ae7b348 to your computer and use it in GitHub Desktop.
Save kannes/ac507b6afe2b19d5eec956ee9ae7b348 to your computer and use it in GitHub Desktop.
half working color distance comparison thingie
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"from itertools import combinations\n",
"from colormath.color_objects import sRGBColor, LabColor\n",
"from colormath.color_conversions import convert_color\n",
"from colormath.color_diff import delta_e_cie1976\n",
"from IPython.core.display import display, HTML"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"only sRGBColor seems to have hex conversions"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"def hex2lab(color: str):\n",
" \"#123456 -> LabColor(lab_l=21.042135383139815,lab_a=1.0561613155631033,lab_b=-24.104650549252526)\"\n",
" srgb = sRGBColor.new_from_rgb_hex(color) # not sure this is avoidable\n",
" lab = convert_color(srgb, LabColor)\n",
" return lab"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"def color2hex(color):\n",
" \"takes a colormath color object and returns the hex string representation\"\n",
" srgb = convert_color(color, sRGBColor)\n",
" hex_string = srgb.get_rgb_hex()\n",
" return hex_string"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"def colored_box(color):\n",
" \"takes a colormath color object and returns a small html span in its color\"\n",
" hex_string = color2hex(color)\n",
" return f\"\"\"<span style=\"color:{hex_string};\">■</span>\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[LabColor(lab_l=53.23896002513146,lab_a=80.09045298802708,lab_b=67.2013836595967),\n",
" LabColor(lab_l=51.38500546895024,lab_a=77.09154971393566,lab_b=25.398963659412665),\n",
" LabColor(lab_l=91.11396352922449,lab_a=-48.083199948639965,lab_b=-14.138634095166536),\n",
" LabColor(lab_l=21.042135383139815,lab_a=1.0561613155631033,lab_b=-24.104650549252526),\n",
" LabColor(lab_l=20.981344399233414,lab_a=0.6362340481182449,lab_b=-23.537416545701884)]"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"colors = \"#ff0000, #f01254, #00ffff, #123456, #123455\"\n",
"colors = [color.strip() for color in colors.split(\",\")]\n",
"colors = [hex2lab(color) for color in colors]\n",
"colors"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre>#ff0000 <span style=\"color:#ff0000;\">■</span> <span style=\"color:#f01254;\">■</span> #f01254 🠪 042</pre>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<pre>#ff0000 <span style=\"color:#ff0000;\">■</span> <span style=\"color:#00ffff;\">■</span> #00ffff 🠪 156</pre>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<pre>#ff0000 <span style=\"color:#ff0000;\">■</span> <span style=\"color:#123456;\">■</span> #123456 🠪 125</pre>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<pre>#ff0000 <span style=\"color:#ff0000;\">■</span> <span style=\"color:#123455;\">■</span> #123455 🠪 125</pre>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<pre>#f01254 <span style=\"color:#f01254;\">■</span> <span style=\"color:#00ffff;\">■</span> #00ffff 🠪 137</pre>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<pre>#f01254 <span style=\"color:#f01254;\">■</span> <span style=\"color:#123456;\">■</span> #123456 🠪 096</pre>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<pre>#f01254 <span style=\"color:#f01254;\">■</span> <span style=\"color:#123455;\">■</span> #123455 🠪 096</pre>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<pre>#00ffff <span style=\"color:#00ffff;\">■</span> <span style=\"color:#123456;\">■</span> #123456 🠪 086</pre>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<pre>#00ffff <span style=\"color:#00ffff;\">■</span> <span style=\"color:#123455;\">■</span> #123455 🠪 086</pre>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<pre>#123456 <span style=\"color:#123456;\">■</span> <span style=\"color:#123455;\">■</span> #123455 🠪 001</pre>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"for color1, color2 in combinations(colors, r=2):\n",
" delta_e = delta_e_cie1976(color1, color2)\n",
" display(HTML(f\"<pre>{color2hex(color1)} {colored_box(color1)} {colored_box(color2)} {color2hex(color2)} 🠪 {delta_e:03.0f}</pre>\"))"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"G.add_edge('#ff0000', '#f01254', weight=41.95083892147483)"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"G.add_edge('#ff0000', '#00ffff', weight=156.4583001502619)"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"G.add_edge('#ff0000', '#123456', weight=124.97938494064857)"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"G.add_edge('#ff0000', '#123455', weight=124.848133890404)"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"G.add_edge('#f01254', '#00ffff', weight=137.15075536739278)"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"G.add_edge('#f01254', '#123456', weight=95.66962879438853)"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"G.add_edge('#f01254', '#123455', weight=95.73174608029213)"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"G.add_edge('#00ffff', '#123456', weight=86.16298166353923)"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"G.add_edge('#00ffff', '#123455', weight=85.90986338337036)"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"G.add_edge('#123456', '#123455', weight=0.7083705728304242)"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"for color1, color2 in combinations(colors, r=2):\n",
" delta_e = delta_e_cie1976(color1, color2)\n",
" display(HTML(f\"G.add_edge('{color2hex(color1)}', '{color2hex(color2)}', weight={delta_e})\"))"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<module 'matplotlib.pyplot' from '/usr/lib/python3.7/site-packages/matplotlib/pyplot.py'>"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Author: Aric Hagberg (hagberg@lanl.gov)\n",
"import matplotlib.pyplot as plt\n",
"import networkx as nx\n",
"\n",
"G = nx.Graph()\n",
"\n",
"G.add_edge('#ff0000', '#f01254', weight=41.95083892147483)\n",
"G.add_edge('#ff0000', '#00ffff', weight=156.4583001502619)\n",
"G.add_edge('#ff0000', '#123456', weight=124.97938494064857)\n",
"G.add_edge('#ff0000', '#123455', weight=124.848133890404)\n",
"G.add_edge('#f01254', '#00ffff', weight=137.15075536739278)\n",
"G.add_edge('#f01254', '#123456', weight=95.66962879438853)\n",
"G.add_edge('#f01254', '#123455', weight=95.73174608029213)\n",
"G.add_edge('#00ffff', '#123456', weight=86.16298166353923)\n",
"G.add_edge('#00ffff', '#123455', weight=85.90986338337036)\n",
"G.add_edge('#123456', '#123455', weight=0.7083705728304242)\n",
"\n",
"pos = nx.spring_layout(G) # positions for all nodes\n",
"\n",
"# nodes\n",
"nx.draw_networkx_nodes(G, pos, node_size=700)\n",
"\n",
"# edges\n",
"nx.draw_networkx_edges(G, pos, width=6)\n",
"\n",
"# labels\n",
"nx.draw_networkx_labels(G, pos, font_size=10, font_family='sans-serif')\n",
"\n",
"plt.axis('off')\n",
"plt"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": true
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment