on xenial help sign in
Add support for luminance threshold in StyleTool.
commit3428442286aad593d81871b9d16e64b464cb87e1 authorMichał Sawicz <michal.sawicz@canonical.com> dateSun 6 Jul 2014, 03:06 (12 years ago) parent46ecd6b
2 files changed+29−2
plugins/Dash/StyleTool.qml+16−2 tests/plugins/Dash/tst_StyleTool.qml+13
plugins/Dash/StyleTool.qml+16−2
@@ -27,7 +27,10 @@ QtObject {
2727 /// Style object passed from the scope
2828 property var style: {}
2929
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+ */
3134 function luminance(color) {
3235 return 0.2126 * color.r + 0.7152 * color.g + 0.0722 * color.b;
3336 }
@@ -36,6 +39,17 @@ QtObject {
3639 // FIXME: should be taken from the theme
3740 readonly property color foreground: style && "foreground-color" in style ? style["foreground-color"] : "grey"
3841
39− /// Color used for text and symbolic icons
42+ /// Color used for the overall background
4043 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+ }
4155}
tests/plugins/Dash/tst_StyleTool.qml+13
@@ -92,5 +92,18 @@ Rectangle {
9292 "Background color not equal: %1 != %2".arg(tool.background).arg(data.background));
9393 }
9494
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+ }
95108 }
96109}