1 /* 2 * Copyright 2021 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.palettes; 18 19 import static java.lang.Math.max; 20 import static java.lang.Math.min; 21 22 import com.google.ux.material.libmonet.hct.Hct; 23 24 /** 25 * An intermediate concept between the key color for a UI theme, and a full color scheme. 5 sets of 26 * tones are generated, all except one use the same hue as the key color, and all vary in chroma. 27 */ 28 public final class CorePalette { 29 public TonalPalette a1; 30 public TonalPalette a2; 31 public TonalPalette a3; 32 public TonalPalette n1; 33 public TonalPalette n2; 34 public TonalPalette error; 35 36 /** 37 * Create key tones from a color. 38 * 39 * @param argb ARGB representation of a color 40 */ of(int argb)41 public static CorePalette of(int argb) { 42 return new CorePalette(argb, false); 43 } 44 45 /** 46 * Create content key tones from a color. 47 * 48 * @param argb ARGB representation of a color 49 */ contentOf(int argb)50 public static CorePalette contentOf(int argb) { 51 return new CorePalette(argb, true); 52 } 53 CorePalette(int argb, boolean isContent)54 private CorePalette(int argb, boolean isContent) { 55 Hct hct = Hct.fromInt(argb); 56 double hue = hct.getHue(); 57 double chroma = hct.getChroma(); 58 if (isContent) { 59 this.a1 = TonalPalette.fromHueAndChroma(hue, chroma); 60 this.a2 = TonalPalette.fromHueAndChroma(hue, chroma / 3.); 61 this.a3 = TonalPalette.fromHueAndChroma(hue + 60., chroma / 2.); 62 this.n1 = TonalPalette.fromHueAndChroma(hue, min(chroma / 12., 4.)); 63 this.n2 = TonalPalette.fromHueAndChroma(hue, min(chroma / 6., 8.)); 64 } else { 65 this.a1 = TonalPalette.fromHueAndChroma(hue, max(48., chroma)); 66 this.a2 = TonalPalette.fromHueAndChroma(hue, 16.); 67 this.a3 = TonalPalette.fromHueAndChroma(hue + 60., 24.); 68 this.n1 = TonalPalette.fromHueAndChroma(hue, 4.); 69 this.n2 = TonalPalette.fromHueAndChroma(hue, 8.); 70 } 71 this.error = TonalPalette.fromHueAndChroma(25, 84.); 72 } 73 } 74