Beispiel #1
0
 def setColor(key: CfgKey, color: QColor):
     valueAsString = str(color.getHsv()[0]) + ";" + str(
         color.getHsv()[1]) + ";" + str(color.getHsv()[2])
     if cfgValue[key] != valueAsString:
         cfgValue[key] = valueAsString
         propertiesService = PropertiesService()
         propertiesService.set(key, valueAsString)
Beispiel #2
0
class ColorBar(QWidget):
    def __init__(self, orientation, *args):
        super().__init__(*args)
        self.orientation = orientation
        self.light = QColor(Qt.gray)
        self.dark = QColor(Qt.black)
        self.height = 100

    def paintEvent(self, _):
        painter = QPainter(self)
        self.draw_color_bar(painter, self.rect())

    def grey(self):
        self.light = QColor(Qt.gray)
        self.dark = QColor(Qt.black)
        self.update()

    def color(self):
        self.light = QColor(Qt.red)
        self.dark = QColor(Qt.green)
        self.update()

    def set_height(self, height):
        self.height = height
        self.update()

    def draw_color_bar(self, painter, rect):
        h1, s1, v1, _ = self.light.getHsv()
        h2, s2, v2, _ = self.dark.getHsv()

        painter.save()
        painter.setClipRect(rect)
        painter.setClipping(True)

        if self.orientation == Qt.Horizontal:
            num_intervalls = rect.width()
        else:
            num_intervalls = rect.height()

        section = QRect()

        num_intervalls_shown = (num_intervalls * self.height) // 100
        l = list(range(num_intervalls - num_intervalls_shown, num_intervalls))
        l.reverse()
        for i in l:
            if self.orientation == Qt.Horizontal:
                section.setRect(rect.x() + i, rect.y(), 1, rect.height())
            else:
                section.setRect(rect.x(), rect.y() + i, rect.width(), 1)

            ratio = float(i) / float(num_intervalls)
            color = QColor()
            color.setHsv(h1 + int(ratio * (h2 - h1) + 0.5),
                         s1 + int(ratio * (s2 - s1) + 0.5),
                         v1 + int(ratio * (v2 - v1) + 0.5))

            painter.fillRect(section, color)

        painter.restore()
Beispiel #3
0
def interpolate_color(
        start: QColor,
        end: QColor,
        percent: int,
        colorspace: typing.Optional[QColor.Spec] = QColor.Rgb
) -> QColor:
    """Get an interpolated color value.

    Args:
        start: The start color.
        end: The end color.
        percent: Which value to get (0 - 100)
        colorspace: The desired interpolation color system,
                    QColor::{Rgb,Hsv,Hsl} (from QColor::Spec enum)
                    If None, start is used except when percent is 100.

    Return:
        The interpolated QColor, with the same spec as the given start color.
    """
    qtutils.ensure_valid(start)
    qtutils.ensure_valid(end)

    if colorspace is None:
        if percent == 100:
            return QColor(*end.getRgb())
        else:
            return QColor(*start.getRgb())

    out = QColor()
    if colorspace == QColor.Rgb:
        a_c1, a_c2, a_c3, _alpha = start.getRgb()
        b_c1, b_c2, b_c3, _alpha = end.getRgb()
        components = _get_color_percentage(a_c1, a_c2, a_c3, b_c1, b_c2, b_c3,
                                           percent)
        out.setRgb(*components)
    elif colorspace == QColor.Hsv:
        a_c1, a_c2, a_c3, _alpha = start.getHsv()
        b_c1, b_c2, b_c3, _alpha = end.getHsv()
        components = _get_color_percentage(a_c1, a_c2, a_c3, b_c1, b_c2, b_c3,
                                           percent)
        out.setHsv(*components)
    elif colorspace == QColor.Hsl:
        a_c1, a_c2, a_c3, _alpha = start.getHsl()
        b_c1, b_c2, b_c3, _alpha = end.getHsl()
        components = _get_color_percentage(a_c1, a_c2, a_c3, b_c1, b_c2, b_c3,
                                           percent)
        out.setHsl(*components)
    else:
        raise ValueError("Invalid colorspace!")
    out = out.convertTo(start.spec())
    qtutils.ensure_valid(out)
    return out
Beispiel #4
0
def interpolate_color(
        start: QColor,
        end: QColor,
        percent: int,
        colorspace: Optional[QColor.Spec] = QColor.Rgb) -> QColor:
    """Get an interpolated color value.

    Args:
        start: The start color.
        end: The end color.
        percent: Which value to get (0 - 100)
        colorspace: The desired interpolation color system,
                    QColor::{Rgb,Hsv,Hsl} (from QColor::Spec enum)
                    If None, start is used except when percent is 100.

    Return:
        The interpolated QColor, with the same spec as the given start color.
    """
    ensure_valid(start)
    ensure_valid(end)

    if colorspace is None:
        if percent == 100:
            return QColor(*end.getRgb())
        else:
            return QColor(*start.getRgb())

    out = QColor()
    if colorspace == QColor.Rgb:
        r1, g1, b1, a1 = start.getRgb()
        r2, g2, b2, a2 = end.getRgb()
        components = _get_color_percentage(r1, g1, b1, a1, r2, g2, b2, a2,
                                           percent)
        out.setRgb(*components)
    elif colorspace == QColor.Hsv:
        h1, s1, v1, a1 = start.getHsv()
        h2, s2, v2, a2 = end.getHsv()
        components = _get_color_percentage(h1, s1, v1, a1, h2, s2, v2, a2,
                                           percent)
        out.setHsv(*components)
    elif colorspace == QColor.Hsl:
        h1, s1, l1, a1 = start.getHsl()
        h2, s2, l2, a2 = end.getHsl()
        components = _get_color_percentage(h1, s1, l1, a1, h2, s2, l2, a2,
                                           percent)
        out.setHsl(*components)
    else:
        raise ValueError("Invalid colorspace!")
    out = out.convertTo(start.spec())
    ensure_valid(out)
    return out
def getSkinToneMessage(color: QColor) -> Tuple[ErrorLevel, str]:
    error, ok, success = lambda m: (ErrorLevel.bad, m + " ✘"), lambda m: (
        ErrorLevel.ok, m), lambda m: (ErrorLevel.good, m + " ✔")

    hue, sat, bright, _ = color.getHsv()
    sat, bright = sat / 2.55, bright / 2.55  # Convert to percentage

    hue_diff = _getHueDiff(hue)
    if hue_diff > _LIMIT_HUE_2:
        return error("The hue is off, adjust the tint.")
    if hue_diff > _LIMIT_HUE_1:
        return ok("The hue is a little off, adjust the tint.")

    bright_diff = _getBrightDiff(bright)
    if bright_diff < -_LIMIT_BRIGHT_2: return error("The brightness is low.")
    if bright_diff < -_LIMIT_BRIGHT_1:
        return ok("The brightness is a little low.")
    if bright_diff > _LIMIT_BRIGHT_2: return error("The brightness is high.")
    if bright_diff > _LIMIT_BRIGHT_1:
        return ok("The brightness is a little high.")

    sat_result = _getSatResult(sat, bright)
    if sat_result == _SaturationResult.low_limit_2:
        return error("The saturation is low.")
    if sat_result == _SaturationResult.low_limit_1:
        return ok("The saturation is a little low.")
    if sat_result == _SaturationResult.high_limit_2:
        return error("The saturation is high.")
    if sat_result == _SaturationResult.high_limit_1:
        return ok("The saturation is a little high.")

    return success("The skin tone is perfect")
Beispiel #6
0
class ColorController:

    def __init__(self, win: Ui_MainWindow):
        self.win = win
        self.color = QColor()
        pixmap = QPixmap('window\\resources\\multicolored-circle.png')
        self.pixmap = pixmap.scaledToHeight(win.l_color_role.height())
        win.l_color_role.setPixmap(self.pixmap)
        win.l_color_role.mousePressEvent = self._get_pixel
        win.sl_saturation.valueChanged.connect(self._slider_change_event)
        win.sl_value.valueChanged.connect(self._slider_change_event)
        self._slider_change_event()
        self.set_gradient_in_saturation_slider()


    def _get_pixel(self, event: QMouseEvent):
        x = event.pos().x()
        y = event.pos().y()
        c = self.pixmap.toImage().pixel(x,y)  # color code (integer): 3235912
        self.color.setRgba(c)  # color object
        self.set_gradient_in_saturation_slider()
        self._slider_change_event(event)

    def set_gradient_in_saturation_slider(self):
        self.win.sl_saturation.setStyleSheet(f'''QSlider::groove:horizontal {{
                                                    background:qlineargradient(x1:0, y1:0, x2:1, y2:0,
                                                    stop:0 #fff, stop:1 rgb{self.color.getRgb()},);
                                                    height: 10px;
                                                }}
                                                QSlider::handle::horizontal
                                                {{
                                                    background: #333;
                                                    width:8px;
                                                    margin: -6px 0;
                                                }}
                                                ''')

    def _slider_change_event(self, event=None):
        self.color.setHsv(self.color.getHsv()[0], self.win.sl_saturation.value(), self.win.sl_value.value())
        self.win.l_current_color.setText(self.color.name().upper())
        text_color = QColor()
        if np.sum(self.color.getRgb()) > 470:
            text_color.setNamedColor('#000')
        else:
            text_color.setNamedColor('#fff')
        self.win.l_current_color.setStyleSheet(
            f'background-color:rgb{self.color.getRgb()}; color : rgb{text_color.getRgb()};')
Beispiel #7
0
class ClassLabel(QListWidgetItem):
    def __init__(self, class_label, color, rect=None, *args, **kwargs):
        super(QListWidgetItem, self).__init__(*args, **kwargs)
        self.label = class_label
        self.color = QColor(color)
        # Get the hue, sat, value so we can change value to 255
        h, s, v, a = self.color.getHsv()
        # This makes the black text more readable
        self.color.setHsv(h, s, 255)
        if rect is not None:  # If the ClassLabel is going into TagsFrameList
            self.setText(f'{self.label}, {str(rect)}')
        else:  # If the ClassLabel is going elsewhere
            self.setText(f'{self.label}')
        self.setBackground(self.color)

    def change_color(self):
        # Sets the background color
        self.setText(f'{self.label}')
        self.setBackground(self.color)

    def add_color_text(self):
        # Display the color name in the list
        self.setText(f'{self.label} - {self.color.getRgb()}')
 def updateMessage(self, color):
     color = QColor(color)
     h, s, v, _ = color.getHsv()
     self._setMessage(color.name(), h, s / 2.55, v / 2.55)
Beispiel #9
0
def create_palette(window_color: QColor, highlight_color: QColor) -> QPalette:
    """
    根据窗口样式window_color 和 高亮(选中)颜色创建窗口风格的palette
    :param window_color: 目标窗口样式
    :param highlight_color: 目标高亮样式
    :return: QPalette
    """

    # 根据HSV样式模型,获取该颜色的 hue色调 sat饱和度 和value明度(最后一个是透明度)
    hue, sat, window_value, _ = window_color.getHsv()

    color_from_value = lambda value: QColor.fromHsv(hue, sat,
                                                    bound(0, value, 255))

    is_light = window_value > 128
    base_value = window_value + 48 if is_light else window_value - 24

    light_text_value = min(255, window_value + 160)
    dark_text_value = max(0, window_value - 160)

    # 根据界面的元素设置字体颜色以便以后可以看清字体
    light_text_color = QColor(light_text_value, light_text_value,
                              light_text_value)
    dark_text_color = QColor(dark_text_value, dark_text_value, dark_text_value)
    light_disabled_text_color = QColor(light_text_value, light_text_value,
                                       light_text_value)
    dark_disabled_text_color = QColor(dark_text_value, dark_text_value,
                                      dark_text_value)

    palette = QPalette(color_from_value(window_value))
    palette.setColor(QPalette.Base, color_from_value(base_value))
    palette.setColor(QPalette.AlternateBase, color_from_value(base_value - 10))
    palette.setColor(QPalette.WindowText,
                     dark_text_color if is_light else light_text_color)
    palette.setColor(QPalette.ButtonText,
                     dark_text_color if is_light else light_text_color)
    palette.setColor(QPalette.Text,
                     dark_text_color if is_light else light_text_color)
    palette.setColor(QPalette.Light, color_from_value(window_value + 55))
    palette.setColor(QPalette.Dark, color_from_value(window_value - 55))
    palette.setColor(QPalette.Mid, color_from_value(window_value - 27))
    palette.setColor(QPalette.Midlight, color_from_value(window_value + 27))

    # 按组设置颜色
    palette.setColor(
        QPalette.Disabled, QPalette.WindowText,
        dark_disabled_text_color if is_light else light_disabled_text_color)
    palette.setColor(
        QPalette.Disabled, QPalette.ButtonText,
        dark_disabled_text_color if is_light else light_disabled_text_color)
    palette.setColor(
        QPalette.Disabled, QPalette.Text,
        dark_disabled_text_color if is_light else light_disabled_text_color)

    # 高亮的颜色是否偏黑
    from PyQt5.Qt import qGray
    highlight_is_dark = qGray(highlight_color.rgb()) < 120
    palette.setColor(QPalette.Highlight, highlight_color)
    palette.setColor(QPalette.HighlightedText,
                     Qt.white if highlight_is_dark else Qt.black)

    return palette