Ignore:
Timestamp:
11/20/08 16:52:27 (5 years ago)
Author:
ales_erjavec <ales.erjavec@…>
Branch:
default
Convert:
af7a148e21a2e21d09ceab80ea7b9ead6821b89c
Message:

-added added extended palette generation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • orange/OrangeWidgets/OWColorPalette.py

    r4972 r5559  
    6464        self.callback = callback 
    6565        self.contPaletteNames = [] 
     66        self.exContPaletteNames = [] 
    6667        self.discPaletteNames = [] 
    6768        self.colorButtonNames = [] 
     
    120121        self.__dict__["cont"+paletteName+"passThroughBlackCheckbox"] = OWGUI.checkBox(buttBox, self, "cont"+paletteName+"passThroughBlack", "Pass through black", callback = self.colorSchemaChange) 
    121122        self.contPaletteNames.append(paletteName) 
     123 
     124    def createExtendedContinuousPalette(self, paletteName, boxCaption, passThroughColors = 0, initialColor1 = QColor(Qt.white), initialColor2 = Qt.black, extendedPassThroughColors = ((Qt.red, 1), (Qt.black, 1), (Qt.green, 1))): 
     125        buttBox = OWGUI.widgetBox(self.mainArea, boxCaption)  
     126        box = OWGUI.widgetBox(buttBox, orientation = "horizontal") 
     127         
     128        self.__dict__["exCont"+paletteName+"Left"]  = ColorButton(self, box, color = QColor(initialColor1)) 
     129        self.__dict__["exCont"+paletteName+"View"] = PaletteView(box) 
     130        self.__dict__["exCont"+paletteName+"Right"] = ColorButton(self, box, color = QColor(initialColor2)) 
     131 
     132        self.__dict__["exCont"+paletteName+"passThroughColors"] = passThroughColors 
     133        self.__dict__["exCont"+paletteName+"passThroughColorsCheckbox"] = OWGUI.checkBox(buttBox, self, "exCont"+paletteName+"passThroughColors", "Use pass-through colors", callback = self.colorSchemaChange) 
     134 
     135        box = OWGUI.widgetBox(buttBox, "Pass-through colors", orientation = "horizontal") 
     136        for i, (color, check) in enumerate(extendedPassThroughColors): 
     137            self.__dict__["exCont"+paletteName+"passThroughColor"+str(i)] = check 
     138            self.__dict__["exCont"+paletteName+"passThroughColor"+str(i)+"Checkbox"] = OWGUI.checkBox(box, self, "exCont"+paletteName+"passThroughColor"+str(i), "", tooltip="Use color", callback = self.colorSchemaChange) 
     139            self.__dict__["exCont"+paletteName+"color"+str(i)] = ColorButton(self, box, color = QColor(color)) 
     140        self.__dict__["exCont"+paletteName+"colorCount"] = len(extendedPassThroughColors) 
     141        self.exContPaletteNames.append(paletteName)             
     142         
    122143 
    123144    # ##################################################### 
     
    173194        return ContinuousPaletteGenerator(c1, c2, b) 
    174195 
     196    def getExtendedContinuousPalette(self, paletteName): 
     197        c1 = self.__dict__["exCont"+paletteName+"Left"].getColor() 
     198        c2 = self.__dict__["exCont"+paletteName+"Right"].getColor() 
     199        colors = self.__dict__["exCont"+paletteName+"passThroughColors"] 
     200        if colors: 
     201            colors = [self.__dict__["exCont"+paletteName+"color"+str(i)].getColor() 
     202                      for i in range(self.__dict__["exCont"+paletteName+"colorCount"]) 
     203                      if self.__dict__["exCont"+paletteName+"passThroughColor"+str(i)]] 
     204        return ExtendedContinuousPaletteGenerator(c1, c2, colors or []) 
     205 
    175206    def getDiscretePalette(self, paletteName): 
    176207        return ColorPaletteGenerator(rgbColors = self.__dict__["disc"+paletteName+"View"].rgbColors) 
     
    183214        l2 = [(name, (self.qRgbFromQColor(self.__dict__["cont"+name+"Left"].getColor()), self.qRgbFromQColor(self.__dict__["cont"+name+"Right"].getColor()), self.__dict__["cont"+name+"passThroughBlack"])) for name in self.contPaletteNames] 
    184215        l3 = [(name, self.__dict__["disc"+name+"View"].rgbColors) for name in self.discPaletteNames] 
    185         return [l1, l2, l3] 
     216        l4 = [(name, (self.qRgbFromQColor(self.__dict__["exCont"+name+"Left"].getColor()), self.qRgbFromQColor(self.__dict__["exCont"+name+"Right"].getColor()), self.__dict__["exCont"+name+"passThroughColors"], 
     217                      [(self.qRgbFromQColor(self.__dict__["exCont"+name+"color"+str(i)].getColor()), self.__dict__["exCont"+name+"passThroughColor"+str(i)]) 
     218                       for i in range(self.__dict__["exCont"+name+"colorCount"])])) 
     219                       for name in self.exContPaletteNames] 
     220        return [l1, l2, l3, l4] 
    186221 
    187222 
     
    199234 
    200235    def setCurrentState(self, state): 
    201         [buttons, contPalettes, discPalettes] = state 
     236        if len(state) > 3: 
     237            [buttons, contPalettes, discPalettes, exContPalettes] = state 
     238        else: 
     239            [buttons, contPalettes, discPalettes] = state 
     240            exContPalettes = [] 
    202241        for (name, but) in buttons: 
    203242            self.__dict__["butt"+name].setColor(self.rgbToQColor(but)) 
     
    211250        for (name, rgbColors) in discPalettes: 
    212251            self.__dict__["disc"+name+"View"].setDiscPalette(rgbColors) 
    213                          
     252 
     253        for name, (l, r, chk, colors) in exContPalettes: 
     254            self.__dict__["exCont"+name+"Left"].setColor(self.rgbToQColor(l)) 
     255            self.__dict__["exCont"+name+"Right"].setColor(self.rgbToQColor(r)) 
     256 
     257            self.__dict__["exCont"+name+"passThroughColors"] = chk 
     258            self.__dict__["exCont"+name+"passThroughColorsCheckbox"].setChecked(chk) 
     259 
     260            colorsList = [] 
     261            for i, (color, check) in enumerate(colors): 
     262                self.__dict__["exCont"+name+"passThroughColor"+str(i)] = check 
     263                self.__dict__["exCont"+name+"passThroughColor"+str(i)+"Checkbox"].setChecked(check) 
     264                self.__dict__["exCont"+name+"color"+str(i)].setColor(self.rgbToQColor(color)) 
     265                if check and chk: 
     266                    colorsList.append(self.rgbToQColor(color)) 
     267            self.__dict__["exCont"+name+"colorCount"] = len(colors) 
     268            self.__dict__["exCont"+name+"View"].setExContPalette(self.rgbToQColor(l), self.rgbToQColor(r), colorsList) 
    214269 
    215270    def paletteSelected(self): 
     
    423478        return QColor(*self.getRGB(val)) 
    424479 
     480class ExtendedContinuousPaletteGenerator: 
     481    def __init__(self, color1, color2, passThroughColors): 
     482        self.colors = [color1] + passThroughColors + [color2] 
     483 
     484    def getRGB(self, val): 
     485        index = int(val * (len(self.colors) - 1)) 
     486        if index == len(self.colors) - 1: 
     487            return (self.colors[-1].red(), self.colors[-1].green(), self.colors[-1].blue()) 
     488        else: 
     489            red1, green1, blue1 = self.colors[index].red(), self.colors[index].green(), self.colors[index].blue() 
     490            red2, green2, blue2 = self.colors[index + 1].red(), self.colors[index + 1].green(), self.colors[index + 1].blue() 
     491            x = val * (len(self.colors) - 1) - index 
     492            return [(c2 - c1) * x + c1 for c1, c2 in [(red1, red2), (green1, green2), (blue1, blue2)]] 
     493##        if self.passThroughBlack: 
     494##            if val < 0.5: 
     495##                return (self.c1Red - self.c1Red*val*2, self.c1Green - self.c1Green*val*2, self.c1Blue - self.c1Blue*val*2) 
     496##            else: 
     497##                return (self.c2Red*(val-0.5)*2., self.c2Green*(val-0.5)*2., self.c2Blue*(val-0.5)*2.) 
     498##        else: 
     499##            return (self.c1Red + (self.c2Red-self.c1Red)*val, self.c1Green + (self.c2Green-self.c1Green)*val, self.c1Blue + (self.c2Blue-self.c1Blue)*val) 
     500 
     501    # val must be between 0 and 1 
     502    def __getitem__(self, val): 
     503        return QColor(*self.getRGB(val)) 
     504 
    425505 
    426506class ColorPaletteGenerator: 
     
    553633        self.color2 = None 
    554634        self.rgbColors = [] 
     635        self.passThroughColors = None 
    555636 
    556637        #self.setFrameStyle(QFrame.NoFrame) 
     
    576657        self.passThroughBlack = passThroughBlack 
    577658        self.updateImage() 
     659 
     660    def setExContPalette(self, color1, color2, passThroughColors): 
     661        self.color1 = color1 
     662        self.color2 = color2 
     663        self.passThroughColors = passThroughColors 
     664        self.updateImage() 
    578665         
    579666    def updateImage(self): 
     
    582669        if self.color1 == None:  
    583670            img = createDiscPalettePixmap(self.width(), self.height(), self.rgbColors) 
    584         else: 
     671        elif self.passThroughColors == None: 
    585672            img = createContPalettePixmap(self.width(), self.height(), self.color1, self.color2, self.passThroughBlack) 
     673        else: 
     674            img = createExContPalettePixmap(self.width(), self.height(), self.color1, self.color2, self.passThroughColors) 
    586675        self.scene().addPixmap(img) 
    587676        self.scene().update() 
     
    619708        p.drawRect(QRectF(i*rectWidth, 0, (i+1)*rectWidth, height)) 
    620709    return img 
     710 
     711# create a pixmap withcolor going from color1 to color2 passing through all intermidiate colors in passThroughColors 
     712def createExContPalettePixmap(width, height, color1, color2, passThroughColors): 
     713    p = QPainter() 
     714    img = QPixmap(width, height) 
     715    p.begin(img) 
     716     
     717    #p.eraseRect(0, 0, w, h) 
     718    p.setPen(QPen(Qt.NoPen)) 
     719    g = QLinearGradient(0, 0, width, height) 
     720    g.setColorAt(0, color1) 
     721    g.setColorAt(1, color2) 
     722    for i, color in enumerate(passThroughColors): 
     723        g.setColorAt(float(i + 1) / (len(passThroughColors) + 1), color) 
     724    p.fillRect(img.rect(), QBrush(g)) 
     725    return img 
    621726         
    622727 
Note: See TracChangeset for help on using the changeset viewer.