mirror of
https://github.com/LNH-team/pico-launcher.git
synced 2026-06-02 09:06:54 +02:00
Initial commit
This commit is contained in:
209
arm9/source/material/cam/cam.cpp
Normal file
209
arm9/source/material/cam/cam.cpp
Normal file
@@ -0,0 +1,209 @@
|
||||
/*
|
||||
* 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 "material/cam/cam.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "material/cam/hct_solver.h"
|
||||
#include "material/cam/viewing_conditions.h"
|
||||
#include "material/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);
|
||||
}
|
||||
|
||||
} // namespace material_color_utilities
|
||||
47
arm9/source/material/cam/cam.h
Normal file
47
arm9/source/material/cam/cam.h
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* 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 "material/cam/viewing_conditions.h"
|
||||
#include "material/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);
|
||||
|
||||
} // namespace material_color_utilities
|
||||
#endif // CPP_CAM_CAM_H_
|
||||
57
arm9/source/material/cam/hct.cpp
Normal file
57
arm9/source/material/cam/hct.cpp
Normal 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 "material/cam/hct.h"
|
||||
|
||||
#include "material/cam/hct_solver.h"
|
||||
#include "material/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() { return hue_; }
|
||||
|
||||
double Hct::get_chroma() { return chroma_; }
|
||||
|
||||
double Hct::get_tone() { return tone_; }
|
||||
|
||||
Argb Hct::ToInt() { 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
|
||||
132
arm9/source/material/cam/hct.h
Normal file
132
arm9/source/material/cam/hct.h
Normal file
@@ -0,0 +1,132 @@
|
||||
/*
|
||||
* 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 "material/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();
|
||||
|
||||
/**
|
||||
* Returns the chroma of the color.
|
||||
*
|
||||
* @return chroma of the color.
|
||||
*/
|
||||
double get_chroma();
|
||||
|
||||
/**
|
||||
* Returns the tone of the color.
|
||||
*
|
||||
* @return tone of the color, satisfying 0 <= tone <= 100.
|
||||
*/
|
||||
double get_tone();
|
||||
|
||||
/**
|
||||
* Returns the color in ARGB format.
|
||||
*
|
||||
* @return an integer, representing the color in ARGB format.
|
||||
*/
|
||||
Argb ToInt();
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
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_
|
||||
526
arm9/source/material/cam/hct_solver.cpp
Normal file
526
arm9/source/material/cam/hct_solver.cpp
Normal 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 "material/cam/hct_solver.h"
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#include "material/cam/viewing_conditions.h"
|
||||
#include "material/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
|
||||
28
arm9/source/material/cam/hct_solver.h
Normal file
28
arm9/source/material/cam/hct_solver.h
Normal 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 "material/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_
|
||||
112
arm9/source/material/cam/viewing_conditions.cpp
Normal file
112
arm9/source/material/cam/viewing_conditions.cpp
Normal file
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* 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 "material/cam/viewing_conditions.h"
|
||||
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "material/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;
|
||||
}
|
||||
|
||||
// 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
|
||||
66
arm9/source/material/cam/viewing_conditions.h
Normal file
66
arm9/source/material/cam/viewing_conditions.h
Normal file
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* 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);
|
||||
|
||||
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_
|
||||
88
arm9/source/material/palettes/core.cpp
Normal file
88
arm9/source/material/palettes/core.cpp
Normal file
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* 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 "material/palettes/core.h"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
#include "material/cam/cam.h"
|
||||
#include "material/palettes/tones.h"
|
||||
|
||||
namespace material_color_utilities {
|
||||
|
||||
namespace {
|
||||
|
||||
double PrimaryChroma(double chroma, bool is_content) {
|
||||
return is_content ? chroma : fmax(chroma, 48);
|
||||
}
|
||||
|
||||
double SecondaryChroma(double chroma, bool is_content) {
|
||||
return is_content ? chroma / 3 : 16;
|
||||
}
|
||||
|
||||
double TertiaryChroma(double chroma, bool is_content) {
|
||||
return is_content ? chroma / 2 : 24;
|
||||
}
|
||||
|
||||
double NeutralChroma(double chroma, bool is_content) {
|
||||
return is_content ? fmin(chroma / 12, 4) : 4;
|
||||
}
|
||||
|
||||
double NeutralVariantChroma(double chroma, bool is_content) {
|
||||
return is_content ? fmin(chroma / 6, 8) : 8;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
CorePalette::CorePalette(double hue, double chroma, bool is_content)
|
||||
: primary_(hue, PrimaryChroma(chroma, is_content)),
|
||||
secondary_(hue, SecondaryChroma(chroma, is_content)),
|
||||
tertiary_(hue + 60, TertiaryChroma(chroma, is_content)),
|
||||
neutral_(hue, NeutralChroma(chroma, is_content)),
|
||||
neutral_variant_(hue, NeutralVariantChroma(chroma, is_content)),
|
||||
error_(25, 84) {}
|
||||
|
||||
CorePalette CorePalette::Of(double hue, double chroma) {
|
||||
return CorePalette(hue, chroma, false);
|
||||
}
|
||||
|
||||
CorePalette CorePalette::ContentOf(double hue, double chroma) {
|
||||
return CorePalette(hue, chroma, true);
|
||||
}
|
||||
|
||||
CorePalette CorePalette::Of(int argb) {
|
||||
Cam cam = CamFromInt(argb);
|
||||
return CorePalette(cam.hue, cam.chroma, false);
|
||||
}
|
||||
|
||||
CorePalette CorePalette::ContentOf(int argb) {
|
||||
Cam cam = CamFromInt(argb);
|
||||
return CorePalette(cam.hue, cam.chroma, true);
|
||||
}
|
||||
|
||||
TonalPalette CorePalette::primary() { return primary_; }
|
||||
|
||||
TonalPalette CorePalette::secondary() { return secondary_; }
|
||||
|
||||
TonalPalette CorePalette::tertiary() { return tertiary_; }
|
||||
|
||||
TonalPalette CorePalette::neutral() { return neutral_; }
|
||||
|
||||
TonalPalette CorePalette::neutral_variant() { return neutral_variant_; }
|
||||
|
||||
TonalPalette CorePalette::error() { return error_; }
|
||||
|
||||
} // namespace material_color_utilities
|
||||
71
arm9/source/material/palettes/core.h
Normal file
71
arm9/source/material/palettes/core.h
Normal 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.
|
||||
*/
|
||||
|
||||
#ifndef CPP_PALETTES_CORE_H_
|
||||
#define CPP_PALETTES_CORE_H_
|
||||
|
||||
#include "material/palettes/tones.h"
|
||||
|
||||
namespace material_color_utilities {
|
||||
|
||||
/**
|
||||
* An intermediate concept between the key color for a UI theme, and a full
|
||||
* color scheme. 5 tonal palettes are generated, all except one use the same
|
||||
* hue as the key color, and all vary in chroma.
|
||||
*/
|
||||
class CorePalette {
|
||||
public:
|
||||
/**
|
||||
* Creates a CorePalette from a hue and a chroma.
|
||||
*/
|
||||
static CorePalette Of(double hue, double chroma);
|
||||
|
||||
/**
|
||||
* Creates a CorePalette from a source color in ARGB format.
|
||||
*/
|
||||
static CorePalette Of(int argb);
|
||||
|
||||
/**
|
||||
* Creates a content CorePalette from a hue and a chroma.
|
||||
*/
|
||||
static CorePalette ContentOf(double hue, double chroma);
|
||||
|
||||
/**
|
||||
* Creates a content CorePalette from a source color in ARGB format.
|
||||
*/
|
||||
static CorePalette ContentOf(int argb);
|
||||
|
||||
TonalPalette primary();
|
||||
TonalPalette secondary();
|
||||
TonalPalette tertiary();
|
||||
TonalPalette neutral();
|
||||
TonalPalette neutral_variant();
|
||||
TonalPalette error();
|
||||
|
||||
private:
|
||||
CorePalette(double hue, double chroma, bool is_content);
|
||||
|
||||
TonalPalette primary_;
|
||||
TonalPalette secondary_;
|
||||
TonalPalette tertiary_;
|
||||
TonalPalette neutral_;
|
||||
TonalPalette neutral_variant_;
|
||||
TonalPalette error_;
|
||||
};
|
||||
|
||||
} // namespace material_color_utilities
|
||||
|
||||
#endif // CPP_PALETTES_CORE_H_
|
||||
40
arm9/source/material/palettes/tones.cpp
Normal file
40
arm9/source/material/palettes/tones.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* 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 "material/palettes/tones.h"
|
||||
|
||||
#include <cstdlib>
|
||||
|
||||
#include "material/cam/cam.h"
|
||||
|
||||
namespace material_color_utilities {
|
||||
|
||||
TonalPalette::TonalPalette(Argb argb) {
|
||||
Cam cam = CamFromInt(argb);
|
||||
hue_ = cam.hue;
|
||||
chroma_ = cam.chroma;
|
||||
}
|
||||
|
||||
TonalPalette::TonalPalette(double hue, double chroma) {
|
||||
hue_ = hue;
|
||||
chroma_ = chroma;
|
||||
}
|
||||
|
||||
Argb TonalPalette::get(double tone) const {
|
||||
return IntFromHcl(hue_, chroma_, tone);
|
||||
}
|
||||
|
||||
} // namespace material_color_utilities
|
||||
43
arm9/source/material/palettes/tones.h
Normal file
43
arm9/source/material/palettes/tones.h
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* 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_PALETTES_TONES_H_
|
||||
#define CPP_PALETTES_TONES_H_
|
||||
|
||||
#include "material/utils/utils.h"
|
||||
|
||||
namespace material_color_utilities {
|
||||
|
||||
class TonalPalette {
|
||||
public:
|
||||
explicit TonalPalette(Argb argb);
|
||||
TonalPalette(double hue, double chroma);
|
||||
|
||||
/**
|
||||
* Returns the color for a given tone in this palette.
|
||||
*
|
||||
* @param tone 0.0 <= tone <= 100.0
|
||||
* @return a color as an integer, in ARGB format.
|
||||
*/
|
||||
Argb get(double tone) const;
|
||||
|
||||
private:
|
||||
double hue_;
|
||||
double chroma_;
|
||||
};
|
||||
|
||||
} // namespace material_color_utilities
|
||||
#endif // CPP_PALETTES_TONES_H_
|
||||
113
arm9/source/material/scheme/scheme.cpp
Normal file
113
arm9/source/material/scheme/scheme.cpp
Normal file
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
* 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 "material/scheme/scheme.h"
|
||||
|
||||
#include "material/palettes/core.h"
|
||||
|
||||
// This file is automatically generated. Do not modify it.
|
||||
|
||||
namespace material_color_utilities {
|
||||
|
||||
Scheme MaterialLightColorSchemeFromPalette(CorePalette palette) {
|
||||
Scheme scheme = {};
|
||||
|
||||
scheme.primary = palette.primary().get(40);
|
||||
scheme.on_primary = palette.primary().get(100);
|
||||
scheme.primary_container = palette.primary().get(90);
|
||||
scheme.on_primary_container = palette.primary().get(10);
|
||||
scheme.secondary = palette.secondary().get(40);
|
||||
scheme.on_secondary = palette.secondary().get(100);
|
||||
scheme.secondary_container = palette.secondary().get(90);
|
||||
scheme.on_secondary_container = palette.secondary().get(10);
|
||||
scheme.tertiary = palette.tertiary().get(40);
|
||||
scheme.on_tertiary = palette.tertiary().get(100);
|
||||
scheme.tertiary_container = palette.tertiary().get(90);
|
||||
scheme.on_tertiary_container = palette.tertiary().get(10);
|
||||
scheme.error = palette.error().get(40);
|
||||
scheme.on_error = palette.error().get(100);
|
||||
scheme.error_container = palette.error().get(90);
|
||||
scheme.on_error_container = palette.error().get(10);
|
||||
scheme.background = palette.neutral().get(99);
|
||||
scheme.on_background = palette.neutral().get(10);
|
||||
scheme.surface = palette.neutral().get(99);
|
||||
scheme.on_surface = palette.neutral().get(10);
|
||||
scheme.surface_variant = palette.neutral_variant().get(90);
|
||||
scheme.on_surface_variant = palette.neutral_variant().get(30);
|
||||
scheme.outline = palette.neutral_variant().get(50);
|
||||
scheme.outline_variant = palette.neutral_variant().get(80);
|
||||
scheme.shadow = palette.neutral().get(0);
|
||||
scheme.scrim = palette.neutral().get(0);
|
||||
scheme.inverse_surface = palette.neutral().get(20);
|
||||
scheme.inverse_on_surface = palette.neutral().get(95);
|
||||
scheme.inverse_primary = palette.primary().get(80);
|
||||
|
||||
return scheme;
|
||||
}
|
||||
|
||||
Scheme MaterialDarkColorSchemeFromPalette(CorePalette palette) {
|
||||
Scheme scheme = {};
|
||||
|
||||
scheme.primary = palette.primary().get(80);
|
||||
scheme.on_primary = palette.primary().get(20);
|
||||
scheme.primary_container = palette.primary().get(30);
|
||||
scheme.on_primary_container = palette.primary().get(90);
|
||||
scheme.secondary = palette.secondary().get(80);
|
||||
scheme.on_secondary = palette.secondary().get(20);
|
||||
scheme.secondary_container = palette.secondary().get(30);
|
||||
scheme.on_secondary_container = palette.secondary().get(90);
|
||||
scheme.tertiary = palette.tertiary().get(80);
|
||||
scheme.on_tertiary = palette.tertiary().get(20);
|
||||
scheme.tertiary_container = palette.tertiary().get(30);
|
||||
scheme.on_tertiary_container = palette.tertiary().get(90);
|
||||
scheme.error = palette.error().get(80);
|
||||
scheme.on_error = palette.error().get(20);
|
||||
scheme.error_container = palette.error().get(30);
|
||||
scheme.on_error_container = palette.error().get(80);
|
||||
scheme.background = palette.neutral().get(10);
|
||||
scheme.on_background = palette.neutral().get(90);
|
||||
scheme.surface = palette.neutral().get(10);
|
||||
scheme.on_surface = palette.neutral().get(90);
|
||||
scheme.surface_variant = palette.neutral_variant().get(30);
|
||||
scheme.on_surface_variant = palette.neutral_variant().get(80);
|
||||
scheme.outline = palette.neutral_variant().get(60);
|
||||
scheme.outline_variant = palette.neutral_variant().get(30);
|
||||
scheme.shadow = palette.neutral().get(0);
|
||||
scheme.scrim = palette.neutral().get(0);
|
||||
scheme.inverse_surface = palette.neutral().get(90);
|
||||
scheme.inverse_on_surface = palette.neutral().get(20);
|
||||
scheme.inverse_primary = palette.primary().get(40);
|
||||
|
||||
return scheme;
|
||||
}
|
||||
|
||||
Scheme MaterialLightColorScheme(Argb color) {
|
||||
return MaterialLightColorSchemeFromPalette(CorePalette::Of(color));
|
||||
}
|
||||
|
||||
Scheme MaterialDarkColorScheme(Argb color) {
|
||||
return MaterialDarkColorSchemeFromPalette(CorePalette::Of(color));
|
||||
}
|
||||
|
||||
Scheme MaterialLightContentColorScheme(Argb color) {
|
||||
return MaterialLightColorSchemeFromPalette(CorePalette::ContentOf(color));
|
||||
}
|
||||
|
||||
Scheme MaterialDarkContentColorScheme(Argb color) {
|
||||
return MaterialDarkColorSchemeFromPalette(CorePalette::ContentOf(color));
|
||||
}
|
||||
|
||||
} // namespace material_color_utilities
|
||||
95
arm9/source/material/scheme/scheme.h
Normal file
95
arm9/source/material/scheme/scheme.h
Normal file
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* 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_SCHEME_SCHEME_H_
|
||||
#define CPP_SCHEME_SCHEME_H_
|
||||
|
||||
#include "material/palettes/core.h"
|
||||
#include "material/utils/utils.h"
|
||||
|
||||
// This file is automatically generated. Do not modify it.
|
||||
|
||||
namespace material_color_utilities {
|
||||
|
||||
struct Scheme {
|
||||
Argb primary = 0;
|
||||
Argb on_primary = 0;
|
||||
Argb primary_container = 0;
|
||||
Argb on_primary_container = 0;
|
||||
Argb secondary = 0;
|
||||
Argb on_secondary = 0;
|
||||
Argb secondary_container = 0;
|
||||
Argb on_secondary_container = 0;
|
||||
Argb tertiary = 0;
|
||||
Argb on_tertiary = 0;
|
||||
Argb tertiary_container = 0;
|
||||
Argb on_tertiary_container = 0;
|
||||
Argb error = 0;
|
||||
Argb on_error = 0;
|
||||
Argb error_container = 0;
|
||||
Argb on_error_container = 0;
|
||||
Argb background = 0;
|
||||
Argb on_background = 0;
|
||||
Argb surface = 0;
|
||||
Argb on_surface = 0;
|
||||
Argb surface_variant = 0;
|
||||
Argb on_surface_variant = 0;
|
||||
Argb outline = 0;
|
||||
Argb outline_variant = 0;
|
||||
Argb shadow = 0;
|
||||
Argb scrim = 0;
|
||||
Argb inverse_surface = 0;
|
||||
Argb inverse_on_surface = 0;
|
||||
Argb inverse_primary = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns the light material color scheme based on the given core palette.
|
||||
*/
|
||||
Scheme MaterialLightColorSchemeFromPalette(CorePalette palette);
|
||||
|
||||
/**
|
||||
* Returns the dark material color scheme based on the given core palette.
|
||||
*/
|
||||
Scheme MaterialDarkColorSchemeFromPalette(CorePalette palette);
|
||||
|
||||
/**
|
||||
* Returns the light material color scheme based on the given color,
|
||||
* in ARGB format.
|
||||
*/
|
||||
Scheme MaterialLightColorScheme(Argb color);
|
||||
|
||||
/**
|
||||
* Returns the dark material color scheme based on the given color,
|
||||
* in ARGB format.
|
||||
*/
|
||||
Scheme MaterialDarkColorScheme(Argb color);
|
||||
|
||||
/**
|
||||
* Returns the light material content color scheme based on the given color,
|
||||
* in ARGB format.
|
||||
*/
|
||||
Scheme MaterialLightContentColorScheme(Argb color);
|
||||
|
||||
/**
|
||||
* Returns the dark material content color scheme based on the given color,
|
||||
* in ARGB format.
|
||||
*/
|
||||
Scheme MaterialDarkContentColorScheme(Argb color);
|
||||
|
||||
} // namespace material_color_utilities
|
||||
|
||||
#endif // CPP_SCHEME_SCHEME_H_
|
||||
173
arm9/source/material/utils/utils.cpp
Normal file
173
arm9/source/material/utils/utils.cpp
Normal file
@@ -0,0 +1,173 @@
|
||||
/*
|
||||
* 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 "material/utils/utils.h"
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
|
||||
// #include "absl/strings/str_cat.h"
|
||||
|
||||
namespace material_color_utilities {
|
||||
|
||||
int RedFromInt(const Argb argb) { return (argb & 0x00ff0000) >> 16; }
|
||||
|
||||
int GreenFromInt(const Argb argb) { return (argb & 0x0000ff00) >> 8; }
|
||||
|
||||
int BlueFromInt(const Argb argb) { return (argb & 0x000000ff); }
|
||||
|
||||
Argb ArgbFromRgb(const int red, const int green, const int blue) {
|
||||
return 0xFF000000 | ((red & 0xff) << 16) | ((green & 0xff) << 8) |
|
||||
(blue & 0xff);
|
||||
}
|
||||
|
||||
// Converts a color from linear RGB components to ARGB format.
|
||||
Argb ArgbFromLinrgb(Vec3 linrgb) {
|
||||
int r = Delinearized(linrgb.a);
|
||||
int g = Delinearized(linrgb.b);
|
||||
int b = Delinearized(linrgb.c);
|
||||
|
||||
return 0xFF000000 | ((r & 0x0ff) << 16) | ((g & 0x0ff) << 8) | (b & 0x0ff);
|
||||
}
|
||||
|
||||
int Delinearized(const double rgb_component) {
|
||||
double normalized = rgb_component / 100;
|
||||
double delinearized;
|
||||
if (normalized <= 0.0031308) {
|
||||
delinearized = normalized * 12.92;
|
||||
} else {
|
||||
delinearized = 1.055 * std::pow(normalized, 1.0 / 2.4) - 0.055;
|
||||
}
|
||||
return std::clamp((int)round(delinearized * 255.0), 0, 255);
|
||||
}
|
||||
|
||||
double Linearized(const int rgb_component) {
|
||||
double normalized = rgb_component / 255.0;
|
||||
if (normalized <= 0.040449936) {
|
||||
return normalized / 12.92 * 100.0;
|
||||
} else {
|
||||
return std::pow((normalized + 0.055) / 1.055, 2.4) * 100.0;
|
||||
}
|
||||
}
|
||||
|
||||
int AlphaFromInt(Argb argb) { return (argb & 0xff000000) >> 24; }
|
||||
|
||||
bool IsOpaque(Argb argb) { return AlphaFromInt(argb) == 255; }
|
||||
|
||||
double LstarFromArgb(Argb argb) {
|
||||
// xyz from argb
|
||||
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 y = 0.2126 * red_l + 0.7152 * green_l + 0.0722 * blue_l;
|
||||
return LstarFromY(y);
|
||||
}
|
||||
|
||||
double YFromLstar(double lstar) {
|
||||
static const double ke = 8.0;
|
||||
if (lstar > ke) {
|
||||
double cube_root = (lstar + 16.0) / 116.0;
|
||||
double cube = cube_root * cube_root * cube_root;
|
||||
return cube * 100.0;
|
||||
} else {
|
||||
return lstar / (24389.0 / 27.0) * 100.0;
|
||||
}
|
||||
}
|
||||
|
||||
double LstarFromY(double y) {
|
||||
static const double e = 216.0 / 24389.0;
|
||||
double yNormalized = y / 100.0;
|
||||
if (yNormalized <= e) {
|
||||
return (24389.0 / 27.0) * yNormalized;
|
||||
} else {
|
||||
return 116.0 * std::pow(yNormalized, 1.0 / 3.0) - 16.0;
|
||||
}
|
||||
}
|
||||
|
||||
int SanitizeDegreesInt(const int degrees) {
|
||||
if (degrees < 0) {
|
||||
return (degrees % 360) + 360;
|
||||
} else if (degrees >= 360.0) {
|
||||
return degrees % 360;
|
||||
} else {
|
||||
return degrees;
|
||||
}
|
||||
}
|
||||
|
||||
// Sanitizes a degree measure as a floating-point number.
|
||||
//
|
||||
// Returns a degree measure between 0.0 (inclusive) and 360.0 (exclusive).
|
||||
double SanitizeDegreesDouble(const double degrees) {
|
||||
if (degrees < 0.0) {
|
||||
return fmod(degrees, 360.0) + 360;
|
||||
} else if (degrees >= 360.0) {
|
||||
return fmod(degrees, 360.0);
|
||||
} else {
|
||||
return degrees;
|
||||
}
|
||||
}
|
||||
|
||||
double DiffDegrees(const double a, const double b) {
|
||||
return 180.0 - abs(abs(a - b) - 180.0);
|
||||
}
|
||||
|
||||
double RotationDirection(const double from, const double to) {
|
||||
double increasing_difference = SanitizeDegreesDouble(to - from);
|
||||
return increasing_difference <= 180.0 ? 1.0 : -1.0;
|
||||
}
|
||||
|
||||
// Converts a color in ARGB format to a hexadecimal string in lowercase.
|
||||
//
|
||||
// For instance: hex_from_argb(0xff012345) == "ff012345"
|
||||
// std::string HexFromArgb(Argb argb) { return absl::StrCat(absl::Hex(argb)); }
|
||||
|
||||
Argb IntFromLstar(const double lstar) {
|
||||
double y = YFromLstar(lstar);
|
||||
int component = Delinearized(y);
|
||||
return ArgbFromRgb(component, component, component);
|
||||
}
|
||||
|
||||
// The signum function.
|
||||
//
|
||||
// Returns 1 if num > 0, -1 if num < 0, and 0 if num = 0
|
||||
int Signum(double num) {
|
||||
if (num < 0) {
|
||||
return -1;
|
||||
} else if (num == 0) {
|
||||
return 0;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
Vec3 MatrixMultiply(Vec3 input, const double matrix[3][3]) {
|
||||
double a =
|
||||
input.a * matrix[0][0] + input.b * matrix[0][1] + input.c * matrix[0][2];
|
||||
double b =
|
||||
input.a * matrix[1][0] + input.b * matrix[1][1] + input.c * matrix[1][2];
|
||||
double c =
|
||||
input.a * matrix[2][0] + input.b * matrix[2][1] + input.c * matrix[2][2];
|
||||
return (Vec3){a, b, c};
|
||||
}
|
||||
} // namespace material_color_utilities
|
||||
204
arm9/source/material/utils/utils.h
Normal file
204
arm9/source/material/utils/utils.h
Normal file
@@ -0,0 +1,204 @@
|
||||
/*
|
||||
* 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_UTILS_UTILS_H_
|
||||
#define CPP_UTILS_UTILS_H_
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
namespace material_color_utilities {
|
||||
|
||||
using Argb = uint32_t;
|
||||
|
||||
/**
|
||||
* A vector with three floating-point numbers as components.
|
||||
*/
|
||||
struct Vec3 {
|
||||
double a = 0.0;
|
||||
double b = 0.0;
|
||||
double c = 0.0;
|
||||
};
|
||||
|
||||
/**
|
||||
* Value of pi.
|
||||
*/
|
||||
inline constexpr double kPi = 3.141592653589793;
|
||||
|
||||
/**
|
||||
* Returns the standard white point; white on a sunny day.
|
||||
*/
|
||||
inline constexpr double kWhitePointD65[] = {95.047, 100.0, 108.883};
|
||||
|
||||
/**
|
||||
* Returns the red component of a color in ARGB format.
|
||||
*/
|
||||
int RedFromInt(const Argb argb);
|
||||
|
||||
/**
|
||||
* Returns the green component of a color in ARGB format.
|
||||
*/
|
||||
int GreenFromInt(const Argb argb);
|
||||
|
||||
/**
|
||||
* Returns the blue component of a color in ARGB format.
|
||||
*/
|
||||
int BlueFromInt(const Argb argb);
|
||||
|
||||
/**
|
||||
* Returns the alpha component of a color in ARGB format.
|
||||
*/
|
||||
int AlphaFromInt(const Argb argb);
|
||||
|
||||
/**
|
||||
* Converts a color from RGB components to ARGB format.
|
||||
*/
|
||||
Argb ArgbFromRgb(const int red, const int green, const int blue);
|
||||
|
||||
/**
|
||||
* Converts a color from linear RGB components to ARGB format.
|
||||
*/
|
||||
Argb ArgbFromLinrgb(Vec3 linrgb);
|
||||
|
||||
/**
|
||||
* Returns whether a color in ARGB format is opaque.
|
||||
*/
|
||||
bool IsOpaque(const Argb argb);
|
||||
|
||||
/**
|
||||
* Sanitizes a degree measure as an integer.
|
||||
*
|
||||
* @return a degree measure between 0 (inclusive) and 360 (exclusive).
|
||||
*/
|
||||
int SanitizeDegreesInt(const int degrees);
|
||||
|
||||
/**
|
||||
* Sanitizes a degree measure as an floating-point number.
|
||||
*
|
||||
* @return a degree measure between 0.0 (inclusive) and 360.0 (exclusive).
|
||||
*/
|
||||
double SanitizeDegreesDouble(const double degrees);
|
||||
|
||||
/**
|
||||
* Distance of two points on a circle, represented using degrees.
|
||||
*/
|
||||
double DiffDegrees(const double a, const double b);
|
||||
|
||||
/**
|
||||
* Sign of direction change needed to travel from one angle to
|
||||
* another.
|
||||
*
|
||||
* For angles that are 180 degrees apart from each other, both
|
||||
* directions have the same travel distance, so either direction is
|
||||
* shortest. The value 1.0 is returned in this case.
|
||||
*
|
||||
* @param from The angle travel starts from, in degrees.
|
||||
*
|
||||
* @param to The angle travel ends at, in degrees.
|
||||
*
|
||||
* @return -1 if decreasing from leads to the shortest travel
|
||||
* distance, 1 if increasing from leads to the shortest travel
|
||||
* distance.
|
||||
*/
|
||||
double RotationDirection(const double from, const double to);
|
||||
|
||||
/**
|
||||
* Computes the L* value of a color in ARGB representation.
|
||||
*
|
||||
* @param argb ARGB representation of a color
|
||||
*
|
||||
* @return L*, from L*a*b*, coordinate of the color
|
||||
*/
|
||||
double LstarFromArgb(const Argb argb);
|
||||
|
||||
/**
|
||||
* Returns the hexadecimal representation of a color.
|
||||
*/
|
||||
// std::string HexFromArgb(Argb argb);
|
||||
|
||||
/**
|
||||
* Linearizes an RGB component.
|
||||
*
|
||||
* @param rgb_component 0 <= rgb_component <= 255, represents R/G/B
|
||||
* channel
|
||||
*
|
||||
* @return 0.0 <= output <= 100.0, color channel converted to
|
||||
* linear RGB space
|
||||
*/
|
||||
double Linearized(const int rgb_component);
|
||||
|
||||
/**
|
||||
* Delinearizes an RGB component.
|
||||
*
|
||||
* @param rgb_component 0.0 <= rgb_component <= 100.0, represents linear
|
||||
* R/G/B channel
|
||||
*
|
||||
* @return 0 <= output <= 255, color channel converted to regular
|
||||
* RGB space
|
||||
*/
|
||||
int Delinearized(const double rgb_component);
|
||||
|
||||
/**
|
||||
* Converts an L* value to a Y value.
|
||||
*
|
||||
* L* in L*a*b* and Y in XYZ measure the same quantity, luminance.
|
||||
*
|
||||
* L* measures perceptual luminance, a linear scale. Y in XYZ
|
||||
* measures relative luminance, a logarithmic scale.
|
||||
*
|
||||
* @param lstar L* in L*a*b*. 0.0 <= L* <= 100.0
|
||||
*
|
||||
* @return Y in XYZ. 0.0 <= Y <= 100.0
|
||||
*/
|
||||
double YFromLstar(const double lstar);
|
||||
|
||||
/**
|
||||
* Converts a Y value to an L* value.
|
||||
*
|
||||
* L* in L*a*b* and Y in XYZ measure the same quantity, luminance.
|
||||
*
|
||||
* L* measures perceptual luminance, a linear scale. Y in XYZ
|
||||
* measures relative luminance, a logarithmic scale.
|
||||
*
|
||||
* @param y Y in XYZ. 0.0 <= Y <= 100.0
|
||||
*
|
||||
* @return L* in L*a*b*. 0.0 <= L* <= 100.0
|
||||
*/
|
||||
double LstarFromY(const double y);
|
||||
|
||||
/**
|
||||
* Converts an L* value to an ARGB representation.
|
||||
*
|
||||
* @param lstar L* in L*a*b*. 0.0 <= L* <= 100.0
|
||||
*
|
||||
* @return ARGB representation of grayscale color with lightness matching L*
|
||||
*/
|
||||
Argb IntFromLstar(const double lstar);
|
||||
|
||||
/**
|
||||
* The signum function.
|
||||
*
|
||||
* @return 1 if num > 0, -1 if num < 0, and 0 if num = 0
|
||||
*/
|
||||
int Signum(double num);
|
||||
|
||||
/**
|
||||
* Multiplies a 1x3 row vector with a 3x3 matrix, returning the product.
|
||||
*/
|
||||
Vec3 MatrixMultiply(Vec3 input, const double matrix[3][3]);
|
||||
|
||||
} // namespace material_color_utilities
|
||||
#endif // CPP_UTILS_UTILS_H_
|
||||
Reference in New Issue
Block a user