From 5a46cefb14d888d011ed4b0963d588bcff36cc3d Mon Sep 17 00:00:00 2001 From: HomeWorld Date: Wed, 8 Dec 2010 14:54:06 +0200 Subject: [PATCH] Streamlined pygauge/pyfaGaugePreferences a little; moved common code in gui.utils.colorUtils - this module will be used soon everywhere where color processing is needed (shipbrowser/chrometabs..) --- .../pyfaGaugePreferences.py | 41 +++++------------ gui/pygauge.py | 42 +++++------------ gui/utils/__init__.py | 0 gui/utils/colorUtils.py | 46 +++++++++++++++++++ 4 files changed, 69 insertions(+), 60 deletions(-) create mode 100644 gui/utils/__init__.py create mode 100644 gui/utils/colorUtils.py diff --git a/gui/builtinPreferenceViews/pyfaGaugePreferences.py b/gui/builtinPreferenceViews/pyfaGaugePreferences.py index 25456d8c6..254f5dd4a 100755 --- a/gui/builtinPreferenceViews/pyfaGaugePreferences.py +++ b/gui/builtinPreferenceViews/pyfaGaugePreferences.py @@ -7,6 +7,7 @@ import copy from gui.preferenceView import PreferenceView from gui import bitmapLoader +from gui.utils import colorUtils ########################################################################### ## Class PFGaugePref @@ -26,11 +27,11 @@ class PFGaugePreview(wx.Window): self.animDir = 1 self._fractionDigits = 2 - self.colorS = wx.Colour(0,0,0) - self.colorE = wx.Colour(0,0,0) + self.colorS = wx.Colour(0,0,0,255) + self.colorE = wx.Colour(0,0,0,255) self.gradientStart = 0 - self.bkColor = wx.Colour(0,0,0) + self.bkColor = wx.Colour(0,0,0,255) self.SetMinSize((100,-1)) self.font = wx.FontFromPixelSize((0,13),wx.SWISS, wx.NORMAL, wx.NORMAL, False) @@ -103,29 +104,6 @@ class PFGaugePreview(wx.Window): self.percE = end self.Refresh() - def CalculateGColor(self, color, delta): - bkR ,bkG , bkB = color - - r = float(bkR * delta) / 100 - g = float(bkG * delta) / 100 - b = float(bkB * delta) / 100 - - r = min(max(r,0),255) - b = min(max(b,0),255) - g = min(max(g,0),255) - - return wx.Colour(r,g,b,255) - - def CalculateTransitionColor(self, startColor, endColor, delta): - sR,sG,sB = startColor - eR,eG,eB = endColor - - tR = sR + (eR - sR) * delta - tG = sG + (eG - sG) * delta - tB = sB + (eB - sB) * delta - - return (tR, tG, tB) - def OnPaint(self, event): rect = self.GetClientRect() dc = wx.BufferedPaintDC(self) @@ -141,8 +119,11 @@ class PFGaugePreview(wx.Window): r.width = w r.height = r.height/2+1 - color = self.CalculateTransitionColor(self.colorS, self.colorE, float(value)/100) - gcolor = self.CalculateGColor(color, self.gradientStart) + color = colorUtils.CalculateTransitionColor(self.colorS, self.colorE, float(value)/100) + if self.gradientStart > 0: + gcolor = colorUtils.BrightenColor(color, float(self.gradientStart) / 100) + else: + gcolor = colorUtils.DarkenColor(color, float(-self.gradientStart) / 100) dc.GradientFillLinear(r, gcolor, color, wx.SOUTH) r.top = r.height dc.GradientFillLinear(r, gcolor, color, wx.NORTH) @@ -291,7 +272,7 @@ class PFGaugePref ( PreferenceView): self.cbLink = wx.CheckBox( panel, wx.ID_ANY, u"Link Colors", wx.DefaultPosition, wx.DefaultSize, 0 ) buttonsSizer.Add( self.cbLink, 0, wx.ALIGN_CENTER_VERTICAL | wx.RIGHT | wx.LEFT, 5 ) - self.sliderGradientStart = wx.Slider( panel, wx.ID_ANY, self.gradientStart, 0, 100, wx.DefaultPosition, (127,-1), wx.SL_HORIZONTAL|wx.SL_LABELS ) + self.sliderGradientStart = wx.Slider( panel, wx.ID_ANY, self.gradientStart, -100, 100, wx.DefaultPosition, (127,-1), wx.SL_HORIZONTAL|wx.SL_LABELS ) buttonsSizer.Add( self.sliderGradientStart, 1, wx.EXPAND | wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5 ) self.btnRestore = wx.Button( panel, wx.ID_ANY, u"Restore Defaults", wx.DefaultPosition, wx.DefaultSize, 0 ) @@ -345,7 +326,7 @@ class PFGaugePref ( PreferenceView): self.c103105S = wx.Colour(255, 128, 0, 255) self.c103105E = wx.Colour(255, 0, 0, 255) - self.gradientStart = 95 + self.gradientStart = -35 def SetColours(self): self.cp0100S.SetColour(self.c0100S) diff --git a/gui/pygauge.py b/gui/pygauge.py index 9041884a4..b934cc4a0 100644 --- a/gui/pygauge.py +++ b/gui/pygauge.py @@ -15,6 +15,8 @@ import wx import copy import math +from gui.utils import colorUtils + class PyGauge(wx.PyWindow): """ This class provides a visual alternative for `wx.Gauge`. It currently @@ -59,11 +61,11 @@ class PyGauge(wx.PyWindow): self._animValue = 0 self._animDirection = 0 - self.transitionsColors = [( (191, 191, 191, 255) , (96, 191, 0, 255) ), - ( (191, 167, 96, 255) , (255, 191, 0, 255) ), - ( (255, 191, 0, 255) , (255, 128, 0, 255) ), - ( (255, 128, 0, 255) , (255, 0, 0, 255) )] - self.gradientEffect = 35 + self.transitionsColors = [( wx.Colour(191, 191, 191, 255) , wx.Colour(96, 191, 0, 255) ), + ( wx.Colour(191, 167, 96, 255) , wx.Colour(255, 191, 0, 255) ), + ( wx.Colour(255, 191, 0, 255) , wx.Colour(255, 128, 0, 255) ), + ( wx.Colour(255, 128, 0, 255) , wx.Colour(255, 0, 0, 255) )] + self.gradientEffect = -35 self._percentage = 0 self._oldPercentage = 0 @@ -256,29 +258,6 @@ class PyGauge(wx.PyWindow): pass - def CalculateGColor(self, color, delta): - bkR ,bkG , bkB = color - - r = float(bkR * delta) / 100 - g = float(bkG * delta) / 100 - b = float(bkB * delta) / 100 - - r = min(max(r,0),255) - b = min(max(b,0),255) - g = min(max(g,0),255) - - return wx.Colour(r,g,b,255) - - def CalculateTransitionColor(self, startColor, endColor, delta): - sR,sG,sB,_ = startColor - eR,eG,eB,_ = endColor - - tR = sR + (eR - sR) * delta - tG = sG + (eG - sG) * delta - tB = sB + (eB - sB) * delta - - return (tR, tG, tB) - def OnPaint(self, event): """ Handles the ``wx.EVT_PAINT`` event for L{PyGauge}. @@ -347,11 +326,14 @@ class PyGauge(wx.PyWindow): if transition != -1: colorS,colorE = self.transitionsColors[transition] - color = self.CalculateTransitionColor(colorS, colorE, xv) + color = colorUtils.CalculateTransitionColor(colorS, colorE, xv) else: color = wx.Colour(191,48,48) - gcolor = self.CalculateGColor(color, self.gradientEffect) + if self.gradientEffect > 0: + gcolor = colorUtils.BrightenColor(color, float(self.gradientEffect) / 100) + else: + gcolor = colorUtils.DarkenColor(color, float(-self.gradientEffect) / 100) dc.GradientFillLinear(r, gcolor, color, wx.SOUTH) r.top = r.height diff --git a/gui/utils/__init__.py b/gui/utils/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/gui/utils/colorUtils.py b/gui/utils/colorUtils.py new file mode 100644 index 000000000..2dc44ee5d --- /dev/null +++ b/gui/utils/colorUtils.py @@ -0,0 +1,46 @@ +import wx + +#Brightens a color (wx.Colour), factor = [0,1] +def BrightenColor(color, factor): + + r,g,b = color + a = color.Alpha() + + factor = min(max(factor, 0), 1) + + r+=(255-r)*factor + b+=(255-b)*factor + g+=(255-g)*factor + + return wx.Colour(r,g,b,a) + +#Darkens a color (wx.Colour), factor = [0, 1] +def DarkenColor(color, factor): + bkR ,bkG , bkB = color + alpha = color.Alpha() + factor = 1 - factor + r = float(bkR * factor) + g = float(bkG * factor) + b = float(bkB * 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) + +#Calculates the color between a given start and end colors, delta = [0,1] +#Colors are wx.Colour objects + +def CalculateTransitionColor(startColor, endColor, delta): + sR,sG,sB = startColor + eR,eG,eB = endColor + + alphaS = startColor.Alpha() + alphaE = endColor.Alpha() + + tR = sR + (eR - sR) * delta + tG = sG + (eG - sG) * delta + tB = sB + (eB - sB) * delta + + return wx.Colour(tR, tG, tB, (alphaS + alphaE)/2) \ No newline at end of file