Perceptual Color

csscolor.h
1// SPDX-FileCopyrightText: Lukas Sommer <sommerluk@gmail.com>
2// SPDX-License-Identifier: BSD-2-Clause OR MIT
3
4#ifndef CSSCOLOR_H
5#define CSSCOLOR_H
6
7#include "genericcolor.h"
8#include "helperconversion.h"
9#include <optional>
10#include <qglobal.h>
11#include <qhash.h>
12#include <qrgb.h>
13#include <qstring.h>
14
15#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
16#include <qcontainerfwd.h>
17#include <qtmetamacros.h>
18#else
19#include <qstringlist.h>
20#endif
21
22namespace PerceptualColor
23{
24
25/** @internal
26 *
27 * @brief Toolbox for CSS colors. */
28class CssColor
29{
30public:
31 /** @brief Represents the
32 * <a href="https://www.w3.org/TR/css-color-4/#typedef-predefined-rgb">
33 * predefined RGB color spaces</a> of CSS Color 4. */
34 enum class CssPredefinedRgbColorSpace {
35 Invalid, /**< Represents a non-existing color space. */
36 Srgb, /**<
37 <a href="https://www.w3.org/TR/css-color-4/#valdef-color-srgb">
38 srgb</a> */
39 SrgbLinear, /**<
40 <a href="https://www.w3.org/TR/css-color-4/#valdef-color-srgb-linear">
41 srgb-linear</a> */
42 DisplayP3, /**<
43 <a href="https://www.w3.org/TR/css-color-4/#valdef-color-display-p3">
44 display-p3</a> */
45 A98Rgb, /**<
46 <a href="https://www.w3.org/TR/css-color-4/#valdef-color-a98-rgb">
47 a98-rgb</a> */
48 ProphotoRgb, /**<
49 <a href="https://www.w3.org/TR/css-color-4/#valdef-color-prophoto-rgb">
50 prophoto-rgb</a> */
51 Rec2020 /**<
52 <a href="https://www.w3.org/TR/css-color-4/#valdef-color-rec2020">
53 rec2020</a> */
54 };
55 Q_ENUM(CssPredefinedRgbColorSpace)
56
57 /** @internal
58 *
59 * @brief Represents a CSS color in a structured fashion. */
60 struct CssColorValue {
61 public:
62 /** @brief The color model.
63 *
64 * This is @ref ColorModel::Invalid if the whole value is invalid. */
65 ColorModel model = ColorModel::Invalid;
66 /** @brief Which RGB profile is used.
67 *
68 * If @ref model is an RGB-based model, it contains
69 * <em>which</em> RGB profile is used. Otherwise, it’s
70 * @ref CssPredefinedRgbColorSpace::Invalid. */
71 CssPredefinedRgbColorSpace rgbColorSpace = CssPredefinedRgbColorSpace::Invalid;
72 /** @brief The numeric color description. */
73 GenericColor color = GenericColor();
74 /** @brief Opacity (alpha channel).
75 *
76 * Range: [0, 1] */
77 double alpha1 = 0;
78 };
79
80 [[nodiscard]] static CssColorValue parse(const QString &string);
81 [[nodiscard]] static QStringList generateCss(const QHash<ColorModel, GenericColor> &input, const double opacity1, const int significantFigures);
82
83private:
84 /** @brief Syntaxes of the CSS Color 4 color functions. */
85 enum class FunctionSyntax {
86 LegacySyntax, /**< Only
87 <a href="https://www.w3.org/TR/css-color-4/#color-syntax-legacy">
88 Legacy (comma-separated) syntax</a>. */
89 StandardSyntax, /**< Only
90 <a href="https://www.w3.org/TR/css-color-4/#color-syntax">Standard
91 (whitespace-separated) syntax</a>, optionally with a slash and an
92 alpha argument at the end. */
93 BothSyntaxes /**< Both, @ref FunctionSyntax::LegacySyntax and
94 @ref FunctionSyntax::StandardSyntax. */
95 };
96
97 [[nodiscard]] static CssColorValue parseAbsoluteColorFunction(const QString &colorFunction);
98 [[nodiscard]] static std::optional<QStringList> parseAllFunctionArguments(const QString &arguments, const FunctionSyntax mode, const int count);
99 [[nodiscard]] static std::optional<QRgb> parseHexColor(const QString &hexColor);
100 [[nodiscard]] static std::optional<QRgb> parseNamedColor(const QString &namedColor);
101 [[nodiscard]] static std::optional<double> parseArgumentHueNoneTo360(const QString &argument);
102 [[nodiscard]] static std::optional<double> parseArgumentPercentNoneTo1(const QString &argument);
103 [[nodiscard]] static std::optional<double> parseArgumentPercentNumberNone(const QString &argument, const double full, const double none);
104 [[nodiscard]] static std::optional<QStringList> validateArguments(const QStringList &arguments);
105
106 /** @internal @brief Only for unit tests. */
107 friend class TestCssColor;
108};
109
110} // namespace PerceptualColor
111
112#endif // CSSCOLOR_H
The namespace of this library.
@ Invalid
Represents invalid data.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 24 2024 11:48:43 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.