Add material library

This commit is contained in:
IXtreme 2025-01-06 23:07:33 -05:00
commit 2869533db9
73 changed files with 8571 additions and 1 deletions

View file

@ -18,7 +18,7 @@ LIB_DIR = lib
STATIC_INCLUDE_DIR = include
CPPFLAGS = -Isrc/imgui -g -D ASSET_DIR=\"$(ASSET_PREFIX)\"
CPPFLAGS = -Isrc/imgui -Isrc/material-colors -g -D ASSET_DIR=\"$(ASSET_PREFIX)\"
CFLAGS = -g -D ASSET_DIR=\"$(ASSET_PREFIX)\"
LDFLAGS = -g
#global LDLIBS
@ -51,6 +51,16 @@ C_SRC += $(wildcard $(SRC_DIR)/*.c) #GET LIST OF ALL C FILES
CPP_SRC += $(wildcard $(SRC_DIR)/*.cpp) #GET LIST OF ALL CPP FILES
CPP_SRC += $(wildcard $(SRC_DIR)/rlImGui/*.cpp) #GET LIST OF ALL CPP FILES
# BUILD MATERIAL_COLORS SYSETM
CPP_SRC += $(wildcard $(SRC_DIR)/material-colors/cpp/quantize/celebi.cpp)
CPP_SRC += $(wildcard $(SRC_DIR)/material-colors/cpp/quantize/wsmeans.cpp)
CPP_SRC += $(wildcard $(SRC_DIR)/material-colors/cpp/quantize/wu.cpp)
CPP_SRC += $(wildcard $(SRC_DIR)/material-colors/cpp/quantize/lab.cpp)
CPP_SRC += $(wildcard $(SRC_DIR)/material-colors/cpp/utils/utils.cpp)
EXTRA_DIRS += $(OBJ_DIR)/material-colors/cpp/quantize
EXTRA_DIRS += $(OBJ_DIR)/material-colors/cpp/utils
ifeq ($(IMGUI_MODE), BUILD)
CPP_SRC += $(wildcard $(SRC_DIR)/imgui/*.cpp) #GET LIST OF ALL CPP FILES
EXTRA_DIRS += $(OBJ_DIR)/imgui

View file

@ -0,0 +1,71 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "cpp/blend/blend.h"
#include <algorithm>
#include <cstdint>
#include "cpp/cam/cam.h"
#include "cpp/cam/hct.h"
#include "cpp/cam/viewing_conditions.h"
#include "cpp/utils/utils.h"
namespace material_color_utilities {
Argb BlendHarmonize(const Argb design_color, const Argb key_color) {
Hct from_hct(design_color);
Hct to_hct(key_color);
double difference_degrees = DiffDegrees(from_hct.get_hue(), to_hct.get_hue());
double rotation_degrees = std::min(difference_degrees * 0.5, 15.0);
double output_hue = SanitizeDegreesDouble(
from_hct.get_hue() +
rotation_degrees *
RotationDirection(from_hct.get_hue(), to_hct.get_hue()));
from_hct.set_hue(output_hue);
return from_hct.ToInt();
}
Argb BlendHctHue(const Argb from, const Argb to, const double amount) {
int ucs = BlendCam16Ucs(from, to, amount);
Hct ucs_hct(ucs);
Hct from_hct(from);
from_hct.set_hue(ucs_hct.get_hue());
return from_hct.ToInt();
}
Argb BlendCam16Ucs(const Argb from, const Argb to, const double amount) {
Cam from_cam = CamFromInt(from);
Cam to_cam = CamFromInt(to);
const double a_j = from_cam.jstar;
const double a_a = from_cam.astar;
const double a_b = from_cam.bstar;
const double b_j = to_cam.jstar;
const double b_a = to_cam.astar;
const double b_b = to_cam.bstar;
const double jstar = a_j + (b_j - a_j) * amount;
const double astar = a_a + (b_a - a_a) * amount;
const double bstar = a_b + (b_b - a_b) * amount;
const Cam blended = CamFromUcsAndViewingConditions(jstar, astar, bstar,
kDefaultViewingConditions);
return IntFromCam(blended);
}
} // namespace material_color_utilities

View file

@ -0,0 +1,31 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef CPP_BLEND_BLEND_H_
#define CPP_BLEND_BLEND_H_
#include <cstdint>
#include "cpp/utils/utils.h"
namespace material_color_utilities {
Argb BlendHarmonize(const Argb design_color, const Argb key_color);
Argb BlendHctHue(const Argb from, const Argb to, const double amount);
Argb BlendCam16Ucs(const Argb from, const Argb to, const double amount);
} // namespace material_color_utilities
#endif // CPP_BLEND_BLEND_H_

View file

@ -0,0 +1,31 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "cpp/blend/blend.h"
#include "testing/base/public/gunit.h"
#include "cpp/utils/utils.h"
namespace material_color_utilities {
namespace {
TEST(BlendTest, RedToBlue) {
int blended = BlendHctHue(0xffff0000, 0xff0000ff, 0.8);
EXPECT_EQ(HexFromArgb(blended), "ff905eff");
}
} // namespace
} // namespace material_color_utilities

View file

@ -0,0 +1,263 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "cpp/cam/cam.h"
#include <assert.h>
#include <math.h>
#include "cpp/cam/hct_solver.h"
#include "cpp/cam/viewing_conditions.h"
#include "cpp/utils/utils.h"
namespace material_color_utilities {
Cam CamFromJchAndViewingConditions(double j, double c, double h,
ViewingConditions viewing_conditions);
Cam CamFromUcsAndViewingConditions(
double jstar, double astar, double bstar,
const ViewingConditions &viewing_conditions) {
const double a = astar;
const double b = bstar;
const double m = sqrt(a * a + b * b);
const double m_2 = (exp(m * 0.0228) - 1.0) / 0.0228;
const double c = m_2 / viewing_conditions.fl_root;
double h = atan2(b, a) * (180.0 / kPi);
if (h < 0.0) {
h += 360.0;
}
const double j = jstar / (1 - (jstar - 100) * 0.007);
return CamFromJchAndViewingConditions(j, c, h, viewing_conditions);
}
Cam CamFromIntAndViewingConditions(
Argb argb, const ViewingConditions &viewing_conditions) {
// XYZ from ARGB, inlined.
int red = (argb & 0x00ff0000) >> 16;
int green = (argb & 0x0000ff00) >> 8;
int blue = (argb & 0x000000ff);
double red_l = Linearized(red);
double green_l = Linearized(green);
double blue_l = Linearized(blue);
double x = 0.41233895 * red_l + 0.35762064 * green_l + 0.18051042 * blue_l;
double y = 0.2126 * red_l + 0.7152 * green_l + 0.0722 * blue_l;
double z = 0.01932141 * red_l + 0.11916382 * green_l + 0.95034478 * blue_l;
// Convert XYZ to 'cone'/'rgb' responses
double r_c = 0.401288 * x + 0.650173 * y - 0.051461 * z;
double g_c = -0.250268 * x + 1.204414 * y + 0.045854 * z;
double b_c = -0.002079 * x + 0.048952 * y + 0.953127 * z;
// Discount illuminant.
double r_d = viewing_conditions.rgb_d[0] * r_c;
double g_d = viewing_conditions.rgb_d[1] * g_c;
double b_d = viewing_conditions.rgb_d[2] * b_c;
// Chromatic adaptation.
double r_af = pow(viewing_conditions.fl * fabs(r_d) / 100.0, 0.42);
double g_af = pow(viewing_conditions.fl * fabs(g_d) / 100.0, 0.42);
double b_af = pow(viewing_conditions.fl * fabs(b_d) / 100.0, 0.42);
double r_a = Signum(r_d) * 400.0 * r_af / (r_af + 27.13);
double g_a = Signum(g_d) * 400.0 * g_af / (g_af + 27.13);
double b_a = Signum(b_d) * 400.0 * b_af / (b_af + 27.13);
// Redness-greenness
double a = (11.0 * r_a + -12.0 * g_a + b_a) / 11.0;
double b = (r_a + g_a - 2.0 * b_a) / 9.0;
double u = (20.0 * r_a + 20.0 * g_a + 21.0 * b_a) / 20.0;
double p2 = (40.0 * r_a + 20.0 * g_a + b_a) / 20.0;
double radians = atan2(b, a);
double degrees = radians * 180.0 / kPi;
double hue = SanitizeDegreesDouble(degrees);
double hue_radians = hue * kPi / 180.0;
double ac = p2 * viewing_conditions.nbb;
double j = 100.0 * pow(ac / viewing_conditions.aw,
viewing_conditions.c * viewing_conditions.z);
double q = (4.0 / viewing_conditions.c) * sqrt(j / 100.0) *
(viewing_conditions.aw + 4.0) * viewing_conditions.fl_root;
double hue_prime = hue < 20.14 ? hue + 360 : hue;
double e_hue = 0.25 * (cos(hue_prime * kPi / 180.0 + 2.0) + 3.8);
double p1 =
50000.0 / 13.0 * e_hue * viewing_conditions.n_c * viewing_conditions.ncb;
double t = p1 * sqrt(a * a + b * b) / (u + 0.305);
double alpha =
pow(t, 0.9) *
pow(1.64 - pow(0.29, viewing_conditions.background_y_to_white_point_y),
0.73);
double c = alpha * sqrt(j / 100.0);
double m = c * viewing_conditions.fl_root;
double s = 50.0 * sqrt((alpha * viewing_conditions.c) /
(viewing_conditions.aw + 4.0));
double jstar = (1.0 + 100.0 * 0.007) * j / (1.0 + 0.007 * j);
double mstar = 1.0 / 0.0228 * log(1.0 + 0.0228 * m);
double astar = mstar * cos(hue_radians);
double bstar = mstar * sin(hue_radians);
return {hue, c, j, q, m, s, jstar, astar, bstar};
}
Cam CamFromInt(Argb argb) {
return CamFromIntAndViewingConditions(argb, kDefaultViewingConditions);
}
Argb IntFromCamAndViewingConditions(Cam cam,
ViewingConditions viewing_conditions) {
double alpha = (cam.chroma == 0.0 || cam.j == 0.0)
? 0.0
: cam.chroma / sqrt(cam.j / 100.0);
double t = pow(
alpha / pow(1.64 - pow(0.29,
viewing_conditions.background_y_to_white_point_y),
0.73),
1.0 / 0.9);
double h_rad = cam.hue * kPi / 180.0;
double e_hue = 0.25 * (cos(h_rad + 2.0) + 3.8);
double ac =
viewing_conditions.aw *
pow(cam.j / 100.0, 1.0 / viewing_conditions.c / viewing_conditions.z);
double p1 = e_hue * (50000.0 / 13.0) * viewing_conditions.n_c *
viewing_conditions.ncb;
double p2 = ac / viewing_conditions.nbb;
double h_sin = sin(h_rad);
double h_cos = cos(h_rad);
double gamma = 23.0 * (p2 + 0.305) * t /
(23.0 * p1 + 11.0 * t * h_cos + 108.0 * t * h_sin);
double a = gamma * h_cos;
double b = gamma * h_sin;
double r_a = (460.0 * p2 + 451.0 * a + 288.0 * b) / 1403.0;
double g_a = (460.0 * p2 - 891.0 * a - 261.0 * b) / 1403.0;
double b_a = (460.0 * p2 - 220.0 * a - 6300.0 * b) / 1403.0;
double r_c_base = fmax(0, (27.13 * fabs(r_a)) / (400.0 - fabs(r_a)));
double r_c =
Signum(r_a) * (100.0 / viewing_conditions.fl) * pow(r_c_base, 1.0 / 0.42);
double g_c_base = fmax(0, (27.13 * fabs(g_a)) / (400.0 - fabs(g_a)));
double g_c =
Signum(g_a) * (100.0 / viewing_conditions.fl) * pow(g_c_base, 1.0 / 0.42);
double b_c_base = fmax(0, (27.13 * fabs(b_a)) / (400.0 - fabs(b_a)));
double b_c =
Signum(b_a) * (100.0 / viewing_conditions.fl) * pow(b_c_base, 1.0 / 0.42);
double r_x = r_c / viewing_conditions.rgb_d[0];
double g_x = g_c / viewing_conditions.rgb_d[1];
double b_x = b_c / viewing_conditions.rgb_d[2];
double x = 1.86206786 * r_x - 1.01125463 * g_x + 0.14918677 * b_x;
double y = 0.38752654 * r_x + 0.62144744 * g_x - 0.00897398 * b_x;
double z = -0.01584150 * r_x - 0.03412294 * g_x + 1.04996444 * b_x;
// intFromXyz
double r_l = 3.2406 * x - 1.5372 * y - 0.4986 * z;
double g_l = -0.9689 * x + 1.8758 * y + 0.0415 * z;
double b_l = 0.0557 * x - 0.2040 * y + 1.0570 * z;
int red = Delinearized(r_l);
int green = Delinearized(g_l);
int blue = Delinearized(b_l);
return ArgbFromRgb(red, green, blue);
}
Argb IntFromCam(Cam cam) {
return IntFromCamAndViewingConditions(cam, kDefaultViewingConditions);
}
Cam CamFromJchAndViewingConditions(double j, double c, double h,
ViewingConditions viewing_conditions) {
double q = (4.0 / viewing_conditions.c) * sqrt(j / 100.0) *
(viewing_conditions.aw + 4.0) * (viewing_conditions.fl_root);
double m = c * viewing_conditions.fl_root;
double alpha = c / sqrt(j / 100.0);
double s = 50.0 * sqrt((alpha * viewing_conditions.c) /
(viewing_conditions.aw + 4.0));
double hue_radians = h * kPi / 180.0;
double jstar = (1.0 + 100.0 * 0.007) * j / (1.0 + 0.007 * j);
double mstar = 1.0 / 0.0228 * log(1.0 + 0.0228 * m);
double astar = mstar * cos(hue_radians);
double bstar = mstar * sin(hue_radians);
return {h, c, j, q, m, s, jstar, astar, bstar};
}
double CamDistance(Cam a, Cam b) {
double d_j = a.jstar - b.jstar;
double d_a = a.astar - b.astar;
double d_b = a.bstar - b.bstar;
double d_e_prime = sqrt(d_j * d_j + d_a * d_a + d_b * d_b);
double d_e = 1.41 * pow(d_e_prime, 0.63);
return d_e;
}
Argb IntFromHcl(double hue, double chroma, double lstar) {
return SolveToInt(hue, chroma, lstar);
}
Cam CamFromXyzAndViewingConditions(
double x, double y, double z, const ViewingConditions &viewing_conditions) {
// Convert XYZ to 'cone'/'rgb' responses
double r_c = 0.401288 * x + 0.650173 * y - 0.051461 * z;
double g_c = -0.250268 * x + 1.204414 * y + 0.045854 * z;
double b_c = -0.002079 * x + 0.048952 * y + 0.953127 * z;
// Discount illuminant.
double r_d = viewing_conditions.rgb_d[0] * r_c;
double g_d = viewing_conditions.rgb_d[1] * g_c;
double b_d = viewing_conditions.rgb_d[2] * b_c;
// Chromatic adaptation.
double r_af = pow(viewing_conditions.fl * fabs(r_d) / 100.0, 0.42);
double g_af = pow(viewing_conditions.fl * fabs(g_d) / 100.0, 0.42);
double b_af = pow(viewing_conditions.fl * fabs(b_d) / 100.0, 0.42);
double r_a = Signum(r_d) * 400.0 * r_af / (r_af + 27.13);
double g_a = Signum(g_d) * 400.0 * g_af / (g_af + 27.13);
double b_a = Signum(b_d) * 400.0 * b_af / (b_af + 27.13);
// Redness-greenness
double a = (11.0 * r_a + -12.0 * g_a + b_a) / 11.0;
double b = (r_a + g_a - 2.0 * b_a) / 9.0;
double u = (20.0 * r_a + 20.0 * g_a + 21.0 * b_a) / 20.0;
double p2 = (40.0 * r_a + 20.0 * g_a + b_a) / 20.0;
double radians = atan2(b, a);
double degrees = radians * 180.0 / kPi;
double hue = SanitizeDegreesDouble(degrees);
double hue_radians = hue * kPi / 180.0;
double ac = p2 * viewing_conditions.nbb;
double j = 100.0 * pow(ac / viewing_conditions.aw,
viewing_conditions.c * viewing_conditions.z);
double q = (4.0 / viewing_conditions.c) * sqrt(j / 100.0) *
(viewing_conditions.aw + 4.0) * viewing_conditions.fl_root;
double hue_prime = hue < 20.14 ? hue + 360 : hue;
double e_hue = 0.25 * (cos(hue_prime * kPi / 180.0 + 2.0) + 3.8);
double p1 =
50000.0 / 13.0 * e_hue * viewing_conditions.n_c * viewing_conditions.ncb;
double t = p1 * sqrt(a * a + b * b) / (u + 0.305);
double alpha =
pow(t, 0.9) *
pow(1.64 - pow(0.29, viewing_conditions.background_y_to_white_point_y),
0.73);
double c = alpha * sqrt(j / 100.0);
double m = c * viewing_conditions.fl_root;
double s = 50.0 * sqrt((alpha * viewing_conditions.c) /
(viewing_conditions.aw + 4.0));
double jstar = (1.0 + 100.0 * 0.007) * j / (1.0 + 0.007 * j);
double mstar = 1.0 / 0.0228 * log(1.0 + 0.0228 * m);
double astar = mstar * cos(hue_radians);
double bstar = mstar * sin(hue_radians);
return {hue, c, j, q, m, s, jstar, astar, bstar};
}
} // namespace material_color_utilities

View file

@ -0,0 +1,53 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef CPP_CAM_CAM_H_
#define CPP_CAM_CAM_H_
#include "cpp/cam/viewing_conditions.h"
#include "cpp/utils/utils.h"
namespace material_color_utilities {
struct Cam {
double hue = 0.0;
double chroma = 0.0;
double j = 0.0;
double q = 0.0;
double m = 0.0;
double s = 0.0;
double jstar = 0.0;
double astar = 0.0;
double bstar = 0.0;
};
Cam CamFromInt(Argb argb);
Cam CamFromIntAndViewingConditions(Argb argb,
const ViewingConditions &viewing_conditions);
Argb IntFromHcl(double hue, double chroma, double lstar);
Argb IntFromCam(Cam cam);
Cam CamFromUcsAndViewingConditions(double jstar, double astar, double bstar,
const ViewingConditions &viewing_conditions);
/**
* Given color expressed in the XYZ color space and viewed
* in [viewingConditions], converts the color to CAM16.
*/
Cam CamFromXyzAndViewingConditions(double x, double y, double z,
const ViewingConditions &viewing_conditions);
} // namespace material_color_utilities
#endif // CPP_CAM_CAM_H_

View file

@ -0,0 +1,109 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "cpp/cam/cam.h"
#include "testing/base/public/gmock.h"
#include "testing/base/public/gunit.h"
namespace material_color_utilities {
namespace {
using testing::DoubleNear;
using testing::Eq;
Argb RED = 0xffff0000;
Argb GREEN = 0xff00ff00;
Argb BLUE = 0xff0000ff;
Argb WHITE = 0xffffffff;
Argb BLACK = 0xff000000;
TEST(CamTest, Red) {
Cam cam = CamFromInt(RED);
EXPECT_THAT(cam.hue, DoubleNear(27.408, 0.001));
EXPECT_THAT(cam.chroma, DoubleNear(113.357, 0.001));
EXPECT_THAT(cam.j, DoubleNear(46.445, 0.001));
EXPECT_THAT(cam.m, DoubleNear(89.494, 0.001));
EXPECT_THAT(cam.s, DoubleNear(91.889, 0.001));
EXPECT_THAT(cam.q, DoubleNear(105.988, 0.001));
}
TEST(CamTest, Green) {
Cam cam = CamFromInt(GREEN);
EXPECT_THAT(cam.hue, DoubleNear(142.139, 0.001));
EXPECT_THAT(cam.chroma, DoubleNear(108.410, 0.001));
EXPECT_THAT(cam.j, DoubleNear(79.331, 0.001));
EXPECT_THAT(cam.m, DoubleNear(85.587, 0.001));
EXPECT_THAT(cam.s, DoubleNear(78.604, 0.001));
EXPECT_THAT(cam.q, DoubleNear(138.520, 0.001));
}
TEST(CamTest, Blue) {
Cam cam = CamFromInt(BLUE);
EXPECT_THAT(cam.hue, DoubleNear(282.788, 0.001));
EXPECT_THAT(cam.chroma, DoubleNear(87.230, 0.001));
EXPECT_THAT(cam.j, DoubleNear(25.465, 0.001));
EXPECT_THAT(cam.m, DoubleNear(68.867, 0.001));
EXPECT_THAT(cam.s, DoubleNear(93.674, 0.001));
EXPECT_THAT(cam.q, DoubleNear(78.481, 0.001));
}
TEST(CamTest, White) {
Cam cam = CamFromInt(WHITE);
EXPECT_THAT(cam.hue, DoubleNear(209.492, 0.001));
EXPECT_THAT(cam.chroma, DoubleNear(2.869, 0.001));
EXPECT_THAT(cam.j, DoubleNear(100.0, 0.001));
EXPECT_THAT(cam.m, DoubleNear(2.265, 0.001));
EXPECT_THAT(cam.s, DoubleNear(12.068, 0.001));
EXPECT_THAT(cam.q, DoubleNear(155.521, 0.001));
}
TEST(CamTest, Black) {
Cam cam = CamFromInt(BLACK);
EXPECT_THAT(cam.hue, DoubleNear(0.0, 0.001));
EXPECT_THAT(cam.chroma, DoubleNear(0.0, 0.001));
EXPECT_THAT(cam.j, DoubleNear(0.0, 0.001));
EXPECT_THAT(cam.m, DoubleNear(0.0, 0.001));
EXPECT_THAT(cam.s, DoubleNear(0.0, 0.001));
EXPECT_THAT(cam.q, DoubleNear(0.0, 0.001));
}
TEST(CamTest, RedRoundTrip) {
Cam cam = CamFromInt(RED);
Argb argb = IntFromCam(cam);
EXPECT_THAT(argb, Eq(RED));
}
TEST(CamTest, GreenRoundTrip) {
Cam cam = CamFromInt(GREEN);
Argb argb = IntFromCam(cam);
EXPECT_THAT(argb, Eq(GREEN));
}
TEST(CamTest, BlueRoundTrip) {
Cam cam = CamFromInt(BLUE);
Argb argb = IntFromCam(cam);
EXPECT_THAT(argb, Eq(BLUE));
}
} // namespace
} // namespace material_color_utilities

View file

@ -0,0 +1,57 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "cpp/cam/hct.h"
#include "cpp/cam/hct_solver.h"
#include "cpp/utils/utils.h"
namespace material_color_utilities {
Hct::Hct(double hue, double chroma, double tone) {
SetInternalState(SolveToInt(hue, chroma, tone));
}
Hct::Hct(Argb argb) { SetInternalState(argb); }
double Hct::get_hue() const { return hue_; }
double Hct::get_chroma() const { return chroma_; }
double Hct::get_tone() const { return tone_; }
Argb Hct::ToInt() const { return argb_; }
void Hct::set_hue(double new_hue) {
SetInternalState(SolveToInt(new_hue, chroma_, tone_));
}
void Hct::set_chroma(double new_chroma) {
SetInternalState(SolveToInt(hue_, new_chroma, tone_));
}
void Hct::set_tone(double new_tone) {
SetInternalState(SolveToInt(hue_, chroma_, new_tone));
}
void Hct::SetInternalState(Argb argb) {
argb_ = argb;
Cam cam = CamFromInt(argb);
hue_ = cam.hue;
chroma_ = cam.chroma;
tone_ = LstarFromArgb(argb);
}
} // namespace material_color_utilities

View file

@ -0,0 +1,137 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef CPP_CAM_HCT_H_
#define CPP_CAM_HCT_H_
#include "cpp/utils/utils.h"
namespace material_color_utilities {
/**
* HCT: hue, chroma, and tone.
*
* A color system built using CAM16 hue and chroma, and L* (lightness) from
* the L*a*b* color space, providing a perceptually accurate
* color measurement system that can also accurately render what colors
* will appear as in different lighting environments.
*
* Using L* creates a link between the color system, contrast, and thus
* accessibility. Contrast ratio depends on relative luminance, or Y in the XYZ
* color space. L*, or perceptual luminance can be calculated from Y.
*
* Unlike Y, L* is linear to human perception, allowing trivial creation of
* accurate color tones.
*
* Unlike contrast ratio, measuring contrast in L* is linear, and simple to
* calculate. A difference of 40 in HCT tone guarantees a contrast ratio >= 3.0,
* and a difference of 50 guarantees a contrast ratio >= 4.5.
*/
class Hct {
public:
/**
* Creates an HCT color from hue, chroma, and tone.
*
* @param hue 0 <= hue < 360; invalid values are corrected.
* @param chroma >= 0; the maximum value of chroma depends on the hue
* and tone. May be lower than the requested chroma.
* @param tone 0 <= tone <= 100; invalid values are corrected.
* @return HCT representation of a color in default viewing conditions.
*/
Hct(double hue, double chroma, double tone);
/**
* Creates an HCT color from a color.
*
* @param argb ARGB representation of a color.
* @return HCT representation of a color in default viewing conditions
*/
explicit Hct(Argb argb);
/**
* Returns the hue of the color.
*
* @return hue of the color, in degrees.
*/
double get_hue() const;
/**
* Returns the chroma of the color.
*
* @return chroma of the color.
*/
double get_chroma() const;
/**
* Returns the tone of the color.
*
* @return tone of the color, satisfying 0 <= tone <= 100.
*/
double get_tone() const;
/**
* Returns the color in ARGB format.
*
* @return an integer, representing the color in ARGB format.
*/
Argb ToInt() const;
/**
* Sets the hue of this color. Chroma may decrease because chroma has a
* different maximum for any given hue and tone.
*
* @param new_hue 0 <= new_hue < 360; invalid values are corrected.
*/
void set_hue(double new_hue);
/**
* Sets the chroma of this color. Chroma may decrease because chroma has a
* different maximum for any given hue and tone.
*
* @param new_chroma 0 <= new_chroma < ?
*/
void set_chroma(double new_chroma);
/**
* Sets the tone of this color. Chroma may decrease because chroma has a
* different maximum for any given hue and tone.
*
* @param new_tone 0 <= new_tone <= 100; invalid valids are corrected.
*/
void set_tone(double new_tone);
/**
* For using HCT as a key in a ordered map.
*/
bool operator<(const Hct& a) const { return hue_ < a.hue_; }
private:
/**
* Sets the Hct object to represent an sRGB color.
*
* @param argb the new color as an integer in ARGB format.
*/
void SetInternalState(Argb argb);
double hue_ = 0.0;
double chroma_ = 0.0;
double tone_ = 0.0;
Argb argb_ = 0;
};
} // namespace material_color_utilities
#endif // CPP_CAM_HCT_H_

View file

@ -0,0 +1,526 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "cpp/cam/hct_solver.h"
#include <math.h>
#include "cpp/cam/viewing_conditions.h"
#include "cpp/utils/utils.h"
namespace material_color_utilities {
constexpr double kScaledDiscountFromLinrgb[3][3] = {
{
0.001200833568784504,
0.002389694492170889,
0.0002795742885861124,
},
{
0.0005891086651375999,
0.0029785502573438758,
0.0003270666104008398,
},
{
0.00010146692491640572,
0.0005364214359186694,
0.0032979401770712076,
},
};
constexpr double kLinrgbFromScaledDiscount[3][3] = {
{
1373.2198709594231,
-1100.4251190754821,
-7.278681089101213,
},
{
-271.815969077903,
559.6580465940733,
-32.46047482791194,
},
{
1.9622899599665666,
-57.173814538844006,
308.7233197812385,
},
};
constexpr double kYFromLinrgb[3] = {0.2126, 0.7152, 0.0722};
constexpr double kCriticalPlanes[255] = {
0.015176349177441876, 0.045529047532325624, 0.07588174588720938,
0.10623444424209313, 0.13658714259697685, 0.16693984095186062,
0.19729253930674434, 0.2276452376616281, 0.2579979360165119,
0.28835063437139563, 0.3188300904430532, 0.350925934958123,
0.3848314933096426, 0.42057480301049466, 0.458183274052838,
0.4976837250274023, 0.5391024159806381, 0.5824650784040898,
0.6277969426914107, 0.6751227633498623, 0.7244668422128921,
0.775853049866786, 0.829304845476233, 0.8848452951698498,
0.942497089126609, 1.0022825574869039, 1.0642236851973577,
1.1283421258858297, 1.1946592148522128, 1.2631959812511864,
1.3339731595349034, 1.407011200216447, 1.4823302800086415,
1.5599503113873272, 1.6398909516233677, 1.7221716113234105,
1.8068114625156377, 1.8938294463134073, 1.9832442801866852,
2.075074464868551, 2.1693382909216234, 2.2660538449872063,
2.36523901573795, 2.4669114995532007, 2.5710888059345764,
2.6777882626779785, 2.7870270208169257, 2.898822059350997,
3.0131901897720907, 3.1301480604002863, 3.2497121605402226,
3.3718988244681087, 3.4967242352587946, 3.624204428461639,
3.754355295633311, 3.887192587735158, 4.022731918402185,
4.160988767090289, 4.301978482107941, 4.445716283538092,
4.592217266055746, 4.741496401646282, 4.893568542229298,
5.048448422192488, 5.20615066083972, 5.3666897647573375,
5.5300801301023865, 5.696336044816294, 5.865471690767354,
6.037501145825082, 6.212438385869475, 6.390297286737924,
6.571091626112461, 6.7548350853498045, 6.941541251256611,
7.131223617812143, 7.323895587840543, 7.5195704746346665,
7.7182615035334345, 7.919981813454504, 8.124744458384042,
8.332562408825165, 8.543448553206703, 8.757415699253682,
8.974476575321063, 9.194643831691977, 9.417930041841839,
9.644347703669503, 9.873909240696694, 10.106627003236781,
10.342513269534024, 10.58158024687427, 10.8238400726681,
11.069304815507364, 11.317986476196008, 11.569896988756009,
11.825048221409341, 12.083451977536606, 12.345119996613247,
12.610063955123938, 12.878295467455942, 13.149826086772048,
13.42466730586372, 13.702830557985108, 13.984327217668513,
14.269168601521828, 14.55736596900856, 14.848930523210871,
15.143873411576273, 15.44220572664832, 15.743938506781891,
16.04908273684337, 16.35764934889634, 16.66964922287304,
16.985093187232053, 17.30399201960269, 17.62635644741625,
17.95219714852476, 18.281524751807332, 18.614349837764564,
18.95068293910138, 19.290534541298456, 19.633915083172692,
19.98083495742689, 20.331304511189067, 20.685334046541502,
21.042933821039977, 21.404114048223256, 21.76888489811322,
22.137256497705877, 22.50923893145328, 22.884842241736916,
23.264076429332462, 23.6469514538663, 24.033477234264016,
24.42366364919083, 24.817520537484558, 25.21505769858089,
25.61628489293138, 26.021211842414342, 26.429848230738664,
26.842203703840827, 27.258287870275353, 27.678110301598522,
28.10168053274597, 28.529008062403893, 28.96010235337422,
29.39497283293396, 29.83362889318845, 30.276079891419332,
30.722335150426627, 31.172403958865512, 31.62629557157785,
32.08401920991837, 32.54558406207592, 33.010999283389665,
33.4802739966603, 33.953417292456834, 34.430438229418264,
34.911345834551085, 35.39614910352207, 35.88485700094671,
36.37747846067349, 36.87402238606382, 37.37449765026789,
37.87891309649659, 38.38727753828926, 38.89959975977785,
39.41588851594697, 39.93615253289054, 40.460400508064545,
40.98864111053629, 41.520882981230194, 42.05713473317016,
42.597404951718396, 43.141702194811224, 43.6900349931913,
44.24241185063697, 44.798841244188324, 45.35933162437017,
45.92389141541209, 46.49252901546552, 47.065252796817916,
47.64207110610409, 48.22299226451468, 48.808024568002054,
49.3971762874833, 49.9904556690408, 50.587870934119984,
51.189430279724725, 51.79514187861014, 52.40501387947288,
53.0190544071392, 53.637271562750364, 54.259673423945976,
54.88626804504493, 55.517063457223934, 56.15206766869424,
56.79128866487574, 57.43473440856916, 58.08241284012621,
58.734331877617365, 59.39049941699807, 60.05092333227251,
60.715611475655585, 61.38457167773311, 62.057811747619894,
62.7353394731159, 63.417162620860914, 64.10328893648692,
64.79372614476921, 65.48848194977529, 66.18756403501224,
66.89098006357258, 67.59873767827808, 68.31084450182222,
69.02730813691093, 69.74813616640164, 70.47333615344107,
71.20291564160104, 71.93688215501312, 72.67524319850172,
73.41800625771542, 74.16517879925733, 74.9167682708136,
75.67278210128072, 76.43322770089146, 77.1981124613393,
77.96744375590167, 78.74122893956174, 79.51947534912904,
80.30219030335869, 81.08938110306934, 81.88105503125999,
82.67721935322541, 83.4778813166706, 84.28304815182372,
85.09272707154808, 85.90692527145302, 86.72564993000343,
87.54890820862819, 88.3767072518277, 89.2090541872801,
90.04595612594655, 90.88742016217518, 91.73345337380438,
92.58406282226491, 93.43925555268066, 94.29903859396902,
95.16341895893969, 96.03240364439274, 96.9059996312159,
97.78421388448044, 98.6670533535366, 99.55452497210776,
};
/**
* Sanitizes a small enough angle in radians.
*
* @param angle An angle in radians; must not deviate too much from 0.
* @return A coterminal angle between 0 and 2pi.
*/
double SanitizeRadians(double angle) { return fmod(angle + kPi * 8, kPi * 2); }
/**
* Delinearizes an RGB component, returning a floating-point number.
*
* @param rgb_component 0.0 <= rgb_component <= 100.0, represents linear R/G/B
* channel
* @return 0.0 <= output <= 255.0, color channel converted to regular RGB space
*/
double TrueDelinearized(double rgb_component) {
double normalized = rgb_component / 100.0;
double delinearized = 0.0;
if (normalized <= 0.0031308) {
delinearized = normalized * 12.92;
} else {
delinearized = 1.055 * pow(normalized, 1.0 / 2.4) - 0.055;
}
return delinearized * 255.0;
}
double ChromaticAdaptation(double component) {
double af = pow(abs(component), 0.42);
return Signum(component) * 400.0 * af / (af + 27.13);
}
/**
* Returns the hue of a linear RGB color in CAM16.
*
* @param linrgb The linear RGB coordinates of a color.
* @return The hue of the color in CAM16, in radians.
*/
double HueOf(Vec3 linrgb) {
Vec3 scaledDiscount = MatrixMultiply(linrgb, kScaledDiscountFromLinrgb);
double r_a = ChromaticAdaptation(scaledDiscount.a);
double g_a = ChromaticAdaptation(scaledDiscount.b);
double b_a = ChromaticAdaptation(scaledDiscount.c);
// redness-greenness
double a = (11.0 * r_a + -12.0 * g_a + b_a) / 11.0;
// yellowness-blueness
double b = (r_a + g_a - 2.0 * b_a) / 9.0;
return atan2(b, a);
}
bool AreInCyclicOrder(double a, double b, double c) {
double delta_a_b = SanitizeRadians(b - a);
double delta_a_c = SanitizeRadians(c - a);
return delta_a_b < delta_a_c;
}
/**
* Solves the lerp equation.
*
* @param source The starting number.
* @param mid The number in the middle.
* @param target The ending number.
* @return A number t such that lerp(source, target, t) = mid.
*/
double Intercept(double source, double mid, double target) {
return (mid - source) / (target - source);
}
Vec3 LerpPoint(Vec3 source, double t, Vec3 target) {
return (Vec3){
source.a + (target.a - source.a) * t,
source.b + (target.b - source.b) * t,
source.c + (target.c - source.c) * t,
};
}
double GetAxis(Vec3 vector, int axis) {
switch (axis) {
case 0:
return vector.a;
case 1:
return vector.b;
case 2:
return vector.c;
default:
return -1.0;
}
}
/**
* Intersects a segment with a plane.
*
* @param source The coordinates of point A.
* @param coordinate The R-, G-, or B-coordinate of the plane.
* @param target The coordinates of point B.
* @param axis The axis the plane is perpendicular with. (0: R, 1: G, 2: B)
* @return The intersection point of the segment AB with the plane R=coordinate,
* G=coordinate, or B=coordinate
*/
Vec3 SetCoordinate(Vec3 source, double coordinate, Vec3 target, int axis) {
double t =
Intercept(GetAxis(source, axis), coordinate, GetAxis(target, axis));
return LerpPoint(source, t, target);
}
bool IsBounded(double x) { return 0.0 <= x && x <= 100.0; }
/**
* Returns the nth possible vertex of the polygonal intersection.
*
* @param y The Y value of the plane.
* @param n The zero-based index of the point. 0 <= n <= 11.
* @return The nth possible vertex of the polygonal intersection of the y plane
* and the RGB cube, in linear RGB coordinates, if it exists. If this possible
* vertex lies outside of the cube,
* [-1.0, -1.0, -1.0] is returned.
*/
Vec3 NthVertex(double y, int n) {
double k_r = kYFromLinrgb[0];
double k_g = kYFromLinrgb[1];
double k_b = kYFromLinrgb[2];
double coord_a = n % 4 <= 1 ? 0.0 : 100.0;
double coord_b = n % 2 == 0 ? 0.0 : 100.0;
if (n < 4) {
double g = coord_a;
double b = coord_b;
double r = (y - g * k_g - b * k_b) / k_r;
if (IsBounded(r)) {
return (Vec3){r, g, b};
} else {
return (Vec3){-1.0, -1.0, -1.0};
}
} else if (n < 8) {
double b = coord_a;
double r = coord_b;
double g = (y - r * k_r - b * k_b) / k_g;
if (IsBounded(g)) {
return (Vec3){r, g, b};
} else {
return (Vec3){-1.0, -1.0, -1.0};
}
} else {
double r = coord_a;
double g = coord_b;
double b = (y - r * k_r - g * k_g) / k_b;
if (IsBounded(b)) {
return (Vec3){r, g, b};
} else {
return (Vec3){-1.0, -1.0, -1.0};
}
}
}
/**
* Finds the segment containing the desired color.
*
* @param y The Y value of the color.
* @param target_hue The hue of the color.
* @return A list of two sets of linear RGB coordinates, each corresponding to
* an endpoint of the segment containing the desired color.
*/
void BisectToSegment(double y, double target_hue, Vec3 out[2]) {
Vec3 left = (Vec3){-1.0, -1.0, -1.0};
Vec3 right = left;
double left_hue = 0.0;
double right_hue = 0.0;
bool initialized = false;
bool uncut = true;
for (int n = 0; n < 12; n++) {
Vec3 mid = NthVertex(y, n);
if (mid.a < 0) {
continue;
}
double mid_hue = HueOf(mid);
if (!initialized) {
left = mid;
right = mid;
left_hue = mid_hue;
right_hue = mid_hue;
initialized = true;
continue;
}
if (uncut || AreInCyclicOrder(left_hue, mid_hue, right_hue)) {
uncut = false;
if (AreInCyclicOrder(left_hue, target_hue, mid_hue)) {
right = mid;
right_hue = mid_hue;
} else {
left = mid;
left_hue = mid_hue;
}
}
}
out[0] = left;
out[1] = right;
}
Vec3 Midpoint(Vec3 a, Vec3 b) {
return (Vec3){
(a.a + b.a) / 2,
(a.b + b.b) / 2,
(a.c + b.c) / 2,
};
}
int CriticalPlaneBelow(double x) { return (int)floor(x - 0.5); }
int CriticalPlaneAbove(double x) { return (int)ceil(x - 0.5); }
/**
* Finds a color with the given Y and hue on the boundary of the cube.
*
* @param y The Y value of the color.
* @param target_hue The hue of the color.
* @return The desired color, in linear RGB coordinates.
*/
Vec3 BisectToLimit(double y, double target_hue) {
Vec3 segment[2];
BisectToSegment(y, target_hue, segment);
Vec3 left = segment[0];
double left_hue = HueOf(left);
Vec3 right = segment[1];
for (int axis = 0; axis < 3; axis++) {
if (GetAxis(left, axis) != GetAxis(right, axis)) {
int l_plane = -1;
int r_plane = 255;
if (GetAxis(left, axis) < GetAxis(right, axis)) {
l_plane = CriticalPlaneBelow(TrueDelinearized(GetAxis(left, axis)));
r_plane = CriticalPlaneAbove(TrueDelinearized(GetAxis(right, axis)));
} else {
l_plane = CriticalPlaneAbove(TrueDelinearized(GetAxis(left, axis)));
r_plane = CriticalPlaneBelow(TrueDelinearized(GetAxis(right, axis)));
}
for (int i = 0; i < 8; i++) {
if (abs(r_plane - l_plane) <= 1) {
break;
} else {
int m_plane = (int)floor((l_plane + r_plane) / 2.0);
double mid_plane_coordinate = kCriticalPlanes[m_plane];
Vec3 mid = SetCoordinate(left, mid_plane_coordinate, right, axis);
double mid_hue = HueOf(mid);
if (AreInCyclicOrder(left_hue, target_hue, mid_hue)) {
right = mid;
r_plane = m_plane;
} else {
left = mid;
left_hue = mid_hue;
l_plane = m_plane;
}
}
}
}
}
return Midpoint(left, right);
}
double InverseChromaticAdaptation(double adapted) {
double adapted_abs = abs(adapted);
double base = fmax(0, 27.13 * adapted_abs / (400.0 - adapted_abs));
return Signum(adapted) * pow(base, 1.0 / 0.42);
}
/**
* Finds a color with the given hue, chroma, and Y.
*
* @param hue_radians The desired hue in radians.
* @param chroma The desired chroma.
* @param y The desired Y.
* @return The desired color as a hexadecimal integer, if found; 0 otherwise.
*/
Argb FindResultByJ(double hue_radians, double chroma, double y) {
// Initial estimate of j.
double j = sqrt(y) * 11.0;
// ===========================================================
// Operations inlined from Cam16 to avoid repeated calculation
// ===========================================================
ViewingConditions viewing_conditions = kDefaultViewingConditions;
double t_inner_coeff =
1 /
pow(1.64 - pow(0.29, viewing_conditions.background_y_to_white_point_y),
0.73);
double e_hue = 0.25 * (cos(hue_radians + 2.0) + 3.8);
double p1 = e_hue * (50000.0 / 13.0) * viewing_conditions.n_c *
viewing_conditions.ncb;
double h_sin = sin(hue_radians);
double h_cos = cos(hue_radians);
for (int iteration_round = 0; iteration_round < 5; iteration_round++) {
// ===========================================================
// Operations inlined from Cam16 to avoid repeated calculation
// ===========================================================
double j_normalized = j / 100.0;
double alpha =
chroma == 0.0 || j == 0.0 ? 0.0 : chroma / sqrt(j_normalized);
double t = pow(alpha * t_inner_coeff, 1.0 / 0.9);
double ac =
viewing_conditions.aw *
pow(j_normalized, 1.0 / viewing_conditions.c / viewing_conditions.z);
double p2 = ac / viewing_conditions.nbb;
double gamma = 23.0 * (p2 + 0.305) * t /
(23.0 * p1 + 11 * t * h_cos + 108.0 * t * h_sin);
double a = gamma * h_cos;
double b = gamma * h_sin;
double r_a = (460.0 * p2 + 451.0 * a + 288.0 * b) / 1403.0;
double g_a = (460.0 * p2 - 891.0 * a - 261.0 * b) / 1403.0;
double b_a = (460.0 * p2 - 220.0 * a - 6300.0 * b) / 1403.0;
double r_c_scaled = InverseChromaticAdaptation(r_a);
double g_c_scaled = InverseChromaticAdaptation(g_a);
double b_c_scaled = InverseChromaticAdaptation(b_a);
Vec3 scaled = (Vec3){r_c_scaled, g_c_scaled, b_c_scaled};
Vec3 linrgb = MatrixMultiply(scaled, kLinrgbFromScaledDiscount);
// ===========================================================
// Operations inlined from Cam16 to avoid repeated calculation
// ===========================================================
if (linrgb.a < 0 || linrgb.b < 0 || linrgb.c < 0) {
return 0;
}
double k_r = kYFromLinrgb[0];
double k_g = kYFromLinrgb[1];
double k_b = kYFromLinrgb[2];
double fnj = k_r * linrgb.a + k_g * linrgb.b + k_b * linrgb.c;
if (fnj <= 0) {
return 0;
}
if (iteration_round == 4 || abs(fnj - y) < 0.002) {
if (linrgb.a > 100.01 || linrgb.b > 100.01 || linrgb.c > 100.01) {
return 0;
}
return ArgbFromLinrgb(linrgb);
}
// Iterates with Newton method,
// Using 2 * fn(j) / j as the approximation of fn'(j)
j = j - (fnj - y) * j / (2 * fnj);
}
return 0;
}
/**
* Finds an sRGB color with the given hue, chroma, and L*, if possible.
*
* @param hue_degrees The desired hue, in degrees.
* @param chroma The desired chroma.
* @param lstar The desired L*.
* @return A hexadecimal representing the sRGB color. The color has sufficiently
* close hue, chroma, and L* to the desired values, if possible; otherwise, the
* hue and L* will be sufficiently close, and chroma will be maximized.
*/
Argb SolveToInt(double hue_degrees, double chroma, double lstar) {
if (chroma < 0.0001 || lstar < 0.0001 || lstar > 99.9999) {
return IntFromLstar(lstar);
}
hue_degrees = SanitizeDegreesDouble(hue_degrees);
double hue_radians = hue_degrees / 180 * kPi;
double y = YFromLstar(lstar);
Argb exact_answer = FindResultByJ(hue_radians, chroma, y);
if (exact_answer != 0) {
return exact_answer;
}
Vec3 linrgb = BisectToLimit(y, hue_radians);
return ArgbFromLinrgb(linrgb);
}
/**
* Finds an sRGB color with the given hue, chroma, and L*, if possible.
*
* @param hue_degrees The desired hue, in degrees.
* @param chroma The desired chroma.
* @param lstar The desired L*.
* @return An CAM16 object representing the sRGB color. The color has
* sufficiently close hue, chroma, and L* to the desired values, if possible;
* otherwise, the hue and L* will be sufficiently close, and chroma will be
* maximized.
*/
Cam SolveToCam(double hue_degrees, double chroma, double lstar) {
return CamFromInt(SolveToInt(hue_degrees, chroma, lstar));
}
} // namespace material_color_utilities

View file

@ -0,0 +1,28 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef CPP_CAM_HCT_SOLVER_H_
#define CPP_CAM_HCT_SOLVER_H_
#include "cpp/cam/cam.h"
namespace material_color_utilities {
Argb SolveToInt(double hue_degrees, double chroma, double lstar);
Cam SolveToCam(double hue_degrees, double chroma, double lstar);
} // namespace material_color_utilities
#endif // CPP_CAM_HCT_SOLVER_H_

View file

@ -0,0 +1,74 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "cpp/cam/hct_solver.h"
#include "testing/base/public/gmock.h"
#include "testing/base/public/gunit.h"
#include "cpp/cam/cam.h"
#include "cpp/utils/utils.h"
namespace material_color_utilities {
namespace {
using testing::Eq;
TEST(HctSolverTest, Red) {
// Compute HCT
Argb color = 0xFFFE0315;
Cam cam = CamFromInt(color);
double tone = LstarFromArgb(color);
// Compute input
Argb recovered = SolveToInt(cam.hue, cam.chroma, tone);
EXPECT_THAT(recovered, Eq(color));
}
TEST(HctSolverTest, Green) {
// Compute HCT
Argb color = 0xFF15FE03;
Cam cam = CamFromInt(color);
double tone = LstarFromArgb(color);
// Compute input
Argb recovered = SolveToInt(cam.hue, cam.chroma, tone);
EXPECT_THAT(recovered, Eq(color));
}
TEST(HctSolverTest, Blue) {
// Compute HCT
Argb color = 0xFF0315FE;
Cam cam = CamFromInt(color);
double tone = LstarFromArgb(color);
// Compute input
Argb recovered = SolveToInt(cam.hue, cam.chroma, tone);
EXPECT_THAT(recovered, Eq(color));
}
TEST(HctSolverTest, Exhaustive) {
for (int colorIndex = 0; colorIndex <= 0xFFFFFF; colorIndex++) {
Argb color = 0xFF000000 | colorIndex;
Cam cam = CamFromInt(color);
double tone = LstarFromArgb(color);
// Compute input
Argb recovered = SolveToInt(cam.hue, cam.chroma, tone);
EXPECT_THAT(recovered, Eq(color));
}
}
} // namespace
} // namespace material_color_utilities

View file

@ -0,0 +1,100 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "cpp/cam/hct.h"
#include <tuple>
#include "testing/base/public/gmock.h"
#include "testing/base/public/gunit.h"
#include "cpp/cam/cam.h"
#include "cpp/utils/utils.h"
namespace material_color_utilities {
namespace {
using ::testing::Combine;
using ::testing::DoubleNear;
using ::testing::Eq;
using ::testing::Lt;
using ::testing::TestWithParam;
using ::testing::Values;
TEST(HctTest, LimitedToSRGB) {
// Ensures that the HCT class can only represent sRGB colors.
// An impossibly high chroma is used.
Hct hct(/*hue=*/120.0, /*chroma=*/200.0, /*tone=*/50.0);
Argb argb = hct.ToInt();
// The hue, chroma, and tone members of hct should actually
// represent the sRGB color.
EXPECT_THAT(CamFromInt(argb).hue, Eq(hct.get_hue()));
EXPECT_THAT(CamFromInt(argb).chroma, Eq(hct.get_chroma()));
EXPECT_THAT(LstarFromArgb(argb), Eq(hct.get_tone()));
}
TEST(HctTest, TruncatesColors) {
// Ensures that HCT truncates colors.
Hct hct(/*hue=*/120.0, /*chroma=*/60.0, /*tone=*/50.0);
double chroma = hct.get_chroma();
EXPECT_THAT(chroma, Lt(60.0));
// The new chroma should be lower than the original.
hct.set_tone(180.0);
EXPECT_THAT(hct.get_chroma(), Lt(chroma));
}
bool IsOnBoundary(int rgb_component) {
return rgb_component == 0 || rgb_component == 255;
}
bool ColorIsOnBoundary(int argb) {
return IsOnBoundary(RedFromInt(argb)) || IsOnBoundary(GreenFromInt(argb)) ||
IsOnBoundary(BlueFromInt(argb));
}
using HctTest = TestWithParam<std::tuple<int, int, int>>;
TEST_P(HctTest, Correctness) {
std::tuple<int, int, int> hctTuple = GetParam();
int hue = std::get<0>(hctTuple);
int chroma = std::get<1>(hctTuple);
int tone = std::get<2>(hctTuple);
Hct color(hue, chroma, tone);
if (chroma > 0) {
EXPECT_THAT(color.get_hue(), DoubleNear(hue, 4.0));
}
EXPECT_THAT(color.get_chroma(), Lt(chroma + 2.5));
if (color.get_chroma() < chroma - 2.5) {
EXPECT_TRUE(ColorIsOnBoundary(color.ToInt()));
}
EXPECT_THAT(color.get_tone(), DoubleNear(tone, 0.5));
}
INSTANTIATE_TEST_SUITE_P(
HctTests, HctTest,
Combine(/*hues*/ Values(15, 45, 75, 105, 135, 165, 195, 225, 255, 285, 315,
345),
/*chromas*/ Values(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100),
/*tones*/ Values(20, 30, 40, 50, 60, 70, 80)));
} // namespace
} // namespace material_color_utilities

View file

@ -0,0 +1,118 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "cpp/cam/viewing_conditions.h"
#include <math.h>
#include <stdio.h>
#include "cpp/utils/utils.h"
namespace material_color_utilities {
static double lerp(double start, double stop, double amount) {
return (1.0 - amount) * start + amount * stop;
}
ViewingConditions CreateViewingConditions(const double white_point[3],
const double adapting_luminance,
const double background_lstar,
const double surround,
const bool discounting_illuminant) {
double background_lstar_corrected =
(background_lstar < 30.0) ? 30.0 : background_lstar;
double rgb_w[3] = {
0.401288 * white_point[0] + 0.650173 * white_point[1] -
0.051461 * white_point[2],
-0.250268 * white_point[0] + 1.204414 * white_point[1] +
0.045854 * white_point[2],
-0.002079 * white_point[0] + 0.048952 * white_point[1] +
0.953127 * white_point[2],
};
double f = 0.8 + (surround / 10.0);
double c = f >= 0.9 ? lerp(0.59, 0.69, (f - 0.9) * 10.0)
: lerp(0.525, 0.59, (f - 0.8) * 10.0);
double d = discounting_illuminant
? 1.0
: f * (1.0 - ((1.0 / 3.6) *
exp((-adapting_luminance - 42.0) / 92.0)));
d = d > 1.0 ? 1.0 : d < 0.0 ? 0.0 : d;
double nc = f;
double rgb_d[3] = {(d * (100.0 / rgb_w[0]) + 1.0 - d),
(d * (100.0 / rgb_w[1]) + 1.0 - d),
(d * (100.0 / rgb_w[2]) + 1.0 - d)};
double k = 1.0 / (5.0 * adapting_luminance + 1.0);
double k4 = k * k * k * k;
double k4f = 1.0 - k4;
double fl = (k4 * adapting_luminance) +
(0.1 * k4f * k4f * pow(5.0 * adapting_luminance, 1.0 / 3.0));
double fl_root = pow(fl, 0.25);
double n = YFromLstar(background_lstar_corrected) / white_point[1];
double z = 1.48 + sqrt(n);
double nbb = 0.725 / pow(n, 0.2);
double ncb = nbb;
double rgb_a_factors[3] = {pow(fl * rgb_d[0] * rgb_w[0] / 100.0, 0.42),
pow(fl * rgb_d[1] * rgb_w[1] / 100.0, 0.42),
pow(fl * rgb_d[2] * rgb_w[2] / 100.0, 0.42)};
double rgb_a[3] = {
400.0 * rgb_a_factors[0] / (rgb_a_factors[0] + 27.13),
400.0 * rgb_a_factors[1] / (rgb_a_factors[1] + 27.13),
400.0 * rgb_a_factors[2] / (rgb_a_factors[2] + 27.13),
};
double aw = (40.0 * rgb_a[0] + 20.0 * rgb_a[1] + rgb_a[2]) / 20.0 * nbb;
ViewingConditions viewingConditions = {
adapting_luminance,
background_lstar_corrected,
surround,
discounting_illuminant,
n,
aw,
nbb,
ncb,
c,
nc,
fl,
fl_root,
z,
{white_point[0], white_point[1], white_point[2]},
{rgb_d[0], rgb_d[1], rgb_d[2]},
};
return viewingConditions;
}
ViewingConditions DefaultWithBackgroundLstar(const double background_lstar) {
return CreateViewingConditions(kWhitePointD65,
(200.0 / kPi * YFromLstar(50.0) / 100.0),
background_lstar, 2.0, 0);
}
void PrintDefaultFrame() {
ViewingConditions frame = CreateViewingConditions(
kWhitePointD65, (200.0 / kPi * YFromLstar(50.0) / 100.0), 50.0, 2.0, 0);
printf(
"(Frame){%0.9lf,\n %0.9lf,\n %0.9lf,\n %s\n, %0.9lf,\n "
"%0.9lf,\n%0.9lf,\n%0.9lf,\n%0.9lf,\n%0.9lf,\n"
"%0.9lf,\n%0.9lf,\n%0.9lf,\n%0.9lf,\n"
"%0.9lf,\n%0.9lf\n};",
frame.adapting_luminance, frame.background_lstar, frame.surround,
frame.discounting_illuminant ? "true" : "false",
frame.background_y_to_white_point_y, frame.aw, frame.nbb, frame.ncb,
frame.c, frame.n_c, frame.fl, frame.fl_root, frame.z, frame.rgb_d[0],
frame.rgb_d[1], frame.rgb_d[2]);
}
} // namespace material_color_utilities

View file

@ -0,0 +1,68 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef CPP_CAM_VIEWING_CONDITIONS_H_
#define CPP_CAM_VIEWING_CONDITIONS_H_
namespace material_color_utilities {
struct ViewingConditions {
double adapting_luminance = 0.0;
double background_lstar = 0.0;
double surround = 0.0;
bool discounting_illuminant = false;
double background_y_to_white_point_y = 0.0;
double aw = 0.0;
double nbb = 0.0;
double ncb = 0.0;
double c = 0.0;
double n_c = 0.0;
double fl = 0.0;
double fl_root = 0.0;
double z = 0.0;
double white_point[3] = {0.0, 0.0, 0.0};
double rgb_d[3] = {0.0, 0.0, 0.0};
};
ViewingConditions CreateViewingConditions(const double white_point[3],
const double adapting_luminance,
const double background_lstar,
const double surround,
const bool discounting_illuminant);
ViewingConditions DefaultWithBackgroundLstar(const double background_lstar);
static const ViewingConditions kDefaultViewingConditions = (ViewingConditions){
11.725676537,
50.000000000,
2.000000000,
false,
0.184186503,
29.981000900,
1.016919255,
1.016919255,
0.689999998,
1.000000000,
0.388481468,
0.789482653,
1.909169555,
{95.047, 100.0, 108.883},
{1.021177769, 0.986307740, 0.933960497},
};
} // namespace material_color_utilities
#endif // CPP_CAM_VIEWING_CONDITIONS_H_

View file

@ -0,0 +1,138 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "cpp/contrast/contrast.h"
#include <algorithm>
#include <cmath>
#include "cpp/utils/utils.h"
namespace material_color_utilities {
// Given a color and a contrast ratio to reach, the luminance of a color that
// reaches that ratio with the color can be calculated. However, that luminance
// may not contrast as desired, i.e. the contrast ratio of the input color
// and the returned luminance may not reach the contrast ratio asked for.
//
// When the desired contrast ratio and the result contrast ratio differ by
// more than this amount, an error value should be returned, or the method
// should be documented as 'unsafe', meaning, it will return a valid luminance
// but that luminance may not meet the requested contrast ratio.
//
// 0.04 selected because it ensures the resulting ratio rounds to the
// same tenth.
constexpr double CONTRAST_RATIO_EPSILON = 0.04;
// Color spaces that measure luminance, such as Y in XYZ, L* in L*a*b*,
// or T in HCT, are known as perceptual accurate color spaces.
//
// To be displayed, they must gamut map to a "display space", one that has
// a defined limit on the number of colors. Display spaces include sRGB,
// more commonly understood as RGB/HSL/HSV/HSB.
//
// Gamut mapping is undefined and not defined by the color space. Any
// gamut mapping algorithm must choose how to sacrifice accuracy in hue,
// saturation, and/or lightness.
//
// A principled solution is to maintain lightness, thus maintaining
// contrast/a11y, maintain hue, thus maintaining aesthetic intent, and reduce
// chroma until the color is in gamut.
//
// HCT chooses this solution, but, that doesn't mean it will _exactly_ matched
// desired lightness, if only because RGB is quantized: RGB is expressed as
// a set of integers: there may be an RGB color with, for example,
// 47.892 lightness, but not 47.891.
//
// To allow for this inherent incompatibility between perceptually accurate
// color spaces and display color spaces, methods that take a contrast ratio
// and luminance, and return a luminance that reaches that contrast ratio for
// the input luminance, purposefully darken/lighten their result such that
// the desired contrast ratio will be reached even if inaccuracy is introduced.
//
// 0.4 is generous, ex. HCT requires much less delta. It was chosen because
// it provides a rough guarantee that as long as a percetual color space
// gamut maps lightness such that the resulting lightness rounds to the same
// as the requested, the desired contrast ratio will be reached.
constexpr double LUMINANCE_GAMUT_MAP_TOLERANCE = 0.4;
double RatioOfYs(double y1, double y2) {
double lighter = y1 > y2 ? y1 : y2;
double darker = (lighter == y2) ? y1 : y2;
return (lighter + 5.0) / (darker + 5.0);
}
double RatioOfTones(double tone_a, double tone_b) {
tone_a = std::clamp(tone_a, 0.0, 100.0);
tone_b = std::clamp(tone_b, 0.0, 100.0);
return RatioOfYs(YFromLstar(tone_a), YFromLstar(tone_b));
}
double Lighter(double tone, double ratio) {
if (tone < 0.0 || tone > 100.0) {
return -1.0;
}
double dark_y = YFromLstar(tone);
double light_y = ratio * (dark_y + 5.0) - 5.0;
double real_contrast = RatioOfYs(light_y, dark_y);
double delta = abs(real_contrast - ratio);
if (real_contrast < ratio && delta > CONTRAST_RATIO_EPSILON) {
return -1;
}
// ensure gamut mapping, which requires a 'range' on tone, will still result
// the correct ratio by darkening slightly.
double value = LstarFromY(light_y) + LUMINANCE_GAMUT_MAP_TOLERANCE;
if (value < 0 || value > 100) {
return -1;
}
return value;
}
double Darker(double tone, double ratio) {
if (tone < 0.0 || tone > 100.0) {
return -1.0;
}
double light_y = YFromLstar(tone);
double dark_y = ((light_y + 5.0) / ratio) - 5.0;
double real_contrast = RatioOfYs(light_y, dark_y);
double delta = abs(real_contrast - ratio);
if (real_contrast < ratio && delta > CONTRAST_RATIO_EPSILON) {
return -1;
}
// ensure gamut mapping, which requires a 'range' on tone, will still result
// the correct ratio by darkening slightly.
double value = LstarFromY(dark_y) - LUMINANCE_GAMUT_MAP_TOLERANCE;
if (value < 0 || value > 100) {
return -1;
}
return value;
}
double LighterUnsafe(double tone, double ratio) {
double lighter_safe = Lighter(tone, ratio);
return (lighter_safe < 0.0) ? 100.0 : lighter_safe;
}
double DarkerUnsafe(double tone, double ratio) {
double darker_safe = Darker(tone, ratio);
return (darker_safe < 0.0) ? 0.0 : darker_safe;
}
} // namespace material_color_utilities

View file

@ -0,0 +1,97 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef CPP_CONTRAST_CONTRAST_H_
#define CPP_CONTRAST_CONTRAST_H_
/**
* Utility methods for calculating contrast given two colors, or calculating a
* color given one color and a contrast ratio.
*
* Contrast ratio is calculated using XYZ's Y. When linearized to match human
* perception, Y becomes HCT's tone and L*a*b*'s' L*. Informally, this is the
* lightness of a color.
*
* Methods refer to tone, T in the the HCT color space.
* Tone is equivalent to L* in the L*a*b* color space, or L in the LCH color
* space.
*/
namespace material_color_utilities {
/**
* @return a contrast ratio, which ranges from 1 to 21.
* @param tone_a Tone between 0 and 100. Values outside will be clamped.
* @param tone_b Tone between 0 and 100. Values outside will be clamped.
*/
double RatioOfTones(double tone_a, double tone_b);
/**
* @return a tone >= [tone] that ensures [ratio].
* Return value is between 0 and 100.
* Returns -1 if [ratio] cannot be achieved with [tone].
*
* @param tone Tone return value must contrast with.
* Range is 0 to 100. Invalid values will result in -1 being returned.
* @param ratio Contrast ratio of return value and [tone].
* Range is 1 to 21, invalid values have undefined behavior.
*/
double Lighter(double tone, double ratio);
/**
* @return a tone <= [tone] that ensures [ratio].
* Return value is between 0 and 100.
* Returns -1 if [ratio] cannot be achieved with [tone].
*
* @param tone Tone return value must contrast with.
* Range is 0 to 100. Invalid values will result in -1 being returned.
* @param ratio Contrast ratio of return value and [tone].
* Range is 1 to 21, invalid values have undefined behavior.
*/
double Darker(double tone, double ratio);
/**
* @return a tone >= [tone] that ensures [ratio].
* Return value is between 0 and 100.
* Returns 100 if [ratio] cannot be achieved with [tone].
*
* This method is unsafe because the returned value is guaranteed to be in
* bounds for tone, i.e. between 0 and 100. However, that value may not reach
* the [ratio] with [tone]. For example, there is no color lighter than T100.
*
* @param tone Tone return value must contrast with.
* Range is 0 to 100. Invalid values will result in 100 being returned.
* @param ratio Desired contrast ratio of return value and tone parameter.
* Range is 1 to 21, invalid values have undefined behavior.
*/
double LighterUnsafe(double tone, double ratio);
/**
* @return a tone <= [tone] that ensures [ratio].
* Return value is between 0 and 100.
* Returns 0 if [ratio] cannot be achieved with [tone].
*
* This method is unsafe because the returned value is guaranteed to be in
* bounds for tone, i.e. between 0 and 100. However, that value may not reach
* the [ratio] with [tone]. For example, there is no color darker than T0.
*
* @param tone Tone return value must contrast with.
* Range is 0 to 100. Invalid values will result in 0 being returned.
* @param ratio Desired contrast ratio of return value and tone parameter.
* Range is 1 to 21, invalid values have undefined behavior.
*/
double DarkerUnsafe(double tone, double ratio);
} // namespace material_color_utilities
#endif // CPP_CONTRAST_CONTRAST_H_

View file

@ -0,0 +1,61 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "cpp/contrast/contrast.h"
#include "testing/base/public/gunit.h"
namespace material_color_utilities {
namespace {
TEST(ContrastTest, RatioOfTonesOutOfBoundsInput) {
EXPECT_NEAR(RatioOfTones(-10.0, 110.0), 21.0, 0.001);
}
TEST(ContrastTest, LighterImpossibleRatioErrors) {
EXPECT_NEAR(Lighter(90.0, 10.0), -1.0, 0.001);
}
TEST(ContrastTest, LighterOutOfBoundsInputAboveErrors) {
EXPECT_NEAR(Lighter(110.0, 2.0), -1.0, 0.001);
}
TEST(ContrastTest, LighterOutOfBoundsInputBelowErrors) {
EXPECT_NEAR(Lighter(-10.0, 2.0), -1.0, 0.001);
}
TEST(ContrastTest, LighterUnsafeReturnsMaxTone) {
EXPECT_NEAR(LighterUnsafe(100.0, 2.0), 100, 0.001);
}
TEST(ContrastTest, DarkerImpossibleRatioErrors) {
EXPECT_NEAR(Darker(10.0, 20.0), -1.0, 0.001);
}
TEST(ContrastTest, DarkerOutOfBoundsInputAboveErrors) {
EXPECT_NEAR(Darker(110.0, 2.0), -1.0, 0.001);
}
TEST(ContrastTest, DarkerOutOfBoundsInputBelowErrors) {
EXPECT_NEAR(Darker(-10.0, 2.0), -1.0, 0.001);
}
TEST(ContrastTest, DarkerUnsafeReturnsMinTone) {
EXPECT_NEAR(DarkerUnsafe(0.0, 2.0), 0.0, 0.001);
}
} // namespace
} // namespace material_color_utilities

View file

@ -0,0 +1,42 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "cpp/dislike/dislike.h"
#include <cmath>
#include "cpp/cam/hct.h"
namespace material_color_utilities {
bool IsDisliked(Hct hct) {
double roundedHue = std::round(hct.get_hue());
bool hue_passes = roundedHue >= 90.0 && roundedHue <= 111.0;
bool chroma_passes = std::round(hct.get_chroma()) > 16.0;
bool tone_passes = std::round(hct.get_tone()) < 65.0;
return hue_passes && chroma_passes && tone_passes;
}
Hct FixIfDisliked(Hct hct) {
if (IsDisliked(hct)) {
return Hct(hct.get_hue(), hct.get_chroma(), 70.0);
}
return hct;
}
} // namespace material_color_utilities

View file

@ -0,0 +1,55 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef CPP_DISLIKE_DISLIKE_H_
#define CPP_DISLIKE_DISLIKE_H_
#include "cpp/cam/hct.h"
namespace material_color_utilities {
/**
* Checks and/or fixes universally disliked colors.
*
* Color science studies of color preference indicate universal distaste for
* dark yellow-greens, and also show this is correlated to distate for
* biological waste and rotting food.
*
* See Palmer and Schloss, 2010 or Schloss and Palmer's Chapter 21 in Handbook
* of Color Psychology (2015).
*/
/**
* @return whether the color is disliked.
*
* Disliked is defined as a dark yellow-green that is not neutral.
* @param hct The color to be tested.
*/
bool IsDisliked(Hct hct);
/**
* If a color is disliked, lightens it to make it likable.
*
* The original color is not modified.
*
* @param hct The color to be tested (and fixed, if needed).
* @return The original color if it is not disliked; otherwise, the fixed
* color.
*/
Hct FixIfDisliked(Hct hct);
} // namespace material_color_utilities
#endif // CPP_DISLIKE_DISLIKE_H_

Some files were not shown because too many files have changed in this diff Show more