summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Dirk Hohndel <dirk@hohndel.org>2021-01-01 14:55:23 +0100
committerGravatar bstoeger <32835590+bstoeger@users.noreply.github.com>2021-01-01 21:10:10 +0100
commit7339dc28ae709f86e847ba5a6acb3e96f4ad0882 (patch)
treec2fc2c652cf09e43027437fe5b780702e3bf6456
parent93ba2219ee06abe27573deeb4cfdf39f60d41087 (diff)
downloadsubsurface-7339dc28ae709f86e847ba5a6acb3e96f4ad0882.tar.gz
statistics: add color-related functions
Add a source file and a header file, which implement the color scheme used by the statistics module. Besides a few color constants, the centerpiece is a function that returns the color representing a bin and an appropriate label color. It picks a roughly equi-distant set of colors out of an already balanced set of 50 candidate colors. And it also picks white as text color when adding a label to a segment with a dark color. The color list was created using a tool by Gregor Aisch that is available on GitHub as https://github.com/gka/palettes to create multi-hued, multi-stop color scales that are safe for color blind people. This commit contains code from three authors. Dirk (main author): adaptive color scheme. Willem: Colors of single-bin charts and lines. Berthold: Infrastructure. Signed-off-by: Dirk Hohndel <dirk@hohndel.org> Signed-off-by: willemferguson <willemferguson@zoology.up.ac.za> Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
-rw-r--r--stats/CMakeLists.txt2
-rw-r--r--stats/statscolors.cpp36
-rw-r--r--stats/statscolors.h18
3 files changed, 56 insertions, 0 deletions
diff --git a/stats/CMakeLists.txt b/stats/CMakeLists.txt
index 1eba8377f..e4c110271 100644
--- a/stats/CMakeLists.txt
+++ b/stats/CMakeLists.txt
@@ -5,6 +5,8 @@ include_directories(.
)
set(SUBSURFACE_STATS_SRCS
+ statscolors.h
+ statscolors.cpp
statsvariables.h
statsvariables.cpp
)
diff --git a/stats/statscolors.cpp b/stats/statscolors.cpp
new file mode 100644
index 000000000..7930b2a5f
--- /dev/null
+++ b/stats/statscolors.cpp
@@ -0,0 +1,36 @@
+#include "statscolors.h"
+
+// Colors created using the Chroma.js Color Palette Helper
+// https://vis4.net/palettes/#/50|d|00108c,3ed8ff,ffffe0|ffffe0,ff005e,743535|1|1
+static const QColor binColors[] = {
+ QRgb(0x00108c), QRgb(0x0f1c92), QRgb(0x1a2798), QRgb(0x23319d), QRgb(0x2a3ba3),
+ QRgb(0x3144a8), QRgb(0x374eae), QRgb(0x3e58b3), QRgb(0x4461b8), QRgb(0x4b6bbd),
+ QRgb(0x5274c2), QRgb(0x587ec7), QRgb(0x6088cc), QRgb(0x6791d0), QRgb(0x6f9bd4),
+ QRgb(0x78a5d8), QRgb(0x81aedb), QRgb(0x8ab8df), QRgb(0x95c2e2), QRgb(0xa0cbe4),
+ QRgb(0xacd5e6), QRgb(0xb9dee7), QRgb(0xc7e7e7), QRgb(0xd7efe7), QRgb(0xeaf8e4),
+ QRgb(0xfff5d8), QRgb(0xffead0), QRgb(0xffe0c8), QRgb(0xffd5c0), QRgb(0xffcab8),
+ QRgb(0xffbfb0), QRgb(0xffb4a8), QRgb(0xffa99f), QRgb(0xfc9e98), QRgb(0xf99490),
+ QRgb(0xf48b89), QRgb(0xf08182), QRgb(0xea787b), QRgb(0xe46f74), QRgb(0xde666e),
+ QRgb(0xd75e67), QRgb(0xcf5661), QRgb(0xc64f5b), QRgb(0xbd4855), QRgb(0xb3434f),
+ QRgb(0xa83e49), QRgb(0x9d3a44), QRgb(0x90383f), QRgb(0x83363a), QRgb(0x743535)
+};
+
+// Pick roughly equidistant colors out of the color set above
+// if we need more bins than we have colors (what chart is THAT?) simply loop
+QColor binColor(int bin, int numBins)
+{
+ if (numBins == 1 || bin < 0 || bin >= numBins)
+ return fillColor;
+ if (numBins > (int)std::size(binColors))
+ return binColors[bin % std::size(binColors)];
+
+ // use integer math to spread out the indices
+ int idx = bin * (std::size(binColors) - 1) / (numBins - 1);
+ return binColors[idx];
+}
+
+// Figure out if we want a light or a dark label
+QColor labelColor(int bin, size_t numBins)
+{
+ return (binColor(bin, numBins).lightness() < 150) ? lightLabelColor : darkLabelColor;
+}
diff --git a/stats/statscolors.h b/stats/statscolors.h
new file mode 100644
index 000000000..92ed53377
--- /dev/null
+++ b/stats/statscolors.h
@@ -0,0 +1,18 @@
+// SPDX-License-Identifier: GPL-2.0
+// Color constants for the various series
+#ifndef STATSCOLORS_H
+#define STATSCOLORS_H
+
+#include <QColor>
+
+inline const QColor fillColor(0x44, 0x76, 0xaa);
+inline const QColor borderColor(0x66, 0xb2, 0xff);
+inline const QColor highlightedColor(Qt::yellow);
+inline const QColor highlightedBorderColor(0xaa, 0xaa, 0x22);
+inline const QColor darkLabelColor(Qt::black);
+inline const QColor lightLabelColor(Qt::white);
+
+QColor binColor(int bin, int numBins);
+QColor labelColor(int bin, size_t numBins);
+
+#endif