Browse Source

Add CIE xyY and CIE 1960 UCS colorspace functions

master
C. J. Howard 3 years ago
parent
commit
159aeb4906
3 changed files with 167 additions and 0 deletions
  1. +56
    -0
      src/color/ucs.hpp
  2. +89
    -0
      src/color/xyy.hpp
  3. +22
    -0
      src/color/xyz.hpp

+ 56
- 0
src/color/ucs.hpp View File

@ -0,0 +1,56 @@
/*
* Copyright (C) 2021 Christopher J. Howard
*
* This file is part of Antkeeper source code.
*
* Antkeeper source code is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Antkeeper source code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ANTKEEPER_COLOR_UCS_HPP
#define ANTKEEPER_COLOR_UCS_HPP
#include "math/math.hpp"
namespace color {
/// Functions which operate in the CIE 1960 UCS colorspace.
namespace ucs {
/**
* Transforms a CIE 1960 UCS color into the CIE xyY colorspace.
*
* @param uv CIE 1960 UCS color coordinates.
* @param luminance Luminance or `Y` value of the resulting xyY color.
* @return CIE xyY color.
*/
template <class T>
math::vector3<T> to_xyy(const math::vector2<T>& uv, T luminance);
template <class T>
math::vector3<T> to_xyy(const math::vector2<T>& uv, T luminance);
{
const T inverse_denom = 1.0 / (2.0 * uv[0] - 8.0 * uv[1] + 4.0);
return math::vector3<T>
{
(3.0 * uv[0]) * inverse_denom,
(2.0 * uv[1]) * inverse_denom,
luminance
};
}
} // namespace ucs
} // namespace color
#endif // ANTKEEPER_COLOR_UCS_HPP

+ 89
- 0
src/color/xyy.hpp View File

@ -0,0 +1,89 @@
/*
* Copyright (C) 2021 Christopher J. Howard
*
* This file is part of Antkeeper source code.
*
* Antkeeper source code is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Antkeeper source code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ANTKEEPER_COLOR_XYY_HPP
#define ANTKEEPER_COLOR_XYY_HPP
#include "math/math.hpp"
namespace color {
/// Functions which operate in the CIE xyY colorspace.
namespace xyy {
/**
* Returns the luminance of a CIE xyY color.
*
* @param x CIE xyY color.
* @return return Luminance of @p x.
*/
template <class T>
T luminance(const math::vector3<T>& x);
/**
* Transforms a CIE xyY color into the CIE 1960 UCS colorspace.
*
* @param x CIE xyY color.
* @return CIE 1960 UCS color.
*/
template <class T>
math::vector2<T> to_ucs(const math::vector3<T>& x);
/**
* Transforms a CIE xyY color into the CIE XYZ colorspace.
*
* @param x CIE xyY color.
* @return CIE XYZ color.
*/
template <class T>
math::vector3<T> to_xyz(const math::vector3<T>& x);
template <class T>
inline T luminance(const math::vector3<T>& x)
{
return x[2];
}
template <class T>
math::vector2<T> to_ucs(const math::vector3<T>& x)
{
const T inverse_denom = 1.0 / (-2.0 * x[0] + 12.0 * x[1] + 3.0);
return math::vector2<T>
{
(4.0 * x[0]) * inverse_denom,
(6.0 * x[1]) * inverse_denom
};
}
template <class T>
math::vector3<T> to_xyz(const math::vector3<T>& x)
{
return math::vector3<T>
{
(x[0] * x[2]) / x[1],
x[2],
((1.0 - x[0] - x[1]) * x[2]) / x[1]
};
}
} // namespace xyy
} // namespace color
#endif // ANTKEEPER_COLOR_XYY_HPP

+ 22
- 0
src/color/xyz.hpp View File

@ -54,6 +54,15 @@ math::vector3 to_acescg(const math::vector3& x);
template <class T> template <class T>
math::vector3<T> to_srgb(const math::vector3<T>& x); math::vector3<T> to_srgb(const math::vector3<T>& x);
/**
* Transforms a CIE XYZ color into the CIE xyY colorspace.
*
* @param x CIE XYZ color.
* @return CIE xyY color.
*/
template <class T>
math::vector3<T> to_xyy(const math::vector3<T>& x);
template <class T> template <class T>
inline T luminance(const math::vector3<T>& x) inline T luminance(const math::vector3<T>& x)
{ {
@ -86,6 +95,19 @@ math::vector3 to_srgb(const math::vector3& x)
return xyz_to_srgb * x; return xyz_to_srgb * x;
} }
template <class T>
math::vector3<T> to_xyy(const math::vector3<T>& x)
{
const T sum = x[0] + x[1] + x[2];
return math::vector3<T>
{
x[0] / sum
x[1] / sum,
x[1]
};
}
} // namespace xyz } // namespace xyz
} // namespace color } // namespace color

Loading…
Cancel
Save