Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Best way to set text color for maximum contrast on background color?
Forum Updated to NodeBB v4.3 + New Features

Best way to set text color for maximum contrast on background color?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 3 Posters 2.5k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    patrickkidd
    wrote on last edited by
    #1

    Title says it all.

    I have the following ColorPicker type that is a ComoBox that paints the background with the current selected color and the hex of the color as the currentText property. By default the text color is black, which isn't visible when the selected color is dark enough.

    import QtQuick 2.5
    import QtQuick.Controls 2.5
    
    ComboBox {
    
        property string color: currentText
    
        id: root
        model: util.ABLETON_COLORS
        delegate: ItemDelegate {
            rightPadding: 15
            Rectangle {
                color: modelData
                width: root.implicitWidth
                height: root.implicitHeight
            }
        }
    
        Rectangle {
            x: 0
            y: 0
            anchors.fill: parent
            color: root.model[currentIndex]
        }
    
        onColorChanged: {
            var iColor = root.model.indexOf(color)
            if(root.currentIndex !== iColor) {
                root.currentIndex = iColor;
            }
        }
    }
    

    How can I make the text visible without a stroke? Some kind of graphics filter?

    Thanks!

    https://alaskafamilysystems.com/

    Gojir4G 1 Reply Last reply
    0
    • IntruderExcluderI Offline
      IntruderExcluderI Offline
      IntruderExcluder
      wrote on last edited by
      #2

      You can write a function that will detect if color dark or light, then change your color text to white, when color is dark enough. There are plenty examples in google how to get darkness of color.

      1 Reply Last reply
      2
      • P patrickkidd

        Title says it all.

        I have the following ColorPicker type that is a ComoBox that paints the background with the current selected color and the hex of the color as the currentText property. By default the text color is black, which isn't visible when the selected color is dark enough.

        import QtQuick 2.5
        import QtQuick.Controls 2.5
        
        ComboBox {
        
            property string color: currentText
        
            id: root
            model: util.ABLETON_COLORS
            delegate: ItemDelegate {
                rightPadding: 15
                Rectangle {
                    color: modelData
                    width: root.implicitWidth
                    height: root.implicitHeight
                }
            }
        
            Rectangle {
                x: 0
                y: 0
                anchors.fill: parent
                color: root.model[currentIndex]
            }
        
            onColorChanged: {
                var iColor = root.model.indexOf(color)
                if(root.currentIndex !== iColor) {
                    root.currentIndex = iColor;
                }
            }
        }
        

        How can I make the text visible without a stroke? Some kind of graphics filter?

        Thanks!

        Gojir4G Offline
        Gojir4G Offline
        Gojir4
        wrote on last edited by
        #3

        @patrickkidd Hi,

        Here is an example I have used in one project:

        /*!
            Select a color depending on whether the background is light or dark.
            \c lightColor is the color used on a light background.
            \c darkColor is the color used on a dark background.
        */
        function lightDark(background, lightColor, darkColor) {
            return isDarkColor(background) ? darkColor : lightColor
        }
        
        /*!
            Returns true if the color is dark and should have light content on top
        */
        function isDarkColor(background) {
            var temp = Qt.darker(background, 1) //Force conversion to color QML type object
            var a = 1 - ( 0.299 * temp.r + 0.587 * temp.g + 0.114 * temp.b);
            return temp.a > 0 && a >= 0.3
        }
        
        P 1 Reply Last reply
        3
        • Gojir4G Gojir4

          @patrickkidd Hi,

          Here is an example I have used in one project:

          /*!
              Select a color depending on whether the background is light or dark.
              \c lightColor is the color used on a light background.
              \c darkColor is the color used on a dark background.
          */
          function lightDark(background, lightColor, darkColor) {
              return isDarkColor(background) ? darkColor : lightColor
          }
          
          /*!
              Returns true if the color is dark and should have light content on top
          */
          function isDarkColor(background) {
              var temp = Qt.darker(background, 1) //Force conversion to color QML type object
              var a = 1 - ( 0.299 * temp.r + 0.587 * temp.g + 0.114 * temp.b);
              return temp.a > 0 && a >= 0.3
          }
          
          P Offline
          P Offline
          patrickkidd
          wrote on last edited by
          #4

          @gojir4 said in Best way to set text color for maximum contrast on background color?:

          @patrickkidd Hi,

          Here is an example I have used in one project:

          /*!
              Select a color depending on whether the background is light or dark.
              \c lightColor is the color used on a light background.
              \c darkColor is the color used on a dark background.
          */
          function lightDark(background, lightColor, darkColor) {
              return isDarkColor(background) ? darkColor : lightColor
          }
          
          /*!
              Returns true if the color is dark and should have light content on top
          */
          function isDarkColor(background) {
              var temp = Qt.darker(background, 1) //Force conversion to color QML type object
              var a = 1 - ( 0.299 * temp.r + 0.587 * temp.g + 0.114 * temp.b);
              return temp.a > 0 && a >= 0.3
          }
          

          That's a good solution. Works well here...

          https://alaskafamilysystems.com/

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved