import wx

def HSBtoRGB(hsb):
    h,s, b = map(int, hsb.split("."))
    
    nHue, nSaturation, nBrightness = h/360., s/100., b/100.
    """HSB to RGB color space conversion routine.
    nHue, nSaturation and nBrightness are all from 0.0 to 1.0.
    This routine returns three integer numbers, nRed, nGreen, nBlue.
    nRed, nGreen and nBlue are all numbers from 0 to 255.
    """
    # Scale the brightness from a range of 0.0 thru 1.0
    #  to a range of 0.0 thru 255.0
    # Then truncate to an integer.
    nBrightness = int( min( nBrightness * 256.0, 255.0 ) )

    if nSaturation == 0.0:
        # Grayscale because there is no saturation
        nRed = nBrightness
        nGreen = nBrightness
        nBlue = nBrightness
    else:
        # Make hue angle be within a single rotation.
        # If the hue is > 1.0 or < 0.0, then it has
        #  "gone around the color wheel" too many times.
        #  For example, a value of 1.2 means that it has
        #  gone around the wheel 1.2 times, which is really
        #  the same ending angle as 0.2 trips around the wheel.
        # Scale it back to the 0.0 to 1.0 range.
        if nHue > 1.0:
            nHue = nHue - int( nHue )
        elif nHue < 0.0:
            nHue = abs( nHue )
            if nHue > 1.0:
                nHue = nHue - int( nHue )
            nHue = 1.0 - nHue
        # Rescale hue to a range of 0.0 thru 6.0
        nHue = nHue * 6.0
        # Separate hue into int and fractional parts
        iHue = int( nHue )
        fHue = nHue - iHue
        # Is hue even?
        if iHue % 2 == 0:
            fHue = 1.0 - fHue
        #
        m = nBrightness * (1.0 - nSaturation)
        n = nBrightness * (1.0 - (nSaturation * fHue))

        if iHue == 1:
            nRed = n
            nGreen = nBrightness
            nBlue = m
        elif iHue == 2:
            nRed = m
            nGreen = nBrightness
            nBlue = n
        elif iHue == 3:
            nRed = m
            nGreen = n
            nBlue = nBrightness
        elif iHue == 4:
            nRed = n
            nGreen = m
            nBlue = nBrightness
        elif iHue == 5:
            nRed = nBrightness
            nGreen = m
            nBlue = n
        else:
            nRed = nBrightness
            nGreen = n
            nBlue = m
   
    return "#"+"".join([x[2:] for x in  map(hex,  map(int, (nRed, nGreen, nBlue )))])
        
def HSBtoRGBA(hsb, a):
    rgb = HSBtoRGB(hsb)[1:]
    return (int(rgb[:2], 16), int(rgb[2:4], 16), int(rgb[4:], 16), a)

class GradientPanel(wx.Panel):
    def __init__(self, parent, border=wx.ALL):
        wx.Panel.__init__(self, parent)
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        # self.Bind(wx.EVT_ERASE_BACKGROUND, DoNothing)
        self.Bind(wx.EVT_SIZE, self.OnSize)
        self.border_pen = wx.Pen(HSBtoRGB("0.0.25"))
        self.border = border
        
    def OnSize(self, evt):
        evt.Skip()
        self.Refresh()
        
    def OnPaint(self, evt):
        pdc = wx.PaintDC(self)
        w, h = self.GetSize()
        rect = (-1,-1,w+1, h+1)
        pdc.GradientFillLinear(rect, HSBtoRGB("0.0.76"), HSBtoRGB("0.0.59"), wx.SOUTH)
        pdc.SetPen(self.border_pen)
        if self.border & wx.TOP:
            pdc.DrawLine(0,0, w, 0)
        if self.border & wx.BOTTOM:
            pdc.DrawLine(0,h-1, w, h-1)
    
        if self.border & wx.LEFT:
            pdc.DrawLine(0,0, 0, h)
    
        if self.border & wx.LEFT:
            pdc.DrawLine(w-1,0, w-1, h)
    
class ToolBar2(GradientPanel):
    def __init__(self, parent):
        GradientPanel.__init__(self, parent)
        self.buttons = ["b1", "b2", "b3"]

        self.border_pen = wx.Pen(HSBtoRGB("0.0.36"))
        self.border_shadow_pen = wx.Pen(HSBtoRGBA("0.0.36", 80))
        self.border_high_pen = wx.Pen(HSBtoRGB("0.0.80"))
        
    def OnPaint(self, evt):
        gc = wx.GCDC(wx.PaintDC(self))
        # gc = wx.PaintDC(self)
        w, h = self.GetSize()
        rect = (-1,-1,w+1, h+1)
        gc.GradientFillLinear(rect, HSBtoRGB("0.0.76"), HSBtoRGB("0.0.59"), wx.SOUTH)
        gc.SetPen(self.border_pen)
        if self.border & wx.TOP:
            gc.DrawLine(0,0, w, 0)
        if self.border & wx.BOTTOM:
            gc.DrawLine(0,h-1, w, h-1)
    
        if self.border & wx.LEFT:
            gc.DrawLine(0,0, 0, h)
    
        if self.border & wx.LEFT:
            gc.DrawLine(w-1,0, w-1, h)
        
        for button in self.buttons:
            w, h = 50, 25
            x, y = 50+60*self.buttons.index(button), (48-h)/2
            gc.SetBrush(wx.TRANSPARENT_BRUSH)
            rect = (x+2,y+1, w-4, h-4)
            gc.SetPen(self.border_shadow_pen)
            gc.DrawRoundedRectangle(x,y+1, w, h-1, 2)
            gc.GradientFillLinear(rect, HSBtoRGB("0.0.100"), HSBtoRGB("0.0.70"), wx.SOUTH)
            gc.SetPen(self.border_pen)
            # gc.DrawRoundedRectangle(x+0,y+1, w, h-2, 2) 
            # gc.SetPen(self.border_high_pen)
            # gc.DrawLine(x+2,y+h-3, x+w-2, y+h-3) 

if __name__ == "__main__":
    app = wx.App(0)
    frame = wx.Frame(None)
    panel = ToolBar2(frame)
    
    panel.SetMinSize((400, 48))
    box = wx.BoxSizer()
    box.Add(panel, 1, wx.EXPAND)
    frame.SetSizerAndFit(box)
    frame.Center()
    frame.Show()
    app.MainLoop()