"""
PyColourChooser
Copyright (C) 2002 Michael Gilfix

This file is part of PyColourChooser.

You should have received a file COPYING containing license terms
along with this program; if not, write to Michael Gilfix
(mgilfix@eecs.tufts.edu) for a copy.

This version of PyColourChooser is open source; you can redistribute it and/or
modify it under the terms listed in the file COPYING.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
"""

# 12/14/2003 - Jeff Grimmett (grimmtooth@softhome.net)
#
# o 2.5 compatibility update.
#
# 12/21/2003 - Jeff Grimmett (grimmtooth@softhome.net)
#
# o wxPyColorChooser -> PyColorChooser
# o wxPyColourChooser -> PyColourChooser
# o Commented out wx.InitAllImageHandlers() (see comments at that
#   point for explanation
#

import  wx

from . import  canvas
import  colorsys

from wx.lib.embeddedimage import PyEmbeddedImage

# Size of Image
IMAGE_SIZE = (200,192)

Image = PyEmbeddedImage(
    "iVBORw0KGgoAAAANSUhEUgAAAMgAAADACAYAAABBCyzzAAAABHNCSVQICAgIfAhkiAAACwNJ"
    "REFUeJztnc16o0YQRZHt8SJZJO//lMkiWWQsKwsLK7S49dNdjTyTczYYhBBd7vqoom7B6bIs"
    "l2VZluW3Zdld/l603fn8n18/ln8u2+Ufy/527/Pe731ffsmdeO+Ave3ZgRUb6tvf+2dXPS08"
    "670uf9UMqPN7TwsASHAQAAMcBMDgZfn27eOv6+Ju+SKWz2L55CxP+0ux2T2cOg112utSDbfO"
    "EJ5B1Iidj7OG8AwihnvUtFDDvFnjsYbgCgJggIMAGOAgAAb3Ocjs4DJJVQQajTyPj7aDHGyI"
    "Lz4tjCPVGoQrCIABDgJggIMAGOg6iFdImBx8Hp173Ocg7Z7twNv19kzagbfri1h3PlaHUz+v"
    "TlcNz6mDPC4XmT0j9mcGVxAAAxwEwAAHATB4WV5fP/6Kim5+ktxDbT99oah7w8FJ2curvfvP"
    "l4ugxQJIg4MAGOAgAAZxLdakPhCF1ycw2g3QRpyvn8enH2RZ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5qtzaUXpp1FWrHX+F3lAGEQaIDVy9hbLdnDZI8a+/fOzgNAnuqASqDeIbZ"
    "fgoAO+AgAAY4CIDBrQ7i3W+PBpfR7QL1sYokF2c9u/3+F48yjMOoYToNVv3vHreK+mavAWzD"
    "cAUBMMBBAAxwEAADnYO0eDHw6Pbk171H0maf1KpzkKO3d359fMC726tH1W+N6JGrDPEBVxAA"
    "AxwEwAAHATD4F0lpw33hNrduAAAAAElFTkSuQmCC")


class PyPalette(canvas.Canvas):
    """The Pure-Python Palette

    The PyPalette is a pure python implementation of a colour palette. The
    palette implementation here imitates the palette layout used by MS
    Windows and Adobe Photoshop.

    The actual palette image has been embedded as an XPM for speed. The
    actual reverse-engineered drawing algorithm is provided in the
    GeneratePaletteBMP() method. The algorithm is tweakable by supplying
    the granularity factor to improve speed at the cost of display
    beauty. Since the generator isn't used in real time, no one will
    likely care :) But if you need it for some sort of unforeseen realtime
    application, it's there.
    """

    HORIZONTAL_STEP = 2
    VERTICAL_STEP   = 4

    def __init__(self, parent, id):
        """Creates a palette object."""
        # Load the pre-generated palette XPM

        # Leaving this in causes warning messages in some cases.
        # It is the responsibility of the app to init the image
        # handlers, IAW RD
        #wx.InitAllImageHandlers()

        self.palette = Image.GetBitmap()
        self.point = None

        canvas.Canvas.__init__ (self, parent, id, forceClientSize=IMAGE_SIZE)

    def DoGetBestClientSize(self):
        """Overridden to create a client window that exactly fits our bitmap"""
        return self.palette.GetSize()

    def xInBounds(self, x):
        """Limit x to [0,width)"""
        if x < 0:
            x = 0
        if x >= self.buffer.width:
            x = self.buffer.width - 1
        return x

    def yInBounds(self, y):
        """Limit y to [0,height)"""
        if y < 0:
            y = 0
        if y >= self.buffer.height:
            y = self.buffer.height - 1
        return y

    def GetValue(self, x, y):
        """Returns a colour value at a specific x, y coordinate pair. This
        is useful for determining the colour found a specific mouse click
        in an external event handler."""
        x = self.xInBounds(x)
        y = self.yInBounds(y)
        return self.buffer.GetPixelColour(x, y)

    def DrawBuffer(self):
        """Draws the palette XPM into the memory buffer."""
        self.buffer.DrawBitmap(self.palette, 0, 0, 0)

        if self.point:
            self.buffer.SetPen(wx.BLACK_PEN)
            self.buffer.SetBrush(wx.TRANSPARENT_BRUSH)
            self.buffer.DrawCircle(int(self.point[0]), int(self.point[1]), 3)

    def HighlightPoint(self, x, y):
        """Highlights an area of the palette with a little circle around
        the coordinate point"""
        self.point = (self.xInBounds(x), self.yInBounds(y))
        self.ReDraw()

    def ClearPoint(self):
        self.point = None

    def GeneratePaletteBMP(self, file_name, granularity=1):
        """The actual palette drawing algorithm.

        This used to be 100% reverse engineered by looking at the
        values on the MS map, but has since been redone Correctly(tm)
        according to the HSV (hue, saturation, value) colour model by
        Charl P. Botha <http://cpbotha.net/>.

        Speed is tweakable by changing the granularity factor, but
        that affects how nice the output looks (makes the vertical
        blocks bigger. This method was used to generate the embedded
        XPM data."""
        self.vertical_step = self.VERTICAL_STEP * granularity
        width, height = self.GetSize ()

        # simply iterate over hue (horizontal) and saturation (vertical)
        value = 1.0
        for y in range(0, height, self.vertical_step):
            saturation = 1.0 - float(y) / float(height)
            for x in range(0, width, self.HORIZONTAL_STEP):
                hue = float(x) / float(width)
                r,g,b = colorsys.hsv_to_rgb(hue, saturation, value)
                colour = wx.Colour(int(r * 255.0), int(g * 255.0), int(b * 255.0))
                self.buffer.SetPen(wx.Pen(colour, 1, wx.PENSTYLE_SOLID))
                self.buffer.SetBrush(wx.Brush(colour, wx.BRUSHSTYLE_SOLID))
                self.buffer.DrawRectangle(x, y,
                                          self.HORIZONTAL_STEP, self.vertical_step)

        # this code is now simpler (and works)
        bitmap = self.buffer.GetBitmap()
        image = wx.ImageFromBitmap(bitmap)
        image.SaveFile (file_name, wx.BITMAP_TYPE_XPM)
