{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div class=\"contentcontainer med left\" style=\"margin-left: -50px;\">\n",
    "<dl class=\"dl-horizontal\">\n",
    "  <dt>Title</dt> <dd> HSV Element</dd>\n",
    "  <dt>Dependencies</dt> <dd>Matplotlib</dd>\n",
    "  <dt>Backends</dt> <dd><a href='./HSV.ipynb'>Matplotlib</a></dd> <dd><a href='../bokeh/HSV.ipynb'>Bokeh</a></dd>\n",
    "</dl>\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import holoviews as hv\n",
    "from holoviews import opts\n",
    "\n",
    "hv.extension('matplotlib')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "HSV represents a regularly spaced 2D grid of an underlying continuous space of HSV (hue, saturation and value) color space values. The definition of the grid closely matches the semantics of an ``Image`` or ``RGB`` element and in the simplest case the grid may be specified as a ``NxMx3`` or ``NxMx4`` array of values along with a bounds. An HSV element may also be defined through explicit and regularly spaced x/y-coordinate arrays. The two most basic supported constructors of an HSV element therefore include:\n",
    "\n",
    "    HSV((X, Y, H, S, V))\n",
    "\n",
    "where ``X`` is a 1D array of shape ``M``, ``Y`` is a 1D array of shape ``N`` and ``H``/``S``/``V`` are 2D array of shape ``NxM``, or equivalently:\n",
    "\n",
    "    HSV(Z, bounds=(x0, y0, x1, y1))\n",
    "\n",
    "where ``Z`` is a 3D array of stacked ``H``/``S``/``V`` arrays with shape ``NxMx3/4`` and the bounds define the (left, bottom, right, top) edges of the four corners of the grid. Other gridded formats which support declaring of explicit x/y-coordinate arrays such as xarray are also supported. See the [Gridded Datasets](../../../user_guide/09-Gridded_Datasets.ipynb) user guide for all the other accepted data formats."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x,y = np.mgrid[-50:51, -50:51] * 0.1\n",
    "h = 0.5 + np.sin(0.2*(x**2+y**2)) / 2.0\n",
    "s = 0.5*np.cos(y*3)+0.5\n",
    "v = 0.5*np.cos(x*3)+0.5\n",
    "\n",
    "hsv = hv.HSV(np.dstack([h, s, v]))\n",
    "hsv"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can see how this is created from the original channels:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "opts.defaults(opts.Image(cmap='gray'))\n",
    "\n",
    "hsv[..., 'H'].relabel('H') + hsv[..., 'S'].relabel('S') + hsv[..., 'V'].relabel('V')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "An ``HSV`` Element can also easily be converted to an ``RGB`` Element using the ``rgb`` property:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(hsv.rgb)\n",
    "hsv.rgb[..., 'R'] + hsv.rgb[..., 'G'] + hsv.rgb[..., 'B']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For full documentation and the available style and plot options, use ``hv.help(hv.HSV).``"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
