Ejemplo n.º 1
0
  def setColor(self, color, bgColor='grey'):

    if type(color) is type(()) :
      (r,g,b) = color
      color   = hexRepr(r,g,b)
      
    if type(bgColor) is type(()) :
      (r,g,b) = bgColor
      bgColor = hexRepr(r,g,b)
    
    self.config(bg = color)
    
    if self.relief in ('solid','flat'):
      self.innerFrame.config(bg = bgColor)
    else:
      self.innerFrame.config(bg = color)
Ejemplo n.º 2
0
 def getAtomColor(self, i,N):
   
   h = i/float(N)
   s = 1
   v = 0.8
   [r,g,b] = hsbToRgb(h,s,v)
  
   return hexRepr(r,g,b)
Ejemplo n.º 3
0
    def setColorSchemes(self, *color):

        schemes = self.getColorSchemes()
        names = [scheme.name for scheme in schemes]
        #names.sort()
        if (self.extra_label):
            names = names + [self.extra_label]
        #print 'setColorSchemes', names, self.selected_index

        hexColors = []
        for scheme in schemes:
            schemeColors = []
            for c in scheme.colors:
                schemeColors.append(hexRepr(c.r, c.g, c.b))

            hexColors.append(schemeColors)

        self.replace(names, self.selected_index, colors=hexColors)
Ejemplo n.º 4
0
    def setColor(self, color):

        (r, g, b) = color
        self.canvas.itemconfig(self.item, fill=hexRepr(r, g, b))
Ejemplo n.º 5
0
    def draw(self):

        if self.matrix is None:
            return

        c = self.canvas
        canvasDict = self.canvasDict
        matrix = self.matrix
        boxes = self.boxes
        legend = self.legend
        w = max(3, int(self.boxSize * self.zoom))
        h = max(3, int(self.boxSize * self.zoom))
        h2 = h / 2.0
        w2 = w / 2.0
        N = len(matrix)
        M = len(matrix[0])

        createRect = c.create_rectangle
        createText = c.create_text
        cCoords = c.coords
        cConfig = c.itemconfigure
        xGrid = self.xGrid
        yGrid = self.yGrid
        barPlot = self.barPlot

        for item in self.miscItems:
            c.delete(item)

        if self.border:
            cCoords(self.border, 0, 0, (N * w) + (2 * self.indent),
                    (M * h) + (2 * self.indent))
        else:
            self.border = createRect(
                0,
                0,
                self.origin[0] + (N * w) + (2 * self.indent),
                self.origin[1] + (M * h) + (2 * self.indent),
                outline='white',
                width=1)

        if self.title:
            midWay = self.origin[0] + ((N * w) + (2 * self.indent)) / 2.0
            item = createText(midWay,
                              self.origin[1] + self.indent - h,
                              font=self.boldFont,
                              text=self.title,
                              anchor='s',
                              fill='black')
            self.miscItems.append(item)

        if self.xAxisLabel:
            midWay = self.origin[0] + ((N * w) + (2 * self.indent)) / 2.0
            item = createText(midWay,
                              self.origin[1] + (M * h) + self.indent + h + h +
                              h,
                              font=self.boldFont,
                              text=self.xAxisLabel,
                              anchor='s',
                              fill='black')
            self.miscItems.append(item)

        if self.yAxisLabel:
            item = createText(self.indent - h2,
                              self.origin[1] + self.indent - h2,
                              font=self.boldFont,
                              text=self.yAxisLabel,
                              anchor='s',
                              fill='black')
            self.miscItems.append(item)

        if len(xGrid) > N + 1:
            for i in range(N + 1, len(xGrid)):
                c.delete(xGrid[i])

        if len(yGrid) > M + 1:
            for j in range(M + 1, len(yGrid)):
                c.delete(yGrid[j])

        x = self.indent + self.origin[0]
        y = self.indent + self.origin[1]
        for j in range(M + 1):

            if j >= len(yGrid):
                yGrid.append(None)

            if yGrid[j]:
                c.coords(yGrid[j], x, y, x + (N * h), y)
            else:
                yGrid[j] = self.makeLine(x, y, x + (N * h), y)
                canvasDict[yGrid[j]][0] = 'gridY'
                canvasDict[yGrid[j]][1] = j
            y += h

        y = self.indent + self.origin[1]
        for i in range(N + 1):

            if i >= len(xGrid):
                xGrid.append(None)

            if xGrid[i]:
                c.coords(xGrid[i], x, y, x, y + (M * w))
            else:
                xGrid[i] = self.makeLine(x, y, x, y + (M * w))
                canvasDict[xGrid[i]][0] = 'gridX'
                canvasDict[xGrid[i]][1] = i
            x += w

        x = self.indent + self.origin[0]
        y = self.indent + self.origin[1]
        makeBox = self.makeBox
        setBoxCoords = self.setBoxCoords
        configureBox = self.configureBox
        deleteBox = self.deleteBox
        maxVal = self.maxVal
        labelAxes = self.labelAxes
        yLabels = self.yLabels
        xLabels = self.xLabels

        if labelAxes:
            for j in range(M):
                yText = createText(x - w2,
                                   y + h2,
                                   font=self.normFont,
                                   text=yLabels[j],
                                   anchor='e',
                                   fill='black')
                self.miscItems.append(yText)
                y += h

        dv1 = (maxVal - self.minVal) / 20.0
        order = int(round((log(dv1) / log10)))
        dv = round(dv1, -order) + 10**order
        start = (round(maxVal / dv)) * dv
        end = min(0.0, (round(self.minVal / dv)) * dv)
        maxDiff = max(abs(start), abs(end))

        for i in range(N):
            y = self.indent + self.origin[1]
            if i >= len(boxes):
                boxes.append([])

            for j in range(M):
                if j >= len(boxes[i]):
                    boxes[i].append(None)

                if barPlot:
                    v = matrix[i][j]

                    if v < 0:
                        color = '#A00000'
                        f = -min(1.0, v / maxDiff)
                        yA = y
                        yB = y + (f * h)
                    elif v >= 0:
                        color = '#0000A0'
                        f = min(1.0, v / maxDiff)
                        yB = y + h
                        yA = yB - (f * h)

                    item = boxes[i][j]
                    if item is None:
                        boxes[i][j] = makeBox(c,
                                              x,
                                              yA,
                                              i,
                                              j,
                                              width=w,
                                              height=yB - yA,
                                              fillColor=color)
                    else:
                        #setBoxCoords(c,i,j,x,y,w,h) # Substituted for speed
                        cCoords(item, x, yA, x + w, yB)

                        #configureBox(c,i,j,color) # Substituted for speed
                        cConfig(item, fill=color)

                else:  # Normal colour density

                    #idx = 1-(min(matrix[i][j],maxVal)/float(maxVal))
                    #color = hexRepr(idx,idx,idx)
                    v = matrix[i][j]

                    if v < 0:
                        idx = -min(1.0, v / maxDiff)
                        color = hexRepr(1 - (0.5 * idx), 1 - idx, 1 - idx)

                    elif v > 0:
                        idx = min(1.0, v / maxDiff)
                        color = hexRepr(1 - idx, 1 - idx, 1 - (0.5 * idx))

                    else:
                        color = hexRepr(1, 1, 1)

                    item = boxes[i][j]
                    if item is None:
                        boxes[i][j] = makeBox(c,
                                              x,
                                              y,
                                              i,
                                              j,
                                              width=w,
                                              height=h,
                                              fillColor=color)
                    else:
                        #setBoxCoords(c,i,j,x,y,w,h) # Substituted for speed
                        cCoords(item, x, y, x + w, y + h)

                        #configureBox(c,i,j,color) # Substituted for speed
                        cConfig(item, fill=color)

                y += h

            if labelAxes:
                xText = createText(x + w2,
                                   y + h2,
                                   font=self.normFont,
                                   text=xLabels[i],
                                   anchor='n',
                                   fill='black')
                self.miscItems.append(xText)

            x += w

            # cleanup end of rows
            if len(boxes[i]) > M:
                for j in range(M, len(boxes[i])):
                    if boxes[i][j]:
                        deleteBox(i, j)
                boxes[i] = boxes[i][0:M]

        # clean up unused rows
        if len(boxes) > N:
            for i in range(N, len(boxes)):
                for j in range(len(boxes[i])):
                    if boxes[i][j]:
                        deleteBox(i, j)
            boxes = boxes[0:N]

        if not self.doLegend:
            return

        # Create legend
        x += w + 2
        y = self.indent + self.origin[1]

        borderColor = self.borderColor

        if -5 < order < 6:
            textFormat = '%%.%df' % max(1, -order)
        else:
            textFormat = '%.2e'

        # TBD should have text bounding box here
        sz = max([w, h])
        B = int((start - end) / dv) + 1
        v = start
        for i in range(B):
            v = start - (i * dv)
            text = '?'

            if barPlot:
                if v < 0:
                    color = '#A00000'
                    f = -min(1.0, v / maxDiff)
                    yA = y
                    yB = y + (f * sz)
                    text = textFormat % v
                elif v >= 0:
                    color = '#0000A0'
                    f = min(1.0, v / maxDiff)
                    yB = y + sz
                    yA = yB - (f * sz)
                    text = ' ' + textFormat % v

                if len(legend) < B:
                    itemA = createRect(x,
                                       yA,
                                       x + sz,
                                       yB,
                                       fill=color,
                                       outline=borderColor,
                                       width=1)
                    itemB = createText(x + sz + sz / 2,
                                       y,
                                       font=self.normFont,
                                       text=text,
                                       anchor='w',
                                       fill='black')
                    itemC = createRect(x,
                                       yA,
                                       x + sz,
                                       yB,
                                       outline=borderColor,
                                       width=1)
                    legend.append((itemA, itemB, itemC))
                #else:

                itemA, itemB, itemC = legend[i]
                cCoords(itemA, x, yA, x + sz, yB)
                cCoords(itemB, x + sz + sz / 2, y + sz / 2)
                cCoords(itemC, x, y, x + sz, y + sz)
                cConfig(itemA, fill=color, outline=borderColor)
                cConfig(itemB, text=text, font=self.normFont)
                cConfig(itemC, outline=borderColor)

            else:

                if v < 0:
                    idx = -v / maxDiff
                    color = hexRepr(1 - (0.5 * idx), 1 - idx, 1 - idx)
                    text = textFormat % v

                elif v >= 0:
                    idx = v / maxDiff
                    color = hexRepr(1 - idx, 1 - idx, 1 - (0.5 * idx))
                    text = ' ' + textFormat % v

                if len(legend) < B:
                    itemA = createRect(x,
                                       y,
                                       x + sz,
                                       y + sz,
                                       fill=color,
                                       outline=borderColor,
                                       width=1)
                    itemB = createText(x + sz + sz / 2,
                                       y,
                                       font=self.normFont,
                                       text=text,
                                       anchor='w',
                                       fill='black')
                    legend.append((itemA, itemB))
                #else:

                itemA, itemB = legend[i]
                cCoords(itemA, x, y, x + sz, y + sz)
                cCoords(itemB, x + sz + sz / 2, y + sz / 2)
                cConfig(itemA, fill=color, outline=borderColor)
                cConfig(itemB, text=text, font=self.normFont)

            y += sz

        while len(legend) > B:
            items = legend.pop()

            for item in items:
                c.delete(item)

        self.legend = legend