• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.ux.material.libmonet.scheme;
18 
19 import com.google.ux.material.libmonet.hct.Hct;
20 import com.google.ux.material.libmonet.palettes.TonalPalette;
21 import com.google.ux.material.libmonet.utils.MathUtils;
22 
23 /**
24  * Provides important settings for creating colors dynamically, and 6 color palettes. Requires: 1. A
25  * color. (source color) 2. A theme. (Variant) 3. Whether or not its dark mode. 4. Contrast level.
26  * (-1 to 1, currently contrast ratio 3.0 and 7.0)
27  */
28 public class DynamicScheme {
29   public final int sourceColorArgb;
30   public final Hct sourceColorHct;
31   public final Variant variant;
32   public final boolean isDark;
33   public final double contrastLevel;
34 
35   public final TonalPalette primaryPalette;
36   public final TonalPalette secondaryPalette;
37   public final TonalPalette tertiaryPalette;
38   public final TonalPalette neutralPalette;
39   public final TonalPalette neutralVariantPalette;
40   public final TonalPalette errorPalette;
41 
DynamicScheme( Hct sourceColorHct, Variant variant, boolean isDark, double contrastLevel, TonalPalette primaryPalette, TonalPalette secondaryPalette, TonalPalette tertiaryPalette, TonalPalette neutralPalette, TonalPalette neutralVariantPalette)42   public DynamicScheme(
43       Hct sourceColorHct,
44       Variant variant,
45       boolean isDark,
46       double contrastLevel,
47       TonalPalette primaryPalette,
48       TonalPalette secondaryPalette,
49       TonalPalette tertiaryPalette,
50       TonalPalette neutralPalette,
51       TonalPalette neutralVariantPalette) {
52     this.sourceColorArgb = sourceColorHct.toInt();
53     this.sourceColorHct = sourceColorHct;
54     this.variant = variant;
55     this.isDark = isDark;
56     this.contrastLevel = contrastLevel;
57 
58     this.primaryPalette = primaryPalette;
59     this.secondaryPalette = secondaryPalette;
60     this.tertiaryPalette = tertiaryPalette;
61     this.neutralPalette = neutralPalette;
62     this.neutralVariantPalette = neutralVariantPalette;
63     this.errorPalette = TonalPalette.fromHueAndChroma(25.0, 84.0);
64   }
65 
66   /**
67    * Given a set of hues and set of hue rotations, locate which hues the source color's hue is
68    * between, apply the rotation at the same index as the first hue in the range, and return the
69    * rotated hue.
70    *
71    * @param sourceColorHct The color whose hue should be rotated.
72    * @param hues A set of hues.
73    * @param rotations A set of hue rotations.
74    * @return Color's hue with a rotation applied.
75    */
getRotatedHue(Hct sourceColorHct, double[] hues, double[] rotations)76   public static double getRotatedHue(Hct sourceColorHct, double[] hues, double[] rotations) {
77     final double sourceHue = sourceColorHct.getHue();
78     if (rotations.length == 1) {
79       return MathUtils.sanitizeDegreesDouble(sourceHue + rotations[0]);
80     }
81     final int size = hues.length;
82     for (int i = 0; i <= (size - 2); i++) {
83       final double thisHue = hues[i];
84       final double nextHue = hues[i + 1];
85       if (thisHue < sourceHue && sourceHue < nextHue) {
86         return MathUtils.sanitizeDegreesDouble(sourceHue + rotations[i]);
87       }
88     }
89     // If this statement executes, something is wrong, there should have been a rotation
90     // found using the arrays.
91     return sourceHue;
92   }
93 }
94