File indexing completed on 2024-05-12 04:44:32

0001 // SPDX-FileCopyrightText: Lukas Sommer <sommerluk@gmail.com>
0002 // SPDX-License-Identifier: BSD-2-Clause OR MIT
0003 
0004 // Own header
0005 #include "genericcolor.h"
0006 
0007 #include <lcms2.h>
0008 
0009 namespace PerceptualColor
0010 {
0011 
0012 /** @brief Constructor.
0013  *
0014  * @param list Initial values. Only the first 4 elements are considered.
0015  *             Excess elements are ignored. Missing elements are
0016  *             interpreted as 0. */
0017 GenericColor::GenericColor(const QList<double> &list)
0018     : first(list.value(0, 0))
0019     , second(list.value(1, 0))
0020     , third(list.value(2, 0))
0021     , fourth(list.value(3, 0))
0022 {
0023 }
0024 
0025 /** @brief Type conversion.
0026  *
0027  * @warning Interprets the current data members as Lch.
0028  *
0029  * @returns Type conversion. */
0030 LchDouble GenericColor::reinterpretAsLchToLchDouble() const
0031 {
0032     LchDouble result;
0033     result.l = first;
0034     result.c = second;
0035     result.h = third;
0036     return result;
0037 }
0038 
0039 /** @brief The values @ref first, @ref second, @ref third as @ref Trio.
0040  *
0041  * @returns The values @ref first, @ref second, @ref third as @ref Trio. */
0042 Trio GenericColor::toTrio() const
0043 {
0044     return createMatrix<1, 3, double>(first, second, third);
0045 }
0046 
0047 /** @brief The values @ref first, @ref second, @ref third as QList.
0048  *
0049  * @returns The values @ref first, @ref second, @ref third as QList. */
0050 QList<double> GenericColor::toQList3() const
0051 {
0052     return QList<double>{first, second, third};
0053 }
0054 
0055 /** @brief Type conversion.
0056  *
0057  * @warning Interprets the current data members as XZY.
0058  *
0059  * @returns Type conversion. */
0060 cmsCIEXYZ GenericColor::reinterpretAsXyzToCmsciexyz() const
0061 {
0062     return cmsCIEXYZ{first, second, third};
0063 }
0064 
0065 /** @brief Type conversion.
0066  *
0067  * @warning Interprets the current data members as Lab.
0068  *
0069  * @returns Type conversion. */
0070 cmsCIELab GenericColor::reinterpretAsLabToCmscielab() const
0071 {
0072     return cmsCIELab{first, second, third};
0073 }
0074 
0075 /** @brief Equal operator
0076  *
0077  * @param other The object to compare with.
0078  *
0079  * @returns <tt>true</tt> if equal, <tt>false</tt> otherwise. */
0080 bool GenericColor::operator==(const GenericColor &other) const
0081 {
0082     return ( //
0083         (first == other.first) //
0084         && (second == other.second) //
0085         && (third == other.third) //
0086         && (fourth == other.fourth) //
0087     );
0088 }
0089 
0090 /** @brief Unequal operator
0091  *
0092  * @param other The object to compare with.
0093  *
0094  * @returns <tt>true</tt> if unequal, <tt>false</tt> otherwise. */
0095 bool GenericColor::operator!=(const GenericColor &other) const
0096 {
0097     return !(*this == other);
0098 }
0099 
0100 /** @internal
0101  *
0102  * @brief Adds QDebug() support for data type
0103  * @ref PerceptualColor::GenericColor
0104  *
0105  * @param dbg Existing debug object
0106  * @param value Value to stream into the debug object
0107  * @returns Debug object with value streamed in */
0108 QDebug operator<<(QDebug dbg, const PerceptualColor::GenericColor &value)
0109 {
0110     dbg.nospace() //
0111         << "GenericColor(" << //
0112         value.first << ", " //
0113         << value.second << ", " //
0114         << value.third << ", " //
0115         << value.fourth << ")";
0116     return dbg.maybeSpace();
0117 }
0118 
0119 } // namespace PerceptualColor