验证颜色数据

colorreg = QRegExp("^#([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})$")
colorvalidator = QRegExpValidator()
colorvalidator.setRegExp(colorreg)

self.line_R.setValidator(QIntValidator(0,255))
self.line_G.setValidator(QIntValidator(0, 255))
self.line_B.setValidator(QIntValidator(0, 255))

def validate_hex_color(hex_color):
    # 去除前导和尾随的空格
    hex_color = hex_color.strip()

    # 检查长度
    if len(hex_color) != 7:
        return False, "颜色代码长度应为7个字符"

        # 检查格式
    if hex_color[0] != "#" or not hex_color[1].isdigit() or not hex_color[2].isdigit() or \
            not hex_color[3].isdigit() or not hex_color[4].isdigit() or \
            not hex_color[5].isdigit() or not hex_color[6].isdigit():
        return False, "颜色代码格式不正确"

        # 转换RGB值并验证
    r = int(hex_color[1:3], 16)
    g = int(hex_color[3:5], 16)
    b = int(hex_color[5:7], 16)
    if r < 0 or r > 255 or g < 0 or g > 255 or b < 0 or b > 255:
        return False, "RGB值必须在0-255范围内"

    return True, "颜色代码有效"
  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值