1*970e1046SAndroid Build Coastguard Worker /* 2*970e1046SAndroid Build Coastguard Worker * Copyright 2021 Google LLC 3*970e1046SAndroid Build Coastguard Worker * 4*970e1046SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*970e1046SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*970e1046SAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*970e1046SAndroid Build Coastguard Worker * 8*970e1046SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*970e1046SAndroid Build Coastguard Worker * 10*970e1046SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*970e1046SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*970e1046SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*970e1046SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*970e1046SAndroid Build Coastguard Worker * limitations under the License. 15*970e1046SAndroid Build Coastguard Worker */ 16*970e1046SAndroid Build Coastguard Worker 17*970e1046SAndroid Build Coastguard Worker // This file is automatically generated. Do not modify it. 18*970e1046SAndroid Build Coastguard Worker 19*970e1046SAndroid Build Coastguard Worker package com.google.ux.material.libmonet.blend; 20*970e1046SAndroid Build Coastguard Worker 21*970e1046SAndroid Build Coastguard Worker import com.google.ux.material.libmonet.hct.Cam16; 22*970e1046SAndroid Build Coastguard Worker import com.google.ux.material.libmonet.hct.Hct; 23*970e1046SAndroid Build Coastguard Worker import com.google.ux.material.libmonet.utils.ColorUtils; 24*970e1046SAndroid Build Coastguard Worker import com.google.ux.material.libmonet.utils.MathUtils; 25*970e1046SAndroid Build Coastguard Worker 26*970e1046SAndroid Build Coastguard Worker /** Functions for blending in HCT and CAM16. */ 27*970e1046SAndroid Build Coastguard Worker public class Blend { Blend()28*970e1046SAndroid Build Coastguard Worker private Blend() {} 29*970e1046SAndroid Build Coastguard Worker 30*970e1046SAndroid Build Coastguard Worker /** 31*970e1046SAndroid Build Coastguard Worker * Blend the design color's HCT hue towards the key color's HCT hue, in a way that leaves the 32*970e1046SAndroid Build Coastguard Worker * original color recognizable and recognizably shifted towards the key color. 33*970e1046SAndroid Build Coastguard Worker * 34*970e1046SAndroid Build Coastguard Worker * @param designColor ARGB representation of an arbitrary color. 35*970e1046SAndroid Build Coastguard Worker * @param sourceColor ARGB representation of the main theme color. 36*970e1046SAndroid Build Coastguard Worker * @return The design color with a hue shifted towards the system's color, a slightly 37*970e1046SAndroid Build Coastguard Worker * warmer/cooler variant of the design color's hue. 38*970e1046SAndroid Build Coastguard Worker */ harmonize(int designColor, int sourceColor)39*970e1046SAndroid Build Coastguard Worker public static int harmonize(int designColor, int sourceColor) { 40*970e1046SAndroid Build Coastguard Worker Hct fromHct = Hct.fromInt(designColor); 41*970e1046SAndroid Build Coastguard Worker Hct toHct = Hct.fromInt(sourceColor); 42*970e1046SAndroid Build Coastguard Worker double differenceDegrees = MathUtils.differenceDegrees(fromHct.getHue(), toHct.getHue()); 43*970e1046SAndroid Build Coastguard Worker double rotationDegrees = Math.min(differenceDegrees * 0.5, 15.0); 44*970e1046SAndroid Build Coastguard Worker double outputHue = 45*970e1046SAndroid Build Coastguard Worker MathUtils.sanitizeDegreesDouble( 46*970e1046SAndroid Build Coastguard Worker fromHct.getHue() 47*970e1046SAndroid Build Coastguard Worker + rotationDegrees * MathUtils.rotationDirection(fromHct.getHue(), toHct.getHue())); 48*970e1046SAndroid Build Coastguard Worker return Hct.from(outputHue, fromHct.getChroma(), fromHct.getTone()).toInt(); 49*970e1046SAndroid Build Coastguard Worker } 50*970e1046SAndroid Build Coastguard Worker 51*970e1046SAndroid Build Coastguard Worker /** 52*970e1046SAndroid Build Coastguard Worker * Blends hue from one color into another. The chroma and tone of the original color are 53*970e1046SAndroid Build Coastguard Worker * maintained. 54*970e1046SAndroid Build Coastguard Worker * 55*970e1046SAndroid Build Coastguard Worker * @param from ARGB representation of color 56*970e1046SAndroid Build Coastguard Worker * @param to ARGB representation of color 57*970e1046SAndroid Build Coastguard Worker * @param amount how much blending to perform; 0.0 >= and <= 1.0 58*970e1046SAndroid Build Coastguard Worker * @return from, with a hue blended towards to. Chroma and tone are constant. 59*970e1046SAndroid Build Coastguard Worker */ hctHue(int from, int to, double amount)60*970e1046SAndroid Build Coastguard Worker public static int hctHue(int from, int to, double amount) { 61*970e1046SAndroid Build Coastguard Worker int ucs = cam16Ucs(from, to, amount); 62*970e1046SAndroid Build Coastguard Worker Cam16 ucsCam = Cam16.fromInt(ucs); 63*970e1046SAndroid Build Coastguard Worker Cam16 fromCam = Cam16.fromInt(from); 64*970e1046SAndroid Build Coastguard Worker Hct blended = Hct.from(ucsCam.getHue(), fromCam.getChroma(), ColorUtils.lstarFromArgb(from)); 65*970e1046SAndroid Build Coastguard Worker return blended.toInt(); 66*970e1046SAndroid Build Coastguard Worker } 67*970e1046SAndroid Build Coastguard Worker 68*970e1046SAndroid Build Coastguard Worker /** 69*970e1046SAndroid Build Coastguard Worker * Blend in CAM16-UCS space. 70*970e1046SAndroid Build Coastguard Worker * 71*970e1046SAndroid Build Coastguard Worker * @param from ARGB representation of color 72*970e1046SAndroid Build Coastguard Worker * @param to ARGB representation of color 73*970e1046SAndroid Build Coastguard Worker * @param amount how much blending to perform; 0.0 >= and <= 1.0 74*970e1046SAndroid Build Coastguard Worker * @return from, blended towards to. Hue, chroma, and tone will change. 75*970e1046SAndroid Build Coastguard Worker */ cam16Ucs(int from, int to, double amount)76*970e1046SAndroid Build Coastguard Worker public static int cam16Ucs(int from, int to, double amount) { 77*970e1046SAndroid Build Coastguard Worker Cam16 fromCam = Cam16.fromInt(from); 78*970e1046SAndroid Build Coastguard Worker Cam16 toCam = Cam16.fromInt(to); 79*970e1046SAndroid Build Coastguard Worker double fromJ = fromCam.getJstar(); 80*970e1046SAndroid Build Coastguard Worker double fromA = fromCam.getAstar(); 81*970e1046SAndroid Build Coastguard Worker double fromB = fromCam.getBstar(); 82*970e1046SAndroid Build Coastguard Worker double toJ = toCam.getJstar(); 83*970e1046SAndroid Build Coastguard Worker double toA = toCam.getAstar(); 84*970e1046SAndroid Build Coastguard Worker double toB = toCam.getBstar(); 85*970e1046SAndroid Build Coastguard Worker double jstar = fromJ + (toJ - fromJ) * amount; 86*970e1046SAndroid Build Coastguard Worker double astar = fromA + (toA - fromA) * amount; 87*970e1046SAndroid Build Coastguard Worker double bstar = fromB + (toB - fromB) * amount; 88*970e1046SAndroid Build Coastguard Worker return Cam16.fromUcs(jstar, astar, bstar).toInt(); 89*970e1046SAndroid Build Coastguard Worker } 90*970e1046SAndroid Build Coastguard Worker } 91*970e1046SAndroid Build Coastguard Worker 92