Clean up draw and color utils
This commit is contained in:
@@ -1,13 +1,10 @@
|
||||
# noinspection PyPackageRequirements
|
||||
import wx
|
||||
import math
|
||||
|
||||
|
||||
def BrightenColor(color, factor):
|
||||
# Brightens a color (wx.Colour), factor = [0,1]
|
||||
|
||||
r, g, b = color
|
||||
a = color.Alpha()
|
||||
def Brighten(color: wx.Colour, factor: [0, 1]):
|
||||
""" Brightens a Color using a factor between 0 and 1"""
|
||||
r, g, b, a = color
|
||||
|
||||
factor = min(max(factor, 0), 1)
|
||||
|
||||
@@ -18,64 +15,59 @@ def BrightenColor(color, factor):
|
||||
return wx.Colour(r, g, b, a)
|
||||
|
||||
|
||||
def DarkenColor(color, factor):
|
||||
# Darkens a color (wx.Colour), factor = [0, 1]
|
||||
|
||||
bkR, bkG, bkB = color
|
||||
|
||||
alpha = color.Alpha()
|
||||
def Darken(color: wx.Colour, factor: [0, 1]):
|
||||
""" Darkens a Color using a factor between 0 and 1"""
|
||||
r, g, b, a = color
|
||||
|
||||
factor = min(max(factor, 0), 1)
|
||||
factor = 1 - factor
|
||||
|
||||
r = float(bkR * factor)
|
||||
g = float(bkG * factor)
|
||||
b = float(bkB * factor)
|
||||
r *= factor
|
||||
g *= factor
|
||||
b *= factor
|
||||
|
||||
r = min(max(r, 0), 255)
|
||||
b = min(max(b, 0), 255)
|
||||
g = min(max(g, 0), 255)
|
||||
|
||||
return wx.Colour(r, g, b, alpha)
|
||||
return wx.Colour(r, g, b, a)
|
||||
|
||||
|
||||
def GetBrightnessO1(color):
|
||||
# Calculates the brightness of a color, different options
|
||||
|
||||
r, g, b = color
|
||||
return 0.299 * r + 0.587 * g + 0.114 * b
|
||||
def _getBrightness(color: wx.Colour):
|
||||
"""
|
||||
Calculates brightness of color
|
||||
http://stackoverflow.com/a/596243/788054
|
||||
"""
|
||||
r, g, b, a = color
|
||||
return 0.299*r + 0.587*g + 0.114*b
|
||||
|
||||
|
||||
def GetBrightnessO2(color):
|
||||
r, g, b = color
|
||||
return math.sqrt(0.241 * r * r + 0.691 * g * g + 0.068 * b * b)
|
||||
def GetSuitable(color: wx.Colour, factor: [0, 1]):
|
||||
"""
|
||||
Calculates a suitable color based on original color (wx.Colour), its
|
||||
brightness, and a factor (darken/brighten by factor depending on
|
||||
calculated brightness)
|
||||
"""
|
||||
|
||||
|
||||
def GetSuitableColor(color, factor):
|
||||
# Calculates a suitable color based on original color (wx.Colour), its brightness, a factor=[0,1] (darken/brighten by factor depending on calculated brightness)
|
||||
|
||||
brightness = GetBrightnessO1(color)
|
||||
brightness = _getBrightness(color)
|
||||
|
||||
if brightness > 129:
|
||||
return DarkenColor(color, factor)
|
||||
return Darken(color, factor)
|
||||
else:
|
||||
return BrightenColor(color, factor)
|
||||
return Brighten(color, factor)
|
||||
|
||||
|
||||
def CalculateTransitionColor(startColor, endColor, delta):
|
||||
def CalculateTransition(s_color: wx.Colour, e_color: wx.Colour, delta: [0, 1]):
|
||||
"""
|
||||
Calculates the color between a given start and end colors, delta = [0,1]
|
||||
Colors are wx.Colour objects
|
||||
Calculates the color between a given start and end color using a delta
|
||||
value between 0 and 1
|
||||
"""
|
||||
|
||||
sR, sG, sB = startColor
|
||||
eR, eG, eB = endColor
|
||||
|
||||
alphaS = startColor.Alpha()
|
||||
alphaE = endColor.Alpha()
|
||||
sR, sG, sB, sA = s_color
|
||||
eR, eG, eB, eA = e_color
|
||||
|
||||
tR = sR + (eR - sR) * delta
|
||||
tG = sG + (eG - sG) * delta
|
||||
tB = sB + (eB - sB) * delta
|
||||
|
||||
return wx.Colour(tR, tG, tB, (alphaS + alphaE) / 2)
|
||||
return wx.Colour(tR, tG, tB, (sA + eA)/2)
|
||||
|
||||
@@ -1,26 +1,27 @@
|
||||
# noinspection PyPackageRequirements
|
||||
import wx
|
||||
import gui.utils.color as colorUtils
|
||||
from . import color
|
||||
|
||||
|
||||
def RenderGradientBar(windowColor, width, height, sFactor, eFactor, mFactor=None, fillRatio=2):
|
||||
def RenderGradientBar(windowColor, width, height, sFactor, eFactor, mFactor=None , fillRatio=2):
|
||||
|
||||
if sFactor == 0 and eFactor == 0 and mFactor is None:
|
||||
return DrawFilledBitmap(width, height, windowColor)
|
||||
return DrawFilledBitmap(width,height, windowColor)
|
||||
|
||||
gStart = colorUtils.GetSuitableColor(windowColor, sFactor)
|
||||
gStart = color.GetSuitable(windowColor, sFactor)
|
||||
|
||||
if mFactor:
|
||||
gMid = colorUtils.GetSuitableColor(windowColor, mFactor)
|
||||
gMid = color.GetSuitable(windowColor, mFactor)
|
||||
else:
|
||||
gMid = colorUtils.GetSuitableColor(windowColor, sFactor + (eFactor - sFactor) / 2)
|
||||
gMid = color.GetSuitable(windowColor, sFactor + (eFactor - sFactor) / 2)
|
||||
|
||||
gEnd = colorUtils.GetSuitableColor(windowColor, eFactor)
|
||||
gEnd = color.GetSuitable(windowColor, eFactor)
|
||||
|
||||
return DrawGradientBar(width, height, gStart, gEnd, gMid, fillRatio)
|
||||
|
||||
|
||||
def DrawFilledBitmap(width, height, color):
|
||||
canvas = wx.EmptyBitmap(width, height)
|
||||
canvas = wx.Bitmap(width,height)
|
||||
|
||||
mdc = wx.MemoryDC()
|
||||
mdc.SelectObject(canvas)
|
||||
@@ -32,25 +33,21 @@ def DrawFilledBitmap(width, height, color):
|
||||
|
||||
return canvas
|
||||
|
||||
|
||||
# noinspection PyPropertyAccess
|
||||
def DrawGradientBar(width, height, gStart, gEnd, gMid=None, fillRatio=4):
|
||||
# we need to have dimensions to draw
|
||||
# assert width > 0 and height > 0
|
||||
canvas = wx.EmptyBitmap(width, height)
|
||||
canvas = wx.Bitmap(width,height)
|
||||
|
||||
mdc = wx.MemoryDC()
|
||||
mdc.SelectObject(canvas)
|
||||
|
||||
r = wx.Rect(0, 0, width, height)
|
||||
r.height = height / fillRatio
|
||||
r.SetHeight(height / fillRatio)
|
||||
|
||||
if gMid is None:
|
||||
gMid = gStart
|
||||
|
||||
mdc.GradientFillLinear(r, gStart, gMid, wx.SOUTH)
|
||||
r.top = r.height
|
||||
r.height = height * (fillRatio - 1) / fillRatio + (1 if height % fillRatio != 0 else 0)
|
||||
r.SetTop(r.GetHeight())
|
||||
r.SetHeight(height * (fillRatio - 1)/fillRatio + (1 if height % fillRatio != 0 else 0))
|
||||
|
||||
mdc.GradientFillLinear(r, gMid, gEnd, wx.SOUTH)
|
||||
|
||||
@@ -59,50 +56,31 @@ def DrawGradientBar(width, height, gStart, gEnd, gMid=None, fillRatio=4):
|
||||
return canvas
|
||||
|
||||
|
||||
def GetPartialText(dc, text, maxWidth, defEllipsis="..."):
|
||||
ellipsis = defEllipsis
|
||||
base_w, h = dc.GetTextExtent(ellipsis)
|
||||
|
||||
lenText = len(text)
|
||||
drawntext = text
|
||||
w, dummy = dc.GetTextExtent(text)
|
||||
|
||||
while lenText > 0:
|
||||
|
||||
if w + base_w <= maxWidth:
|
||||
break
|
||||
|
||||
w_c, h_c = dc.GetTextExtent(drawntext[-1])
|
||||
drawntext = drawntext[0:-1]
|
||||
lenText -= 1
|
||||
w -= w_c
|
||||
|
||||
while len(ellipsis) > 0 and w + base_w > maxWidth:
|
||||
ellipsis = ellipsis[0:-1]
|
||||
def GetPartialText(dc, text , maxWidth, defEllipsis="..."):
|
||||
ellipsis = defEllipsis
|
||||
base_w, h = dc.GetTextExtent(ellipsis)
|
||||
if len(text) > lenText:
|
||||
return drawntext + ellipsis
|
||||
else:
|
||||
return text
|
||||
|
||||
lenText = len(text)
|
||||
drawntext = text
|
||||
w, dummy = dc.GetTextExtent(text)
|
||||
|
||||
def GetRoundBitmap(w, h, r):
|
||||
maskColor = wx.Color(0, 0, 0)
|
||||
shownColor = wx.Color(5, 5, 5)
|
||||
b = wx.EmptyBitmap(w, h)
|
||||
dc = wx.MemoryDC(b)
|
||||
dc.SetBrush(wx.Brush(maskColor))
|
||||
dc.DrawRectangle(0, 0, w, h)
|
||||
dc.SetBrush(wx.Brush(shownColor))
|
||||
dc.SetPen(wx.Pen(shownColor))
|
||||
dc.DrawRoundedRectangle(0, 0, w, h, r)
|
||||
dc.SelectObject(wx.NullBitmap)
|
||||
b.SetMaskColour(maskColor)
|
||||
return b
|
||||
while lenText > 0:
|
||||
|
||||
if w + base_w <= maxWidth:
|
||||
break
|
||||
|
||||
def GetRoundShape(w, h, r):
|
||||
return wx.RegionFromBitmap(GetRoundBitmap(w, h, r))
|
||||
w_c, h_c = dc.GetTextExtent(drawntext[-1])
|
||||
drawntext = drawntext[0:-1]
|
||||
lenText -= 1
|
||||
w -= w_c
|
||||
|
||||
while len(ellipsis) > 0 and w + base_w > maxWidth:
|
||||
ellipsis = ellipsis[0:-1]
|
||||
base_w, h = dc.GetTextExtent(ellipsis)
|
||||
if len(text) > lenText:
|
||||
return drawntext + ellipsis
|
||||
else:
|
||||
return text
|
||||
|
||||
|
||||
def CreateDropShadowBitmap(bitmap, opacity):
|
||||
|
||||
Reference in New Issue
Block a user