Last active
March 31, 2025 01:29
-
-
Save Simon-L/ad78361730e9f8efaf6eecb457cc2100 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": null, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"application/vnd.jupyter.widget-view+json": { | |
"model_id": "3cbf5204b51f4c918acb2b93c1e5c5fb", | |
"version_major": 2, | |
"version_minor": 0 | |
}, | |
"text/plain": [ | |
"interactive(children=(FloatSlider(value=0.7, description='R1', max=1.0, step=0.01), FloatSlider(value=0.5, des…" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"import matplotlib.pyplot as plt\n", | |
"import numpy as np\n", | |
"import colorsys\n", | |
"from ipywidgets import interact, FloatSlider\n", | |
"\n", | |
"def rgb_to_gray(rgb):\n", | |
" return 0.2989 * rgb[0] + 0.5870 * rgb[1] + 0.1140 * rgb[2]\n", | |
"\n", | |
"def mix_colors(color1, color2):\n", | |
" mixed = [color1[0] + color2[0], color1[1] + color2[1], color1[2] + color2[2]]\n", | |
" return mixed\n", | |
"\n", | |
"def plot_colors(color1, color2, mixed):\n", | |
" fig, ax = plt.subplots(1, 1, figsize=(10, 5))\n", | |
" m = np.zeros((256, 256, 3))\n", | |
"\n", | |
" for x, a in enumerate(m):\n", | |
" for y, b in enumerate(a):\n", | |
" if (x < 50) and (x >= 0):\n", | |
" m[x][y] = color1\n", | |
" if (x >= 200):\n", | |
" m[x][y] = color2\n", | |
" if (x >= 50) and (x < 75):\n", | |
" m[x][y] = [color1[0] * y/255, color1[1] * y/255, color1[2] * y/255]\n", | |
" if (x >= 175) and (x < 200):\n", | |
" m[x][y] = [color2[0] * y/255, color2[1] * y/255, color2[2] * y/255]\n", | |
" if (x >= 75) and (x < 175):\n", | |
" c1 = (x - 75)/100\n", | |
" c2 = y/255\n", | |
" m[x][y] = [\n", | |
" color1[0] * c1 + color2[0] * c2,\n", | |
" color1[1] * c1 + color2[1] * c2,\n", | |
" color1[2] * c1 + color2[2] * c2]\n", | |
" m = np.rot90(m)\n", | |
" ax.imshow(m)\n", | |
" ax.axis('off')\n", | |
" plt.show()\n", | |
"\n", | |
"def rgb2hex(color):\n", | |
" return \"#{:02x}{:02x}{:02x}\".format(int(color[0] * 255), int(color[1] * 255), int(color[2] * 255))\n", | |
"\n", | |
"def color_experiment(r1, g1, b1, r2, g2, b2):\n", | |
" color1 = [r1, g1, b1]\n", | |
" color2 = [r2, g2, b2]\n", | |
" mixed = mix_colors(color1, color2)\n", | |
" plot_colors(color1, color2, mixed)\n", | |
" print(f\"Luminance of Color 1: {rgb_to_gray(color1)}\")\n", | |
" print(f\"Luminance of Color 2: {rgb_to_gray(color2)}\")\n", | |
" print(f\"Luminance of Mixed Color: {rgb_to_gray(mixed)}\")\n", | |
" print(colorsys.rgb_to_hls(color1[0], color1[1], color1[2]))\n", | |
" print(rgb2hex(color1))\n", | |
" print(colorsys.rgb_to_hls(color2[0], color2[1], color2[2]))\n", | |
" print(rgb2hex(color2))\n", | |
"\n", | |
"interact(color_experiment,\n", | |
" r1=FloatSlider(min=0.0, max=1.0, step=0.01, value=0.7, description='R1'),\n", | |
" g1=FloatSlider(min=0.0, max=1.0, step=0.01, value=0.5, description='G1'),\n", | |
" b1=FloatSlider(min=0.0, max=1.0, step=0.01, value=0.0, description='B1'),\n", | |
" r2=FloatSlider(min=0.0, max=1.0, step=0.01, value=0.3, description='R2'),\n", | |
" g2=FloatSlider(min=0.0, max=1.0, step=0.01, value=0.5, description='G2'),\n", | |
" b2=FloatSlider(min=0.0, max=1.0, step=0.01, value=1.0, description='B2'));\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": ".venv", | |
"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.6" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
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
numpy | |
matplotlib |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment