Exemplo n.º 1
0
def GLcolor(color):
    """Convert a color to an OpenGL RGB color.

    The output is a tuple of three RGB float values ranging from 0.0 to 1.0.
    The input can be any of the following:

    - a QColor
    - a string specifying the Xwindow name of the color
    - a hex string '#RGB' with 1 to 4 hexadecimal digits per color
    - a tuple or list of 3 integer values in the range 0..255
    - a tuple or list of 3 float values in the range 0.0..1.0

    Any other input may give unpredictable results.

    Examples:
    >>> GLcolor('indianred')
    (0.803921568627451, 0.3607843137254902, 0.3607843137254902)
    >>> print(GLcolor('#ff0000'))
    (1.0, 0.0, 0.0)
    >>> GLcolor(red)
    (1.0, 0.0, 0.0)
    >>> GLcolor([200,200,255])
    (0.7843137254901961, 0.7843137254901961, 1.0)
    >>> GLcolor([1.,1.,1.])
    (1.0, 1.0, 1.0)
    """
    col = color

    # as of Qt4.5, QtGui.Qcolor no longer raises an error if given
    # erroneous input. Therefore, we check it ourselves

    # str or QtCore.Globalcolor: convert to QColor
    if (type(col) is str or isinstance(col, QtCore.Qt.GlobalColor)):
        try:
            col = QtGui.QColor(col)
        except:
            pass

    # QColor: convert to (r,g,b) tuple (0..255)
    if isinstance(col, QtGui.QColor):
        col = (col.red(), col.green(), col.blue())

    # Convert to a list and check length
    try:
        col = tuple(col)
        if len(col) == 3:
            if isInt(col[0]):
                # convert int values to float
                col = [c / 255. for c in col]
            col = map(float, col)
            # SUCCESS !
            return tuple(col)
    except:
        pass

    # No success: raise an error
    raise ValueError, "GLcolor: unexpected input of type %s: %s" % (
        type(color), color)
Exemplo n.º 2
0
    def write(self,s,color=None):
        """Write a string to the message board.

        If a color is specified, the text is shown in the specified
        color, but the default board color remains unchanged.
        """
        if color:
            savecolor = self.textColor()
            self.setTextColor(QtGui.QColor(color))
        else:
            savecolor = None
        # A single blank character seems to be generated by a print
        # instruction containing a comma: skip it
        if s == ' ':
            return
        #self.buffer += '[%s:%s]' % (len(s),s)
        s = s.rstrip('\n')
        if len(s) > 0:
            self.append(s)
            self.cursor.movePosition(QtGui.QTextCursor.End)
            self.setTextCursor(self.cursor)
        if savecolor:
            self.setTextColor(savecolor)