Skip to content

Commit

Permalink
Added continuous coloring using Bernstein polynomials
Browse files Browse the repository at this point in the history
You can now use a new continuous coloring algorithm based on Bernstein
polynomials. This introduced the new command line parameter rgb_amp that
is used for the peak to peak amplitude of the color channels. I added
some unit tests for this new coloring technique and a jupyther notebook
to generate color bars and color channel waves.
  • Loading branch information
crapp committed Apr 6, 2016
1 parent 145b7b3 commit cfe3c03
Show file tree
Hide file tree
Showing 16 changed files with 431 additions and 90 deletions.
217 changes: 217 additions & 0 deletions resources/BernsteinContinuousColoring.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 271,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib as mpl\n",
"import matplotlib.pyplot as plt\n",
"import matplotlib.colors as col\n",
"import matplotlib.cm as cm"
]
},
{
"cell_type": "code",
"execution_count": 272,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def rgb_continuous_bernstein(escape_time, bailout, rgb_base, rgb_amp):\n",
" '''\n",
" Convert cont_index with rgb_base rgb_freq and rgb_phase to a rgb value between 0 and 255 \n",
" using adapted Bernstein polynomial\n",
" '''\n",
" red = rgb_base[0]\n",
" green = rgb_base[1]\n",
" blue = rgb_base[2]\n",
" if (escape_time == bailout):\n",
" return (0, 0, 0)\n",
" \n",
" index_mapped = escape_time / bailout\n",
" \n",
" if (rgb_amp[0] > 0):\n",
" red = np.abs(rgb_amp[0] * (1 - index_mapped) * np.power(index_mapped, 3) * (255 - rgb_base[0]) + rgb_base[0] )\n",
" if (rgb_amp[1] > 0):\n",
" green = np.abs(rgb_amp[1] * np.power((1 - index_mapped), 2) * np.power(index_mapped, 2 ) * (255 - rgb_base[1]) + rgb_base[1])\n",
" if (rgb_amp[2] > 0):\n",
" blue = np.abs(rgb_amp[2] * np.power((1 - index_mapped), 3) * index_mapped * (255 - rgb_base[2]) + rgb_base[2])\n",
" \n",
" rgb = np.array([red, green, blue])\n",
" return rgb.astype(int)"
]
},
{
"cell_type": "code",
"execution_count": 273,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def generate_rgb_tuples(escape_idx, bailout, rgb_base, rgb_amp):\n",
" rgb_tuples = np.zeros((escape_idx.size,3), dtype='uint8')\n",
" tuples_idx = 0\n",
" \n",
" for etime in escape_idx:\n",
" rgb_tuples[tuples_idx] = rgb_continuous_bernstein(etime,\n",
" bailout,\n",
" rgb_base,\n",
" rgb_amp)\n",
" # print(rgb_tuples[tuples_idx])\n",
" tuples_idx +=1\n",
" \n",
" return rgb_tuples"
]
},
{
"cell_type": "code",
"execution_count": 274,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"escape_idx = np.linspace(0, 1000, 1001)\n",
"rgb_tuples_1 = generate_rgb_tuples(escape_idx, \n",
" escape_idx.size,\n",
" (0, 0, 0),\n",
" # you have to adjust the frequency so it corresponds with the fractal bailout\n",
" (9, 15, 8.5))"
]
},
{
"cell_type": "code",
"execution_count": 275,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"bounds = np.arange(0, 1000, 1)\n",
"norm_obj_1 = col.Normalize(vmin=0,\n",
" vmax=255)"
]
},
{
"cell_type": "code",
"execution_count": 276,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<matplotlib.colors.ListedColormap object at 0x7f5c68baf940>\n"
]
}
],
"source": [
"colmap_1 = col.ListedColormap(norm_obj_1(rgb_tuples_1))\n",
"print(colmap_1)"
]
},
{
"cell_type": "code",
"execution_count": 277,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"norm_1 = mpl.colors.BoundaryNorm(bounds, colmap_1.N)"
]
},
{
"cell_type": "code",
"execution_count": 278,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"fig = plt.figure(1, figsize=(10, 6))\n",
"fig.suptitle(\"Rainbow Colors using out of phase waves\", fontsize='medium', weight='bold')\n",
"ax_1 = fig.add_subplot(111)\n",
"ax_1.plot(escape_idx, rgb_tuples_1.T[0], c='red', lw=2)\n",
"ax_1.plot(escape_idx, rgb_tuples_1.T[1], c='green', lw=2)\n",
"ax_1.plot(escape_idx, rgb_tuples_1.T[2], c='blue', lw=2)\n",
"ax_1.set_xlabel(\"Continuous Index\")\n",
"ax_1.set_ylabel(\"Color Channel\")\n",
"rgb_settings_1 = \"rgb-base=127,127,127 rgb-freq=0.006,0.006,0.006 rgb-phase=0,2,4\"\n",
"ax_1.text(0.5, 0.92, rgb_settings_1,\n",
" horizontalalignment='center',\n",
" fontsize='small',\n",
" weight='bold',\n",
" transform = ax_1.transAxes)\n",
"ax_1.grid()\n",
"# make room for an additional axis at the bottom\n",
"fig.subplots_adjust(top=0.93, bottom=0.27)\n",
"# get the bounding box\n",
"box = ax_1.get_position()\n",
"# use the box to position the colorbar\n",
"ax_colb = fig.add_axes([box.x0, box.y0 - 0.18, box.width, 0.1])\n",
"cb_1 = mpl.colorbar.ColorbarBase(ax_colb, cmap=colmap_1,\n",
" norm=norm_1,\n",
" boundaries=bounds,\n",
" # extend='both',\n",
" # Make the length of each extension\n",
" # the same as the length of the\n",
" # interior colors:\n",
" extendfrac='auto',\n",
" spacing='uniform',\n",
" orientation='horizontal')\n",
"cb_1.set_label(\"Rainbow Colors\")"
]
},
{
"cell_type": "code",
"execution_count": 279,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"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.5.1"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Loading

0 comments on commit cfe3c03

Please sign in to comment.