Skip to content

Commit 9e67d5e

Browse files
Material Engcopybara-github
authored andcommitted
Improved code for constant arrays and matrices.
PiperOrigin-RevId: 435282593
1 parent d9d688c commit 9e67d5e

File tree

3 files changed

+62
-76
lines changed

3 files changed

+62
-76
lines changed

dart/lib/utils/color_utils.dart

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@ import 'package:material_color_utilities/utils/math_utils.dart';
2222
/// Utility methods for color science constants and color space
2323
/// conversions that aren't HCT or CAM16.
2424
class ColorUtils {
25+
static final _SRGB_TO_XYZ = [
26+
[0.41233895, 0.35762064, 0.18051042],
27+
[0.2126, 0.7152, 0.0722],
28+
[0.01932141, 0.11916382, 0.95034478],
29+
];
30+
31+
static final _XYZ_TO_SRGB = [
32+
[3.2406, -1.5372, -0.4986],
33+
[-0.9689, 1.8758, 0.0415],
34+
[0.0557, -0.204, 1.057],
35+
];
36+
37+
static final _WHITE_POINT_D65 = [95.047, 100.0, 108.883];
38+
2539
/// Converts a color from RGB components to ARGB format.
2640
static int argbFromRgb(int red, int green, int blue) {
2741
return 255 << 24 | (red & 255) << 16 | (green & 255) << 8 | blue & 255;
@@ -52,27 +66,9 @@ class ColorUtils {
5266
return alphaFromArgb(argb) >= 255;
5367
}
5468

55-
/// Returns the sRGB to XYZ transformation matrix.
56-
static List<List<double>> srgbToXyz() {
57-
return [
58-
[0.41233895, 0.35762064, 0.18051042],
59-
[0.2126, 0.7152, 0.0722],
60-
[0.01932141, 0.11916382, 0.95034478],
61-
];
62-
}
63-
64-
/// Returns the XYZ to sRGB transformation matrix.
65-
static List<List<double>> xyzToSrgb() {
66-
return [
67-
[3.2406, -1.5372, -0.4986],
68-
[-0.9689, 1.8758, 0.0415],
69-
[0.0557, -0.204, 1.057],
70-
];
71-
}
72-
7369
/// Converts a color from ARGB to XYZ.
7470
static int argbFromXyz(double x, double y, double z) {
75-
final linearRgb = MathUtils.matrixMultiply([x, y, z], xyzToSrgb());
71+
final linearRgb = MathUtils.matrixMultiply([x, y, z], _XYZ_TO_SRGB);
7672
final r = delinearized(linearRgb[0]);
7773
final g = delinearized(linearRgb[1]);
7874
final b = delinearized(linearRgb[2]);
@@ -84,13 +80,13 @@ class ColorUtils {
8480
final r = linearized(redFromArgb(argb));
8581
final g = linearized(greenFromArgb(argb));
8682
final b = linearized(blueFromArgb(argb));
87-
return MathUtils.matrixMultiply([r, g, b], srgbToXyz());
83+
return MathUtils.matrixMultiply([r, g, b], _SRGB_TO_XYZ);
8884
}
8985

9086
/// Converts a color represented in Lab color space into an ARGB
9187
/// integer.
9288
static int argbFromLab(double l, double a, double b) {
93-
final whitePoint = whitePointD65();
89+
final whitePoint = _WHITE_POINT_D65;
9490
final fy = (l + 16.0) / 116.0;
9591
final fx = a / 500.0 + fy;
9692
final fz = fy - b / 200.0;
@@ -110,7 +106,7 @@ class ColorUtils {
110106
/// [argb] the ARGB representation of a color
111107
/// Returns a Lab object representing the color
112108
static List<double> labFromArgb(int argb) {
113-
final whitePoint = whitePointD65();
109+
final whitePoint = _WHITE_POINT_D65;
114110
final xyz = xyzFromArgb(argb);
115111
final xNormalized = xyz[0] / whitePoint[0];
116112
final yNormalized = xyz[1] / whitePoint[1];
@@ -141,7 +137,7 @@ class ColorUtils {
141137
final cubeExceedEpsilon = fy * fy * fy > epsilon;
142138
final x = cubeExceedEpsilon ? fx * fx * fx : lstar / kappa;
143139
final z = cubeExceedEpsilon ? fz * fz * fz : lstar / kappa;
144-
final whitePoint = whitePointD65();
140+
final whitePoint = _WHITE_POINT_D65;
145141
return argbFromXyz(
146142
x * whitePoint[0],
147143
y * whitePoint[1],
@@ -223,7 +219,7 @@ class ColorUtils {
223219
///
224220
/// Returns The white point
225221
static List<double> whitePointD65() {
226-
return [95.047, 100.0, 108.883];
222+
return _WHITE_POINT_D65;
227223
}
228224

229225
static double _labF(double t) {

java/utils/ColorUtils.java

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,22 @@
2727
public class ColorUtils {
2828
private ColorUtils() {}
2929

30+
static final double[][] SRGB_TO_XYZ =
31+
new double[][] {
32+
new double[] {0.41233895, 0.35762064, 0.18051042},
33+
new double[] {0.2126, 0.7152, 0.0722},
34+
new double[] {0.01932141, 0.11916382, 0.95034478},
35+
};
36+
37+
static final double[][] XYZ_TO_SRGB =
38+
new double[][] {
39+
new double[] {3.2406, -1.5372, -0.4986},
40+
new double[] {-0.9689, 1.8758, 0.0415},
41+
new double[] {0.0557, -0.204, 1.057},
42+
};
43+
44+
static final double[] WHITE_POINT_D65 = new double[] {95.047, 100.0, 108.883};
45+
3046
/** Converts a color from RGB components to ARGB format. */
3147
public static int argbFromRgb(int red, int green, int blue) {
3248
return (255 << 24) | ((red & 255) << 16) | ((green & 255) << 8) | (blue & 255);
@@ -57,27 +73,9 @@ public static boolean isOpaque(int argb) {
5773
return alphaFromArgb(argb) >= 255;
5874
}
5975

60-
/** Returns the sRGB to XYZ transformation matrix. */
61-
public static double[][] srgbToXyz() {
62-
return new double[][] {
63-
new double[] {0.41233895, 0.35762064, 0.18051042},
64-
new double[] {0.2126, 0.7152, 0.0722},
65-
new double[] {0.01932141, 0.11916382, 0.95034478},
66-
};
67-
}
68-
69-
/** Returns the XYZ to sRGB transformation matrix. */
70-
public static double[][] xyzToSrgb() {
71-
return new double[][] {
72-
new double[] {3.2406, -1.5372, -0.4986},
73-
new double[] {-0.9689, 1.8758, 0.0415},
74-
new double[] {0.0557, -0.204, 1.057},
75-
};
76-
}
77-
7876
/** Converts a color from ARGB to XYZ. */
7977
public static int argbFromXyz(double x, double y, double z) {
80-
double[] linearRgb = MathUtils.matrixMultiply(new double[] {x, y, z}, xyzToSrgb());
78+
double[] linearRgb = MathUtils.matrixMultiply(new double[] {x, y, z}, XYZ_TO_SRGB);
8179
int r = delinearized(linearRgb[0]);
8280
int g = delinearized(linearRgb[1]);
8381
int b = delinearized(linearRgb[2]);
@@ -89,12 +87,12 @@ public static double[] xyzFromArgb(int argb) {
8987
double r = linearized(redFromArgb(argb));
9088
double g = linearized(greenFromArgb(argb));
9189
double b = linearized(blueFromArgb(argb));
92-
return MathUtils.matrixMultiply(new double[] {r, g, b}, srgbToXyz());
90+
return MathUtils.matrixMultiply(new double[] {r, g, b}, SRGB_TO_XYZ);
9391
}
9492

9593
/** Converts a color represented in Lab color space into an ARGB integer. */
9694
public static int argbFromLab(double l, double a, double b) {
97-
double[] whitePoint = whitePointD65();
95+
double[] whitePoint = WHITE_POINT_D65;
9896
double fy = (l + 16.0) / 116.0;
9997
double fx = a / 500.0 + fy;
10098
double fz = fy - b / 200.0;
@@ -114,7 +112,7 @@ public static int argbFromLab(double l, double a, double b) {
114112
* @return a Lab object representing the color
115113
*/
116114
public static double[] labFromArgb(int argb) {
117-
double[] whitePoint = whitePointD65();
115+
double[] whitePoint = WHITE_POINT_D65;
118116
double[] xyz = xyzFromArgb(argb);
119117
double xNormalized = xyz[0] / whitePoint[0];
120118
double yNormalized = xyz[1] / whitePoint[1];
@@ -145,7 +143,7 @@ public static int argbFromLstar(double lstar) {
145143
boolean cubeExceedEpsilon = fy * fy * fy > epsilon;
146144
double x = cubeExceedEpsilon ? fx * fx * fx : lstar / kappa;
147145
double z = cubeExceedEpsilon ? fz * fz * fz : lstar / kappa;
148-
double[] whitePoint = whitePointD65();
146+
double[] whitePoint = WHITE_POINT_D65;
149147
return argbFromXyz(x * whitePoint[0], y * whitePoint[1], z * whitePoint[2]);
150148
}
151149

@@ -224,7 +222,7 @@ public static int delinearized(double rgbComponent) {
224222
* @return The white point
225223
*/
226224
public static double[] whitePointD65() {
227-
return new double[] {95.047, 100.0, 108.883};
225+
return WHITE_POINT_D65;
228226
}
229227

230228
static double labF(double t) {

typescript/utils/color_utils.ts

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ import * as mathUtils from './math_utils';
2626
* conversions that aren't HCT or CAM16.
2727
*/
2828

29+
const SRGB_TO_XYZ = [
30+
[0.41233895, 0.35762064, 0.18051042],
31+
[0.2126, 0.7152, 0.0722],
32+
[0.01932141, 0.11916382, 0.95034478],
33+
];
34+
35+
const XYZ_TO_SRGB = [
36+
[3.2406, -1.5372, -0.4986],
37+
[-0.9689, 1.8758, 0.0415],
38+
[0.0557, -0.204, 1.057],
39+
];
40+
41+
const WHITE_POINT_D65 = [95.047, 100.0, 108.883];
42+
2943
/**
3044
* Converts a color from RGB components to ARGB format.
3145
*/
@@ -69,33 +83,11 @@ export function isOpaque(argb: number): boolean {
6983
return alphaFromArgb(argb) >= 255;
7084
}
7185

72-
/**
73-
* Returns the sRGB to XYZ transformation matrix.
74-
*/
75-
export function srgbToXyz(): number[][] {
76-
return [
77-
[0.41233895, 0.35762064, 0.18051042],
78-
[0.2126, 0.7152, 0.0722],
79-
[0.01932141, 0.11916382, 0.95034478],
80-
];
81-
}
82-
83-
/**
84-
* Returns the XYZ to sRGB transformation matrix.
85-
*/
86-
export function xyzToSrgb(): number[][] {
87-
return [
88-
[3.2406, -1.5372, -0.4986],
89-
[-0.9689, 1.8758, 0.0415],
90-
[0.0557, -0.204, 1.057],
91-
];
92-
}
93-
9486
/**
9587
* Converts a color from ARGB to XYZ.
9688
*/
9789
export function argbFromXyz(x: number, y: number, z: number): number {
98-
const linearRgb = mathUtils.matrixMultiply([x, y, z], xyzToSrgb());
90+
const linearRgb = mathUtils.matrixMultiply([x, y, z], XYZ_TO_SRGB);
9991
const r = delinearized(linearRgb[0]);
10092
const g = delinearized(linearRgb[1]);
10193
const b = delinearized(linearRgb[2]);
@@ -109,15 +101,15 @@ export function xyzFromArgb(argb: number): number[] {
109101
const r = linearized(redFromArgb(argb));
110102
const g = linearized(greenFromArgb(argb));
111103
const b = linearized(blueFromArgb(argb));
112-
return mathUtils.matrixMultiply([r, g, b], srgbToXyz());
104+
return mathUtils.matrixMultiply([r, g, b], SRGB_TO_XYZ);
113105
}
114106

115107
/**
116108
* Converts a color represented in Lab color space into an ARGB
117109
* integer.
118110
*/
119111
export function argbFromLab(l: number, a: number, b: number): number {
120-
const whitePoint = whitePointD65();
112+
const whitePoint = WHITE_POINT_D65;
121113
const fy = (l + 16.0) / 116.0;
122114
const fx = a / 500.0 + fy;
123115
const fz = fy - b / 200.0;
@@ -139,7 +131,7 @@ export function argbFromLab(l: number, a: number, b: number): number {
139131
* @return a Lab object representing the color
140132
*/
141133
export function labFromArgb(argb: number): number[] {
142-
const whitePoint = whitePointD65();
134+
const whitePoint = WHITE_POINT_D65;
143135
const xyz = xyzFromArgb(argb);
144136
const xNormalized = xyz[0] / whitePoint[0];
145137
const yNormalized = xyz[1] / whitePoint[1];
@@ -172,7 +164,7 @@ export function argbFromLstar(lstar: number): number {
172164
const cubeExceedEpsilon = fy * fy * fy > epsilon;
173165
const x = cubeExceedEpsilon ? fx * fx * fx : lstar / kappa;
174166
const z = cubeExceedEpsilon ? fz * fz * fz : lstar / kappa;
175-
const whitePoint = whitePointD65();
167+
const whitePoint = WHITE_POINT_D65;
176168
return argbFromXyz(
177169
x * whitePoint[0],
178170
y * whitePoint[1],
@@ -264,7 +256,7 @@ export function delinearized(rgbComponent: number): number {
264256
* @return The white point
265257
*/
266258
export function whitePointD65(): number[] {
267-
return [95.047, 100.0, 108.883];
259+
return WHITE_POINT_D65;
268260
}
269261

270262
function labF(t: number): number {

0 commit comments

Comments
 (0)