on xenial help sign in
Add support for luminance threshold in StyleTool.
3428442Michał Sawicz
plugins/Dash/StyleTool.qml+16−2
@@ -27,7 +27,10 @@ QtObject {
27 /// Style object passed from the scope
27 /// Style object passed from the scope
28 property var style: {}
28 property var style: {}
29
29
30− /// Calculate luminance of the passed color
30+ /*! \brief Calculate luminance of the passed color
31+
32+ \note If not fully opaque, luminance is dependant on blending.
33+ */
31 function luminance(color) {
34 function luminance(color) {
32 return 0.2126 * color.r + 0.7152 * color.g + 0.0722 * color.b;
35 return 0.2126 * color.r + 0.7152 * color.g + 0.0722 * color.b;
33 }
36 }
@@ -36,6 +39,17 @@ QtObject {
36 // FIXME: should be taken from the theme
39 // FIXME: should be taken from the theme
37 readonly property color foreground: style && "foreground-color" in style ? style["foreground-color"] : "grey"
40 readonly property color foreground: style && "foreground-color" in style ? style["foreground-color"] : "grey"
38
41
39− /// Color used for text and symbolic icons
42+ /// Color used for the overall background
40 readonly property color background: style && "background-color" in style ? style["background-color"] : "transparent"
43 readonly property color background: style && "background-color" in style ? style["background-color"] : "transparent"
44+
45+ /*! \brief Luminance threshold for switching between fore and background color
46+
47+ \note If background colour is not fully opaque, it's not taken into account.
48+ */
49+ readonly property real threshold: background.a !== 1.0 ? d.foregroundLuminance : (d.foregroundLuminance + d.backgroundLuminance) / 2
50+
51+ property var d: QtObject {
52+ readonly property real foregroundLuminance: luminance(foreground)
53+ readonly property real backgroundLuminance: luminance(background)
54+ }
41}
55}
tests/plugins/Dash/tst_StyleTool.qml+13
@@ -92,5 +92,18 @@ Rectangle {
92 "Background color not equal: %1 != %2".arg(tool.background).arg(data.background));
92 "Background color not equal: %1 != %2".arg(tool.background).arg(data.background));
93 }
93 }
94
94
95+ function test_threshold_data() {
96+ return [
97+ { tag: "default", index: 0, threshold: 0.5020 },
98+ { tag: "red on black", index: 1, threshold: 0.1063 },
99+ { tag: "green on white", index: 2, threshold: 0.6795 },
100+ { tag: "blue on darkgrey", index: 3, threshold: 0.3675 },
101+ ];
102+ }
103+
104+ function test_threshold(data) {
105+ tool.style = testCase.styles[data.index];
106+ compare(tool.threshold.toFixed(4), data.threshold.toFixed(4), "Luminance threshold was incorrect.")
107+ }
95 }
108 }
96}
109}