xref: /aosp_15_r20/external/libmonet/dislike/DislikeAnalyzer.java (revision 970e10460f970939fd510dd6ad3e0d65908272e3)
1*970e1046SAndroid Build Coastguard Worker /*
2*970e1046SAndroid Build Coastguard Worker  * Copyright 2022 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 package com.google.ux.material.libmonet.dislike;
18*970e1046SAndroid Build Coastguard Worker 
19*970e1046SAndroid Build Coastguard Worker import com.google.ux.material.libmonet.hct.Hct;
20*970e1046SAndroid Build Coastguard Worker 
21*970e1046SAndroid Build Coastguard Worker /**
22*970e1046SAndroid Build Coastguard Worker  * Check and/or fix universally disliked colors.
23*970e1046SAndroid Build Coastguard Worker  *
24*970e1046SAndroid Build Coastguard Worker  * <p>Color science studies of color preference indicate universal distaste for dark yellow-greens,
25*970e1046SAndroid Build Coastguard Worker  * and also show this is correlated to distate for biological waste and rotting food.
26*970e1046SAndroid Build Coastguard Worker  *
27*970e1046SAndroid Build Coastguard Worker  * <p>See Palmer and Schloss, 2010 or Schloss and Palmer's Chapter 21 in Handbook of Color
28*970e1046SAndroid Build Coastguard Worker  * Psychology (2015).
29*970e1046SAndroid Build Coastguard Worker  */
30*970e1046SAndroid Build Coastguard Worker public final class DislikeAnalyzer {
31*970e1046SAndroid Build Coastguard Worker 
DislikeAnalyzer()32*970e1046SAndroid Build Coastguard Worker   private DislikeAnalyzer() {
33*970e1046SAndroid Build Coastguard Worker     throw new UnsupportedOperationException();
34*970e1046SAndroid Build Coastguard Worker   }
35*970e1046SAndroid Build Coastguard Worker 
36*970e1046SAndroid Build Coastguard Worker   /**
37*970e1046SAndroid Build Coastguard Worker    * Returns true if color is disliked.
38*970e1046SAndroid Build Coastguard Worker    *
39*970e1046SAndroid Build Coastguard Worker    * <p>Disliked is defined as a dark yellow-green that is not neutral.
40*970e1046SAndroid Build Coastguard Worker    */
isDisliked(Hct hct)41*970e1046SAndroid Build Coastguard Worker   public static boolean isDisliked(Hct hct) {
42*970e1046SAndroid Build Coastguard Worker     final boolean huePasses = Math.round(hct.getHue()) >= 90.0 && Math.round(hct.getHue()) <= 111.0;
43*970e1046SAndroid Build Coastguard Worker     final boolean chromaPasses = Math.round(hct.getChroma()) > 16.0;
44*970e1046SAndroid Build Coastguard Worker     final boolean tonePasses = Math.round(hct.getTone()) < 65.0;
45*970e1046SAndroid Build Coastguard Worker 
46*970e1046SAndroid Build Coastguard Worker     return huePasses && chromaPasses && tonePasses;
47*970e1046SAndroid Build Coastguard Worker   }
48*970e1046SAndroid Build Coastguard Worker 
49*970e1046SAndroid Build Coastguard Worker   /** If color is disliked, lighten it to make it likable. */
50*970e1046SAndroid Build Coastguard Worker   public static Hct fixIfDisliked(Hct hct) {
51*970e1046SAndroid Build Coastguard Worker     if (isDisliked(hct)) {
52*970e1046SAndroid Build Coastguard Worker       return Hct.from(hct.getHue(), hct.getChroma(), 70.0);
53*970e1046SAndroid Build Coastguard Worker     }
54*970e1046SAndroid Build Coastguard Worker 
55*970e1046SAndroid Build Coastguard Worker     return hct;
56*970e1046SAndroid Build Coastguard Worker   }
57*970e1046SAndroid Build Coastguard Worker }
58