• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // This file is automatically generated. Do not modify it.
18 
19 package com.google.ux.material.libmonet.blend;
20 
21 import com.google.ux.material.libmonet.hct.Cam16;
22 import com.google.ux.material.libmonet.hct.Hct;
23 import com.google.ux.material.libmonet.utils.ColorUtils;
24 import com.google.ux.material.libmonet.utils.MathUtils;
25 
26 /** Functions for blending in HCT and CAM16. */
27 public class Blend {
Blend()28   private Blend() {}
29 
30   /**
31    * Blend the design color's HCT hue towards the key color's HCT hue, in a way that leaves the
32    * original color recognizable and recognizably shifted towards the key color.
33    *
34    * @param designColor ARGB representation of an arbitrary color.
35    * @param sourceColor ARGB representation of the main theme color.
36    * @return The design color with a hue shifted towards the system's color, a slightly
37    *     warmer/cooler variant of the design color's hue.
38    */
harmonize(int designColor, int sourceColor)39   public static int harmonize(int designColor, int sourceColor) {
40     Hct fromHct = Hct.fromInt(designColor);
41     Hct toHct = Hct.fromInt(sourceColor);
42     double differenceDegrees = MathUtils.differenceDegrees(fromHct.getHue(), toHct.getHue());
43     double rotationDegrees = Math.min(differenceDegrees * 0.5, 15.0);
44     double outputHue =
45         MathUtils.sanitizeDegreesDouble(
46             fromHct.getHue()
47                 + rotationDegrees * MathUtils.rotationDirection(fromHct.getHue(), toHct.getHue()));
48     return Hct.from(outputHue, fromHct.getChroma(), fromHct.getTone()).toInt();
49   }
50 
51   /**
52    * Blends hue from one color into another. The chroma and tone of the original color are
53    * maintained.
54    *
55    * @param from ARGB representation of color
56    * @param to ARGB representation of color
57    * @param amount how much blending to perform; 0.0 >= and <= 1.0
58    * @return from, with a hue blended towards to. Chroma and tone are constant.
59    */
hctHue(int from, int to, double amount)60   public static int hctHue(int from, int to, double amount) {
61     int ucs = cam16Ucs(from, to, amount);
62     Cam16 ucsCam = Cam16.fromInt(ucs);
63     Cam16 fromCam = Cam16.fromInt(from);
64     Hct blended = Hct.from(ucsCam.getHue(), fromCam.getChroma(), ColorUtils.lstarFromArgb(from));
65     return blended.toInt();
66   }
67 
68   /**
69    * Blend in CAM16-UCS space.
70    *
71    * @param from ARGB representation of color
72    * @param to ARGB representation of color
73    * @param amount how much blending to perform; 0.0 >= and <= 1.0
74    * @return from, blended towards to. Hue, chroma, and tone will change.
75    */
cam16Ucs(int from, int to, double amount)76   public static int cam16Ucs(int from, int to, double amount) {
77     Cam16 fromCam = Cam16.fromInt(from);
78     Cam16 toCam = Cam16.fromInt(to);
79     double fromJ = fromCam.getJstar();
80     double fromA = fromCam.getAstar();
81     double fromB = fromCam.getBstar();
82     double toJ = toCam.getJstar();
83     double toA = toCam.getAstar();
84     double toB = toCam.getBstar();
85     double jstar = fromJ + (toJ - fromJ) * amount;
86     double astar = fromA + (toA - fromA) * amount;
87     double bstar = fromB + (toB - fromB) * amount;
88     return Cam16.fromUcs(jstar, astar, bstar).toInt();
89   }
90 }
91 
92