Skip to content

Instantly share code, notes, and snippets.

@standarddeviant
Created January 20, 2022 15:13
Show Gist options
  • Save standarddeviant/c94de85a6b00479ef27c09bac24139a3 to your computer and use it in GitHub Desktop.
Save standarddeviant/c94de85a6b00479ef27c09bac24139a3 to your computer and use it in GitHub Desktop.
Viridis Approximation for RGB565
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "5b090acd",
"metadata": {},
"outputs": [],
"source": [
"viridis_approx = [\n",
" \"#440154ff\", \"#440558ff\", \"#450a5cff\", \"#450e60ff\", \"#451465ff\", \"#461969ff\",\n",
" \"#461d6dff\", \"#462372ff\", \"#472775ff\", \"#472c7aff\", \"#46307cff\", \"#45337dff\",\n",
" \"#433880ff\", \"#423c81ff\", \"#404184ff\", \"#3f4686ff\", \"#3d4a88ff\", \"#3c4f8aff\",\n",
" \"#3b518bff\", \"#39558bff\", \"#37598cff\", \"#365c8cff\", \"#34608cff\", \"#33638dff\",\n",
" \"#31678dff\", \"#2f6b8dff\", \"#2d6e8eff\", \"#2c718eff\", \"#2b748eff\", \"#29788eff\",\n",
" \"#287c8eff\", \"#277f8eff\", \"#25848dff\", \"#24878dff\", \"#238b8dff\", \"#218f8dff\",\n",
" \"#21918dff\", \"#22958bff\", \"#23988aff\", \"#239b89ff\", \"#249f87ff\", \"#25a186ff\",\n",
" \"#25a584ff\", \"#26a883ff\", \"#27ab82ff\", \"#29ae80ff\", \"#2eb17dff\", \"#35b479ff\",\n",
" \"#3cb875ff\", \"#42bb72ff\", \"#49be6eff\", \"#4ec16bff\", \"#55c467ff\", \"#5cc863ff\",\n",
" \"#61c960ff\", \"#6bcc5aff\", \"#72ce55ff\", \"#7cd04fff\", \"#85d349ff\", \"#8dd544ff\",\n",
" \"#97d73eff\", \"#9ed93aff\", \"#a8db34ff\", \"#b0dd31ff\", \"#b8de30ff\", \"#c3df2eff\",\n",
" \"#cbe02dff\", \"#d6e22bff\", \"#e1e329ff\", \"#eae428ff\", \"#f5e626ff\", \"#fde725ff\",\n",
"];"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "8c624893",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"viridis_approx_565 = [\n",
" 0x400a,0x402b,0x404b,0x406c,0x40ac,0x40cd,\n",
" 0x40ed,0x410e,0x412e,0x416f,0x418f,0x418f,\n",
" 0x41d0,0x41f0,0x4210,0x3a30,0x3a51,0x3a71,\n",
" 0x3a91,0x3ab1,0x32d1,0x32f1,0x3311,0x3311,\n",
" 0x3331,0x2b51,0x2b71,0x2b91,0x2bb1,0x2bd1,\n",
" 0x2bf1,0x23f1,0x2431,0x2431,0x2451,0x2471,\n",
" 0x2491,0x24b1,0x24d1,0x24d1,0x24f0,0x2510,\n",
" 0x2530,0x2550,0x2550,0x2d70,0x2d8f,0x35af,\n",
" 0x3dce,0x45ce,0x4ded,0x4e0d,0x562c,0x5e4c,\n",
" 0x664c,0x6e6b,0x766a,0x7e89,0x8689,0x8ea8,\n",
" 0x96a7,0x9ec7,0xaec6,0xb6e6,0xbee6,0xc6e5,\n",
" 0xcf05,0xd705,0xe705,0xef25,0xf724,0xff24,\n",
"]\n"
]
}
],
"source": [
"def color565(r, g, b):\n",
" return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3)\n",
"\n",
"print('viridis_approx_565 = [')\n",
"for ix, itm in enumerate(viridis_approx):\n",
" r, g, b = int(itm[1:3], 16), int(itm[3:5], 16), int(itm[5:7], 16)\n",
" c565 = color565(r,g,b)\n",
" if 0 == (ix % 6):\n",
" print(' ', end='')\n",
" print(f'{hex(c565)},', end='')\n",
" if 5 == (ix % 6):\n",
" print('')\n",
"print(']')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ddb39ef4",
"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.9.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment