11# -*- coding: utf-8 -*-
22
3- from qwt .qt .QtGui import QColor , qRed , qGreen , qBlue , qRgb , qRgba
3+ from qwt .qt .QtGui import QColor , qRed , qGreen , qBlue , qRgb , qRgba , qAlpha
44from qwt .qt .QtCore import Qt , qIsNaN
55
66
77class ColorStop (object ):
8- def __init__ (self , pos , color ):
8+ def __init__ (self , pos = 0. , color = None ):
99 self .pos = pos
10- self .rgb = color .rgb ()
10+ if color is None :
11+ self .rgb = 0L
12+ else :
13+ self .rgb = color .rgba ()
1114 self .r = qRed (self .rgb )
1215 self .g = qGreen (self .rgb )
1316 self .b = qBlue (self .rgb )
17+ self .a = qAlpha (self .rgb )
18+
19+ # when mapping a value to rgb we will have to calcualate:
20+ # - const int v = int( ( s1.v0 + ratio * s1.vStep ) + 0.5 );
21+ # Thus adding 0.5 ( for rounding ) can be done in advance
22+ self .r0 = self .r + 0.5
23+ self .g0 = self .g + 0.5
24+ self .b0 = self .b + 0.5
25+ self .a0 = self .a + 0.5
26+
27+ self .rStep = self .gStep = self .bStep = self .aStep = 0.
28+ self .posStep = 0.
29+
30+ def updateSteps (self , nextStop ):
31+ self .rStep = nextStop .r - self .r
32+ self .gStep = nextStop .g - self .g
33+ self .bStep = nextStop .b - self .b
34+ self .aStep = nextStop .a - self .a
35+ self .posStep = nextStop .pos - self .pos
1436
1537
1638class ColorStops (object ):
1739 def __init__ (self ):
18- self ._stops = []
40+ self .__doAlpha = False
41+ self .__stops = []
1942 self .stops = []
2043
2144 def insert (self , pos , color ):
2245 if pos < 0. or pos > 1. :
2346 return
24- if len (self ._stops ) == 0 :
47+ if len (self .__stops ) == 0 :
2548 index = 0
26- self ._stops = [None ]
49+ self .__stops = [None ]
2750 else :
2851 index = self .findUpper (pos )
29- if index == len (self ._stops ) or \
30- abs (self ._stops [index ].pos - pos ) >= .001 :
31- self ._stops .append (None )
32- for i in range (len (self ._stops )- 1 , index , - 1 ):
33- self ._stops [i ] = self ._stops [i - 1 ]
34- self ._stops [index ] = ColorStop (pos , color )
52+ if index == len (self .__stops ) or \
53+ abs (self .__stops [index ].pos - pos ) >= .001 :
54+ self .__stops .append (None )
55+ for i in range (len (self .__stops )- 1 , index , - 1 ):
56+ self .__stops [i ] = self .__stops [i - 1 ]
57+ self .__stops [index ] = ColorStop (pos , color )
58+ if color .alpha () != 255 :
59+ self .__doAlpha = True
60+ if index > 0 :
61+ self .__stops [index - 1 ].updateSteps (self .__stops [index ])
62+ if index < len (self .__stops )- 1 :
63+ self .__stops [index ].updateSteps (self .__stops [index + 1 ])
3564
3665 def stops (self ):
37- return [stop .pos for stop in self ._stops ]
66+ return [stop .pos for stop in self .__stops ]
3867
3968 def findUpper (self , pos ):
4069 index = 0
41- n = len (self ._stops )
70+ n = len (self .__stops )
4271
43- stops = self ._stops
72+ stops = self .__stops
4473
4574 while n > 0 :
4675 half = n >> 1
@@ -54,23 +83,27 @@ def findUpper(self, pos):
5483
5584 def rgb (self , mode , pos ):
5685 if pos <= 0. :
57- return self ._stops [0 ].rgb
86+ return self .__stops [0 ].rgb
5887 if pos >= 1.0 :
59- return self ._stops [- 1 ].rgb
88+ return self .__stops [- 1 ].rgb
6089
6190 index = self .findUpper (pos )
6291 if mode == QwtLinearColorMap .FixedColors :
63- return self ._stops [index - 1 ].rgb
92+ return self .__stops [index - 1 ].rgb
6493 else :
65- s1 = self ._stops [index - 1 ]
66- s2 = self ._stops [index ]
67- ratio = (pos - s1 .pos )/ (s2 .pos - s1 .pos )
68- r = s1 .r + round (ratio * (s2 .r - s1 .r ))
69- g = s1 .g + round (ratio * (s2 .g - s1 .g ))
70- b = s1 .b + round (ratio * (s2 .b - s1 .b ))
71- return qRgb (r , g , b )
72-
73-
94+ s1 = self .__stops [index - 1 ]
95+ ratio = (pos - s1 .pos )/ s1 .posStep
96+ r = int (s1 .r0 + ratio * s1 .rStep )
97+ g = int (s1 .g0 + ratio * s1 .gStep )
98+ b = int (s1 .b0 + ratio * s1 .bStep )
99+ if self .__doAlpha :
100+ if s1 .aStep :
101+ a = int (s1 .a0 + ratio * s1 .aStep )
102+ return qRgba (r , g , b , a )
103+ else :
104+ return qRgba (r , g , b , s1 .a )
105+ else :
106+ return qRgb (r , g , b )
74107
75108
76109class QwtColorMap (object ):
@@ -85,7 +118,7 @@ def __init__(self, format_=None):
85118
86119 def color (self , interval , value ):
87120 if self .__format == self .RGB :
88- return QColor (self .rgb (interval , value ))
121+ return QColor . fromRgba (self .rgb (interval , value ))
89122 else :
90123 index = self .colorIndex (interval , value )
91124 return self .colorTable (interval )[index ]
@@ -156,11 +189,11 @@ def color2(self):
156189
157190 def rgb (self , interval , value ):
158191 if qIsNaN (value ):
159- return qRgba ( 0 , 0 , 0 , 0 )
192+ return 0L
160193 width = interval .width ()
161- ratio = 0.
162- if width > 0. :
163- ratio = (value - interval .minValue ())/ width
194+ if width < = 0. :
195+ return 0L
196+ ratio = (value - interval .minValue ())/ width
164197 return self .__data .colorStops .rgb (self .__data .mode , ratio )
165198
166199 def colorIndex (self , interval , value ):
@@ -171,40 +204,40 @@ def colorIndex(self, interval, value):
171204 return 255
172205 ratio = (value - interval .minValue ())/ width
173206 if self .__data .mode == self .FixedColors :
174- index = ratio * 255
207+ return int ( ratio * 255 )
175208 else :
176- index = round (ratio * 255 )
177- return index
209+ return int (ratio * 255 + .5 )
178210
179211
180212class QwtAlphaColorMap_PrivateData (object ):
181213 def __init__ (self ):
182214 self .color = None
183215 self .rgb = None
216+ self .rgbMax = None
184217
185218class QwtAlphaColorMap (QwtColorMap ):
186219 def __init__ (self , color ):
187220 super (QwtAlphaColorMap , self ).__init__ (QwtColorMap .RGB )
188221 self .__data = QwtAlphaColorMap_PrivateData ()
189- self .__data .color = color
190- self .__data .rgb = color .rgb () & qRgba (255 , 255 , 255 , 0 )
222+ self .setColor (color )
191223
192224 def setColor (self , color ):
193225 self .__data .color = color
194- self .__data .rgb = color .rgb ()
226+ self .__data .rgb = color .rgb () & qRgba (255 , 255 , 255 , 0 )
227+ self .__data .rgbMax = self .__data .rgb | ( 255 << 24 )
195228
196229 def color (self ):
197230 return self .__data .color ()
198231
199232 def rgb (self , interval , value ):
233+ if qIsNaN (value ):
234+ return 0L
200235 width = interval .width ()
201- if not qIsNaN (value ) and width >= 0. :
202- ratio = (value - interval .minValue ())/ width
203- alpha = round (255 * ratio )
204- if alpha < 0 :
205- alpha = 0
206- if alpha > 255 :
207- alpha = 255
208- return self .__data .rgb | (alpha << 24 )
209- return self .__data .rgb
210-
236+ if width <= 0. :
237+ return 0L
238+ if value <= interval .minValue ():
239+ return self .__data .rgb
240+ if value >= interval .maxValue ():
241+ return self .__data .rgbMax
242+ ratio = (value - interval .minValue ())/ width
243+ return self .__data .rgb | (int (round (255 * ratio )) << 24 )
0 commit comments