Created
August 26, 2025 09:16
-
-
Save Leowbattle/d1fbe0fc7747dbaff247ea2dc3484fd9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "id": "bcdb2d98", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "import matplotlib" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "id": "6f7fe68f", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "['magma',\n", | |
| " 'inferno',\n", | |
| " 'plasma',\n", | |
| " 'viridis',\n", | |
| " 'cividis',\n", | |
| " 'twilight',\n", | |
| " 'twilight_shifted',\n", | |
| " 'turbo',\n", | |
| " 'berlin',\n", | |
| " 'managua',\n", | |
| " 'vanimo',\n", | |
| " 'Blues',\n", | |
| " 'BrBG',\n", | |
| " 'BuGn',\n", | |
| " 'BuPu',\n", | |
| " 'CMRmap',\n", | |
| " 'GnBu',\n", | |
| " 'Greens',\n", | |
| " 'Greys',\n", | |
| " 'OrRd',\n", | |
| " 'Oranges',\n", | |
| " 'PRGn',\n", | |
| " 'PiYG',\n", | |
| " 'PuBu',\n", | |
| " 'PuBuGn',\n", | |
| " 'PuOr',\n", | |
| " 'PuRd',\n", | |
| " 'Purples',\n", | |
| " 'RdBu',\n", | |
| " 'RdGy',\n", | |
| " 'RdPu',\n", | |
| " 'RdYlBu',\n", | |
| " 'RdYlGn',\n", | |
| " 'Reds',\n", | |
| " 'Spectral',\n", | |
| " 'Wistia',\n", | |
| " 'YlGn',\n", | |
| " 'YlGnBu',\n", | |
| " 'YlOrBr',\n", | |
| " 'YlOrRd',\n", | |
| " 'afmhot',\n", | |
| " 'autumn',\n", | |
| " 'binary',\n", | |
| " 'bone',\n", | |
| " 'brg',\n", | |
| " 'bwr',\n", | |
| " 'cool',\n", | |
| " 'coolwarm',\n", | |
| " 'copper',\n", | |
| " 'cubehelix',\n", | |
| " 'flag',\n", | |
| " 'gist_earth',\n", | |
| " 'gist_gray',\n", | |
| " 'gist_heat',\n", | |
| " 'gist_ncar',\n", | |
| " 'gist_rainbow',\n", | |
| " 'gist_stern',\n", | |
| " 'gist_yarg',\n", | |
| " 'gnuplot',\n", | |
| " 'gnuplot2',\n", | |
| " 'gray',\n", | |
| " 'hot',\n", | |
| " 'hsv',\n", | |
| " 'jet',\n", | |
| " 'nipy_spectral',\n", | |
| " 'ocean',\n", | |
| " 'pink',\n", | |
| " 'prism',\n", | |
| " 'rainbow',\n", | |
| " 'seismic',\n", | |
| " 'spring',\n", | |
| " 'summer',\n", | |
| " 'terrain',\n", | |
| " 'winter',\n", | |
| " 'Accent',\n", | |
| " 'Dark2',\n", | |
| " 'Paired',\n", | |
| " 'Pastel1',\n", | |
| " 'Pastel2',\n", | |
| " 'Set1',\n", | |
| " 'Set2',\n", | |
| " 'Set3',\n", | |
| " 'tab10',\n", | |
| " 'tab20',\n", | |
| " 'tab20b',\n", | |
| " 'tab20c',\n", | |
| " 'grey',\n", | |
| " 'gist_grey',\n", | |
| " 'gist_yerg',\n", | |
| " 'Grays',\n", | |
| " 'magma_r',\n", | |
| " 'inferno_r',\n", | |
| " 'plasma_r',\n", | |
| " 'viridis_r',\n", | |
| " 'cividis_r',\n", | |
| " 'twilight_r',\n", | |
| " 'twilight_shifted_r',\n", | |
| " 'turbo_r',\n", | |
| " 'berlin_r',\n", | |
| " 'managua_r',\n", | |
| " 'vanimo_r',\n", | |
| " 'Blues_r',\n", | |
| " 'BrBG_r',\n", | |
| " 'BuGn_r',\n", | |
| " 'BuPu_r',\n", | |
| " 'CMRmap_r',\n", | |
| " 'GnBu_r',\n", | |
| " 'Greens_r',\n", | |
| " 'Greys_r',\n", | |
| " 'OrRd_r',\n", | |
| " 'Oranges_r',\n", | |
| " 'PRGn_r',\n", | |
| " 'PiYG_r',\n", | |
| " 'PuBu_r',\n", | |
| " 'PuBuGn_r',\n", | |
| " 'PuOr_r',\n", | |
| " 'PuRd_r',\n", | |
| " 'Purples_r',\n", | |
| " 'RdBu_r',\n", | |
| " 'RdGy_r',\n", | |
| " 'RdPu_r',\n", | |
| " 'RdYlBu_r',\n", | |
| " 'RdYlGn_r',\n", | |
| " 'Reds_r',\n", | |
| " 'Spectral_r',\n", | |
| " 'Wistia_r',\n", | |
| " 'YlGn_r',\n", | |
| " 'YlGnBu_r',\n", | |
| " 'YlOrBr_r',\n", | |
| " 'YlOrRd_r',\n", | |
| " 'afmhot_r',\n", | |
| " 'autumn_r',\n", | |
| " 'binary_r',\n", | |
| " 'bone_r',\n", | |
| " 'brg_r',\n", | |
| " 'bwr_r',\n", | |
| " 'cool_r',\n", | |
| " 'coolwarm_r',\n", | |
| " 'copper_r',\n", | |
| " 'cubehelix_r',\n", | |
| " 'flag_r',\n", | |
| " 'gist_earth_r',\n", | |
| " 'gist_gray_r',\n", | |
| " 'gist_heat_r',\n", | |
| " 'gist_ncar_r',\n", | |
| " 'gist_rainbow_r',\n", | |
| " 'gist_stern_r',\n", | |
| " 'gist_yarg_r',\n", | |
| " 'gnuplot_r',\n", | |
| " 'gnuplot2_r',\n", | |
| " 'gray_r',\n", | |
| " 'hot_r',\n", | |
| " 'hsv_r',\n", | |
| " 'jet_r',\n", | |
| " 'nipy_spectral_r',\n", | |
| " 'ocean_r',\n", | |
| " 'pink_r',\n", | |
| " 'prism_r',\n", | |
| " 'rainbow_r',\n", | |
| " 'seismic_r',\n", | |
| " 'spring_r',\n", | |
| " 'summer_r',\n", | |
| " 'terrain_r',\n", | |
| " 'winter_r',\n", | |
| " 'Accent_r',\n", | |
| " 'Dark2_r',\n", | |
| " 'Paired_r',\n", | |
| " 'Pastel1_r',\n", | |
| " 'Pastel2_r',\n", | |
| " 'Set1_r',\n", | |
| " 'Set2_r',\n", | |
| " 'Set3_r',\n", | |
| " 'tab10_r',\n", | |
| " 'tab20_r',\n", | |
| " 'tab20b_r',\n", | |
| " 'tab20c_r',\n", | |
| " 'grey_r',\n", | |
| " 'gist_grey_r',\n", | |
| " 'gist_yerg_r',\n", | |
| " 'Grays_r']" | |
| ] | |
| }, | |
| "execution_count": 2, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "list(matplotlib.colormaps)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "id": "bee80bb9", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "<class 'matplotlib.colors.ListedColormap'>\n", | |
| "256\n" | |
| ] | |
| }, | |
| { | |
| "data": { | |
| "image/png": "iVBORw0KGgoAAAANSUhEUgAAAgAAAABACAYAAABsv8+/AAAAFnRFWHRUaXRsZQB2aXJpZGlzIGNvbG9ybWFwrE0mCwAAABx0RVh0RGVzY3JpcHRpb24AdmlyaWRpcyBjb2xvcm1hcAtjl3IAAAAxdEVYdEF1dGhvcgBNYXRwbG90bGliIHYzLjEwLjMsIGh0dHBzOi8vbWF0cGxvdGxpYi5vcmeGenhVAAAAM3RFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHYzLjEwLjMsIGh0dHBzOi8vbWF0cGxvdGxpYi5vcmcjXmuhAAACIklEQVR4nO3WS3KkMBAFQAmu5iPM/Y8yOGbMJyiQaXd49zI3REmlklbdr3/0P0v7p/f/n9an9XOu21r3rW9a10t97N+fr/dc5o7mX+bef5eH/dH5Zd9vb+630/y9f1uvfWVe7b/Wdf5obvt+zv7+h3su9f09z/Wr8+/rd+/57fmPfe2989f15b3z7bW5P31HG/YN9tvT+e/X+6CvX+pyrtW+8rM2rB/W17nTpX8pPydbXffv54z69m972C99U//7Yt+5f/7x/vmeud3fW9fn7Vx7qLd7y33X+v4dx9zz+4/+87z50l/uG62Xd4zee6yXer+/rn+Zy9/NvK/3sr7W6/perx1H/1c9rfvz+v+7zQEAgggAABBIAACAQAIAAAQSAAAgkAAAAIEEAAAIJAAAQCABAAACCQAAEEgAAIBAAgAABBIAACCQAAAAgQQAAAgkAABAIAEAAAIJAAAQSAAAgEACAAAEEgAAIJAAAACBBAAACCQAAEAgAQAAAgkAABBIAACAQAIAAAQSAAAgkAAAAIEEAAAIJAAAQCABAAACCQAAEEgAAIBAAgAABBIAACCQAAAAgQQAAAgkAABAIAEAAAIJAAAQSAAAgEACAAAEEgAAIJAAAACBBAAACCQAAEAgAQAAAgkAABBIAACAQAIAAAQSAAAgkAAAAIEEAAAIJAAAQCABAAACCQAAEEgAAIBAAgAABBIAACCQAAAAgQQAAAj0CW5wiYciaorCAAAAAElFTkSuQmCC", | |
| "text/html": [ | |
| "<div style=\"vertical-align: middle;\"><strong>viridis</strong> </div><div class=\"cmap\"><img alt=\"viridis colormap\" title=\"viridis\" style=\"border: 1px solid #555;\" src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgAAAABACAYAAABsv8+/AAAAFnRFWHRUaXRsZQB2aXJpZGlzIGNvbG9ybWFwrE0mCwAAABx0RVh0RGVzY3JpcHRpb24AdmlyaWRpcyBjb2xvcm1hcAtjl3IAAAAxdEVYdEF1dGhvcgBNYXRwbG90bGliIHYzLjEwLjMsIGh0dHBzOi8vbWF0cGxvdGxpYi5vcmeGenhVAAAAM3RFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHYzLjEwLjMsIGh0dHBzOi8vbWF0cGxvdGxpYi5vcmcjXmuhAAACIklEQVR4nO3WS3KkMBAFQAmu5iPM/Y8yOGbMJyiQaXd49zI3REmlklbdr3/0P0v7p/f/n9an9XOu21r3rW9a10t97N+fr/dc5o7mX+bef5eH/dH5Zd9vb+630/y9f1uvfWVe7b/Wdf5obvt+zv7+h3su9f09z/Wr8+/rd+/57fmPfe2989f15b3z7bW5P31HG/YN9tvT+e/X+6CvX+pyrtW+8rM2rB/W17nTpX8pPydbXffv54z69m972C99U//7Yt+5f/7x/vmeud3fW9fn7Vx7qLd7y33X+v4dx9zz+4/+87z50l/uG62Xd4zee6yXer+/rn+Zy9/NvK/3sr7W6/perx1H/1c9rfvz+v+7zQEAgggAABBIAACAQAIAAAQSAAAgkAAAAIEEAAAIJAAAQCABAAACCQAAEEgAAIBAAgAABBIAACCQAAAAgQQAAAgkAABAIAEAAAIJAAAQSAAAgEACAAAEEgAAIJAAAACBBAAACCQAAEAgAQAAAgkAABBIAACAQAIAAAQSAAAgkAAAAIEEAAAIJAAAQCABAAACCQAAEEgAAIBAAgAABBIAACCQAAAAgQQAAAgkAABAIAEAAAIJAAAQSAAAgEACAAAEEgAAIJAAAACBBAAACCQAAEAgAQAAAgkAABBIAACAQAIAAAQSAAAgkAAAAIEEAAAIJAAAQCABAAACCQAAEEgAAIBAAgAABBIAACCQAAAAgQQAAAj0CW5wiYciaorCAAAAAElFTkSuQmCC\"></div><div style=\"vertical-align: middle; max-width: 514px; display: flex; justify-content: space-between;\"><div style=\"float: left;\"><div title=\"#440154ff\" style=\"display: inline-block; width: 1em; height: 1em; margin: 0; vertical-align: middle; border: 1px solid #555; background-color: #440154ff;\"></div> under</div><div style=\"margin: 0 auto; display: inline-block;\">bad <div title=\"#00000000\" style=\"display: inline-block; width: 1em; height: 1em; margin: 0; vertical-align: middle; border: 1px solid #555; background-color: #00000000;\"></div></div><div style=\"float: right;\">over <div title=\"#fde725ff\" style=\"display: inline-block; width: 1em; height: 1em; margin: 0; vertical-align: middle; border: 1px solid #555; background-color: #fde725ff;\"></div></div></div>" | |
| ], | |
| "text/plain": [ | |
| "<matplotlib.colors.ListedColormap at 0x110e37680>" | |
| ] | |
| }, | |
| "execution_count": 3, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "cm = matplotlib.colormaps[\"viridis\"]\n", | |
| "print(type(cm))\n", | |
| "print(len(cm.colors))\n", | |
| "cm" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 9, | |
| "id": "b9e7cc13", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "#ifndef CMAP_H\n", | |
| "#define CMAP_H\n", | |
| "#ifdef __METAL_VERSION__\n", | |
| "#define CMAP_CONST constant\n", | |
| "#else\n", | |
| "#define CMAP_CONST const\n", | |
| "#define saturate(x) ((x) < 0 ? 0 : (x) > 1 ? 1 : (x))\n", | |
| "#define mix(x, y, a) ((x) + ((y) - (x)) * (a))\n", | |
| "#endif\n", | |
| "\n", | |
| "float3 _cmap_piecewise_linear(float x, CMAP_CONST float3* table, int n) {\n", | |
| " float scaled = saturate(x) * (n - 1);\n", | |
| " int i = (int)scaled;\n", | |
| " float t = scaled - i;\n", | |
| " return mix(table[i], table[i + 1], t);\n", | |
| "}\n", | |
| "\n", | |
| "CMAP_CONST float3 _table_viridis[] = {\n", | |
| " float3(0.267004, 0.004874, 0.329415),\n", | |
| " float3(0.277018, 0.050344, 0.375715),\n", | |
| " float3(0.282327, 0.094955, 0.417331),\n", | |
| " float3(0.282884, 0.13592, 0.453427),\n", | |
| " float3(0.278012, 0.180367, 0.486697),\n", | |
| " float3(0.269308, 0.218818, 0.509577),\n", | |
| " float3(0.257322, 0.25613, 0.526563),\n", | |
| " float3(0.243113, 0.292092, 0.538516),\n", | |
| " float3(0.225863, 0.330805, 0.547314),\n", | |
| " float3(0.210503, 0.363727, 0.552206),\n", | |
| " float3(0.19586, 0.395433, 0.555276),\n", | |
| " float3(0.182256, 0.426184, 0.55712),\n", | |
| " float3(0.168126, 0.459988, 0.558082),\n", | |
| " float3(0.15627, 0.489624, 0.557936),\n", | |
| " float3(0.144759, 0.519093, 0.556572),\n", | |
| " float3(0.133743, 0.548535, 0.553541),\n", | |
| " float3(0.123463, 0.581687, 0.547445),\n", | |
| " float3(0.119423, 0.611141, 0.538982),\n", | |
| " float3(0.12478, 0.640461, 0.527068),\n", | |
| " float3(0.143303, 0.669459, 0.511215),\n", | |
| " float3(0.180653, 0.701402, 0.488189),\n", | |
| " float3(0.226397, 0.728888, 0.462789),\n", | |
| " float3(0.281477, 0.755203, 0.432552),\n", | |
| " float3(0.344074, 0.780029, 0.397381),\n", | |
| " float3(0.421908, 0.805774, 0.35191),\n", | |
| " float3(0.496615, 0.826376, 0.306377),\n", | |
| " float3(0.575563, 0.844566, 0.256415),\n", | |
| " float3(0.657642, 0.860219, 0.203082),\n", | |
| " float3(0.751884, 0.874951, 0.143228),\n", | |
| " float3(0.83527, 0.886029, 0.102646),\n", | |
| " float3(0.916242, 0.896091, 0.100717),\n", | |
| " float3(0.993248, 0.906157, 0.143936),\n", | |
| "};\n", | |
| "\n", | |
| "float3 viridis(float x) {\n", | |
| " return _cmap_piecewise_linear(x, _table_viridis, sizeof(_table_viridis) / sizeof(float3));\n", | |
| "}\n", | |
| "\n", | |
| "#endif\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "print(\"\"\"#ifndef CMAP_H\n", | |
| "#define CMAP_H\n", | |
| "#ifdef __METAL_VERSION__\n", | |
| "#define CMAP_CONST constant\n", | |
| "#else\n", | |
| "#define CMAP_CONST const\n", | |
| "#define saturate(x) ((x) < 0 ? 0 : (x) > 1 ? 1 : (x))\n", | |
| "#define mix(x, y, a) ((x) + ((y) - (x)) * (a))\n", | |
| "#endif\n", | |
| "\"\"\")\n", | |
| "\n", | |
| "print(f\"float3 _cmap_piecewise_linear(float x, CMAP_CONST float3* table, int n) {{\")\n", | |
| "print(f\" float scaled = saturate(x) * (n - 1);\")\n", | |
| "print(f\" int i = (int)scaled;\")\n", | |
| "print(f\" float t = scaled - i;\")\n", | |
| "print(f\" return mix(table[i], table[i + 1], t);\")\n", | |
| "print(\"}\")\n", | |
| "print()\n", | |
| "\n", | |
| "def format_cmap(cm):\n", | |
| " table_name = f\"_table_{cm.name}\"\n", | |
| " print(f\"CMAP_CONST float3 {table_name}[] = {{\")\n", | |
| " for color in cm.colors:\n", | |
| " print(f\" float3({color[0]}, {color[1]}, {color[2]}),\")\n", | |
| " print(\"};\")\n", | |
| " print()\n", | |
| "\n", | |
| " print(f\"float3 {cm.name}(float x) {{\")\n", | |
| " print(f\" return _cmap_piecewise_linear(x, {table_name}, sizeof({table_name}) / sizeof(float3));\")\n", | |
| " print(\"}\")\n", | |
| " print()\n", | |
| "\n", | |
| "format_cmap(matplotlib.colormaps[\"viridis\"].resampled(32))\n", | |
| "\n", | |
| "print(\"#endif\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "b52d2d97", | |
| "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.12.8" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 5 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment