当前位置: 首页 > news >正文

Qt的HSL色彩系统

与常见的HSB色彩系统不同,HSL颜色系统有它独特的色彩设置规律,更适合UI工程的界面主题、模式切换、整体亮度调节、CSS 风格配色等应用。

  • HSL颜色 hsl(h, s, l)的参数含义:
    h:
    色相,范围:0~360°0°/360°:红,120°:绿,240°:蓝(和 HSB 一样)
    s:色彩浓郁度(算法和 HSB 不一样)
    l:亮度,100%:纯白,0%:纯黑,50%:标准纯色

运行脚本,生成一个hsl色彩矩阵图(以绿色为例):

import sys from PySide6.QtWidgets import QLabel, QApplication, QWidget, QHBoxLayout, QVBoxLayout class LabelHsl(QLabel): def __init__(self,hsl, geo, parent=None): super().__init__(parent) self.setGeometry(*geo) self.setStyleSheet(f"background-color: hsl({hsl});color:red") app = QApplication(sys.argv) widget1 = QWidget() v_layout = QVBoxLayout() v_layout.setContentsMargins(0, 0, 0, 0) v_layout.setSpacing(0) h = 120 s = 100 width = 80 height = 80 for i in range(11): widget = QWidget() widget.setFixedSize(width * 10, height) h_layout = QHBoxLayout() h_layout.setContentsMargins(0, 0, 0, 0) h_layout.setSpacing(0) for j in range(11): l = f"{j * 10}%" geo = (j*width, i*height, width, height) hsl = f"{h}, {s}%, {l}" label = LabelHsl(hsl, geo) label.setText(f"{h},{s},{j*10}") h_layout.addWidget(label) j += width widget.setLayout(h_layout) v_layout.addWidget(widget) i += height s -= 10 widget1.setLayout(v_layout) widget1.show() app.exec()

看得出,随着s的递增,色彩逐渐浓郁;随着l的递增,亮度逐渐提高;

据此,创建两个渐变色的小部件:

1、s = 100%(色彩浓度不变),l递增(亮度递增)

background-color: qlineargradient(x1: 0, y1: 0, x2: 1, y2:0, stop: 0 hsl(120, 100%, 0%), stop: 0.2 hsl(120, 100%, 20%), stop: 0.4 hsl(120, 100%, 40%), stop: 0.6 hsl(120, 100%, 60%), stop: 0.8 hsl(120, 100%, 80%), stop: 1 hsl(120, 100%, 100%));

2、l = 50%(亮度不变),s递增(色彩浓度递增)

background-color: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0, stop: 0 hsl(120, 0%, 50%), stop: 0.2 hsl(120, 20%, 50%), stop: 0.4 hsl(120, 40%, 50%), stop: 0.6 hsl(120, 60%, 50%), stop: 0.8 hsl(120, 80%, 50%), stop: 1 hsl(120,100%, 50%));

根据设计需求选择不同的常量和递变量即可。

  • demo:一个绿色带灯按钮

先创建一个灭灯的绿色按钮:

QPushButton {background-color: qlineargradient(x1: 1, y1: 0, x2: 1, y2: 1, stop: 0 hsl(120,35%,90%), stop: 0.15 hsl(120,35%,60%), stop: 0.75 hsl(120,35%,50%), stop: 1.0 hsl(120,35%,35%)); border-top: 2px solid #f0f0f0; border-left: 2px solid #f0f0f0; border-right: 2px solid #303030; border-bottom: 2px solid #303030; border-radius: 5px; } QPushButton:hover{background-color: qlineargradient(x1: 1, y1: 0, x2: 1, y2: 1, stop: 0 hsl(120,35%,85%), stop: 0.1 hsl(120,35%,65%), stop: 0.85 hsl(120,35%,55%), stop: 1.0 hsl(120,35%,50%)); border-top: 1px solid #f0f0f0; border-left: 1px solid #f0f0f0; border-right: 1px solid #303030; border-bottom: 1px solid #303030; } QPushButton:pressed{background-color: hsl(120,35%,70%); border-top: 1px solid #303030; border-left: 1px solid #303030; border-right: 1px solid #f0f0f0; border-bottom: 1px solid #f0f0f0; }

然后把按钮样式表中的所有的色彩浓度由25%批量替换为80%

QPushButton {background-color: qlineargradient(x1: 1, y1: 0, x2: 1, y2: 1, stop: 0 hsl(120,80%,90%), stop: 0.15 hsl(120,80%,60%), stop: 0.75 hsl(120,80%,50%), stop: 1.0 hsl(120,80%,40%)); border-top: 2px solid #f0f0f0; border-left: 2px solid #f0f0f0; border-right: 2px solid #303030; border-bottom: 2px solid #303030; border-radius: 5px; } QPushButton:hover{background-color: qlineargradient(x1: 1, y1: 0, x2: 1, y2: 1, stop: 0 hsl(120,80%,85%), stop: 0.1 hsl(120,80%,65%), stop: 0.85 hsl(120,80%,55%), stop: 1.0 hsl(120,80%,50%)); border-top: 1px solid #f0f0f0; border-left: 1px solid #f0f0f0; border-right: 1px solid #303030; border-bottom: 1px solid #303030; } QPushButton:pressed{background-color: hsl(120,80%,70%); border-top: 1px solid #303030; border-left: 1px solid #303030; border-right: 1px solid #f0f0f0; border-bottom: 1px solid #f0f0f0; }

就可以获得一个亮灯的绿按钮:

继续,把上面两个按钮样式表中的色相从120批量替换为0,其余的不变,就可以获得两个红色按钮:

QPushButton {background-color: qlineargradient(x1: 1, y1: 0, x2: 1, y2: 1, stop: 0 hsl(0,35%,90%), stop: 0.15 hsl(0,35%,60%), stop: 0.75 hsl(0,35%,50%), stop: 1.0 hsl(0,35%,35%)); border-top: 2px solid #f0f0f0; border-left: 2px solid #f0f0f0; border-right: 2px solid #303030; border-bottom: 2px solid #303030; border-radius: 5px; } QPushButton:hover{background-color: qlineargradient(x1: 1, y1: 0, x2: 1, y2: 1, stop: 0 hsl(0,35%,85%), stop: 0.1 hsl(0,35%,65%), stop: 0.85 hsl(0,35%,55%), stop: 1.0 hsl(0,35%,50%)); border-top: 1px solid #f0f0f0; border-left: 1px solid #f0f0f0; border-right: 1px solid #303030; border-bottom: 1px solid #303030; } QPushButton:pressed{background-color: hsl(0,35%,70%); border-top: 1px solid #303030; border-left: 1px solid #303030; border-right: 1px solid #f0f0f0; border-bottom: 1px solid #f0f0f0; }
QPushButton {background-color: qlineargradient(x1: 1, y1: 0, x2: 1, y2: 1, stop: 0 hsl(0,80%,90%), stop: 0.15 hsl(0,80%,60%), stop: 0.75 hsl(0,80%,50%), stop: 1.0 hsl(0,80%,40%)); border-top: 2px solid #f0f0f0; border-left: 2px solid #f0f0f0; border-right: 2px solid #303030; border-bottom: 2px solid #303030; border-radius: 5px; } QPushButton:hover{background-color: qlineargradient(x1: 1, y1: 0, x2: 1, y2: 1, stop: 0 hsl(0,80%,85%), stop: 0.1 hsl(0,80%,65%), stop: 0.85 hsl(0,80%,55%), stop: 1.0 hsl(0,80%,50%)); border-top: 1px solid #f0f0f0; border-left: 1px solid #f0f0f0; border-right: 1px solid #303030; border-bottom: 1px solid #303030; } QPushButton:pressed{background-color: hsl(0,80%,70%); border-top: 1px solid #303030; border-left: 1px solid #303030; border-right: 1px solid #f0f0f0; border-bottom: 1px solid #f0f0f0; }

利用HSL颜色系统,可以很方便地创建迭代方便、颜色体系变量化、高效组态UI工程。

  • 一个自定义颜色系统的demo:
import sys from dataclasses import dataclass from PySide6.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout @dataclass class HSLColor: h: str s: str l: str 红色 = "0" 绿色 = "120" 蓝色 = "240" 橙色 = "39" 黄色 = "60" 紫色 = "260" 棕色 = "20" 青色 = "180" 品红 = "300" colors = [红色, 绿色, 蓝色, 橙色, 黄色, 紫色, 棕色, 青色, 品红] color_names = ["红色", "绿色", "蓝色", "橙色", "黄色", "紫色", "棕色", "青色", "品红"] hs = [] for color in colors: c = HSLColor(h=color, s="70%", l="50%") hs.append(c) i = 0 def toggle_color(): global i if i >= len(colors): i = 0 c = hs[i] style_Sheet = f"""background-color:hsl({c.h},{c.s},{c.l})""" widget.setStyleSheet(style_Sheet) widget.style().unpolish(widget) widget.style().polish(widget) btn.setStyleSheet(style_Sheet) btn.setText(color_names[i]) i += 1 app = QApplication(sys.argv) widget = QWidget() btn = QPushButton("切换颜色") btn.clicked.connect(toggle_color) layout = QVBoxLayout() layout.addWidget(btn) widget.setLayout(layout) widget.show() sys.exit(app.exec())

http://www.cnnetsun.cn/news/2106550.html

相关文章:

  • Linux下AI代码编辑器Cursor自动化安装与系统集成脚本详解
  • 基于PraisonAI的多智能体编排框架:从YAML配置到生产部署全解析
  • 深度学习数据缩放:原理、方法与实践指南
  • DeepSeek辅助解决windows 11 wsl2中启用图形界面
  • 【仅限首批200家示范农场】:MCP 2026农业物联网对接“免调试”配置包泄露——含国密SM4加密模板与北斗授时同步策略
  • 机器学习算法选择指南:构建高效算法清单
  • 用 AI Agent Harness Engineering 自动化你的数据分析工作流
  • 从零搭建AI开发环境:手把手教你用Anaconda管理多个PyTorch+CUDA版本(Ubuntu 20.04/22.04实测)
  • 每日算法-两个数组的dp、哈夫曼编码、子序列dp问题+哈希表
  • 机器学习自学路线:从基础到深度学习实战
  • 【限时开放】Docker官方2026安全基线评估工具(非开源版)内测资格仅剩47席:自动扫描你的AI训练镜像是否存在LLM提示注入残留、权重后门及CUDA驱动提权路径
  • 基于安卓的快递包裹隐私保护系统毕业设计源码
  • Claude Code技能精选指南:从信息过载到高效AI工作流构建
  • 多智能体系统在医疗领域的应用:架构设计与工程实践
  • 计算机毕业设计:Python金融大数据可视化与LSTM预测系统 Flask框架 深度学习 机器学习 AI 大模型(建议收藏)✅
  • RandLA-Net 点云语义分割:S3DIS 全流程实现
  • Seedance2:自动化生成osu!音游故事板,解放谱师视觉创意
  • 混合量子计算与三角连续变量门技术解析
  • 香港科大与新加坡国立大学找到了评判AI翻译SQL语句的更好方法
  • 智增增:国内用户免翻墙使用GPT-3.5/4的API中转与配置全攻略
  • cv_unet_image-colorization部署案例:Kubernetes集群中高可用服务编排
  • 基于DistilBERT的问答系统微调与部署实践
  • 3个关键优势:为什么MPC-HC仍是Windows上最纯净的媒体播放器解决方案
  • DJI Cloud API Demo终极指南:5分钟快速上手无人机云服务集成
  • 深度学习词级神经语言模型开发全流程解析
  • 作 业
  • PyTorch实现多目标多元线性回归模型教程
  • 瑞萨DA14592双核BLE芯片架构与低功耗设计解析
  • hyperf 创建型(单例、工厂、建造者、原型)
  • 用100道题拿下你的算法面试(字符串篇-8):回文子串数目