Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-95292

Setting values on invalid/achromatic color won't work for HSL

    XMLWordPrintable

    Details

    • Commits:
      162d486c0d79ce3b2a460782319bf4c71d88398c

      Description

      The following example shows an issue with setting HSL values on a color property in QML. First the color property called testColor is set to a valid color #ff00ff. Afterwards it becomes invalid/achromatic by setting the lightness in Qt.hsla() to 0. This can be observed by printing out the HSV color values of that same color.

      The meaning of the "Qt returns a hue value of -1 for achromatic colors." is that when you have an achromatic QColor in some other color model and ask QColor to convert it to either HSL or HSV, then QColor will assign it a hue value of -1, (having no other sensible value to give it). But if you yourself set the HSV/HSL values directly, then you are assumed to know what you're doing, and QColor will just store the values as given.

      After having an invalid/achromatic color setting values like hslHue and hslSaturation won't work. Even setting the color by using Qt.hsla() won't change it. Once you have set the lightness of an HSL color to 0, you can't set the saturation. It won't trigger an update or what so ever. As long as lightness is 0 the color won't be updated.

      Example HSL:

      import QtQuick 2.15
      
      Item {
          property color testColor: "red"
      
          Component.onCompleted: () => {
              testColor = "#ff00ff"
              console.log(testColor)
              testColor = Qt.hsla(0.5, 0.3, 0, 1)
              console.log("hsl", testColor.hslHue, testColor.hslSaturation, testColor.hslLightness)
              testColor.hslSaturation = 0.6
              console.log("hsl", testColor.hslHue, testColor.hslSaturation, testColor.hslLightness)
              testColor = Qt.hsla(0.5, 0.6, 0, 1)
              console.log("hsl", testColor.hslHue, testColor.hslSaturation, testColor.hslLightness)
              testColor = Qt.hsla(0.5, 0.6, 0.2, 1)
              console.log("hsl", testColor.hslHue, testColor.hslSaturation, testColor.hslLightness)
          }
      }
      

      Output:

      qml: #ff00ff
      qml: hsl 0.5 0.30000762951094834 0
      qml: hsl 0.5 0.30000762951094834 0
      qml: hsl 0.5 0.30000762951094834 0
      qml: hsl 0.5 0.6 0.2
      

      The actual issue is that doing it with the HSV model will work without an issue.

      Example HSV:

      import QtQuick 2.15
      
      Item {
          property color testColor: "red"
      
          Component.onCompleted: () => {
              testColor = "#ff00ff"
              console.log(testColor)
              testColor = Qt.hsva(0.5, 0.3, 0, 1)
              console.log("hsv", testColor.hsvHue, testColor.hsvSaturation, testColor.hsvValue)
              testColor.hsvSaturation = 0.6
              console.log("hsv", testColor.hsvHue, testColor.hsvSaturation, testColor.hsvValue)
              testColor = Qt.hsva(0.5, 0.6, 0, 1)
              console.log("hsv", testColor.hsvHue, testColor.hsvSaturation, testColor.hsvValue)
              testColor = Qt.hsva(0.5, 0.6, 0.2, 1)
              console.log("hsv", testColor.hsvHue, testColor.hsvSaturation, testColor.hsvValue)
          }
      }
      

      Output:

      qml: #ff00ff
      qml: hsv 0.5 0.30000762951094834 0
      qml: hsv 0.5 0.6 0
      qml: hsv 0.5 0.6 0
      qml: hsv 0.5 0.6 0.2
      

        Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

            Assignee:
            fabiankosmale Fabian Kosmale
            Reporter:
            henning Henning Gr√ľndl
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:

                Gerrit Reviews

                There are no open Gerrit changes