• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2023 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.dynamiccolor;
18 
19 import com.google.ux.material.libmonet.utils.MathUtils;
20 
21 /**
22  * A class containing a value that changes with the contrast level.
23  *
24  * <p>Usually represents the contrast requirements for a dynamic color on its background. The four
25  * values correspond to values for contrast levels -1.0, 0.0, 0.5, and 1.0, respectively.
26  */
27 public final class ContrastCurve {
28   /** Value for contrast level -1.0 */
29   private final double low;
30 
31   /** Value for contrast level 0.0 */
32   private final double normal;
33 
34   /** Value for contrast level 0.5 */
35   private final double medium;
36 
37   /** Value for contrast level 1.0 */
38   private final double high;
39 
40   /**
41    * Creates a `ContrastCurve` object.
42    *
43    * @param low Value for contrast level -1.0
44    * @param normal Value for contrast level 0.0
45    * @param medium Value for contrast level 0.5
46    * @param high Value for contrast level 1.0
47    */
ContrastCurve(double low, double normal, double medium, double high)48   public ContrastCurve(double low, double normal, double medium, double high) {
49     this.low = low;
50     this.normal = normal;
51     this.medium = medium;
52     this.high = high;
53   }
54 
55   /**
56    * Returns the value at a given contrast level.
57    *
58    * @param contrastLevel The contrast level. 0.0 is the default (normal); -1.0 is the lowest; 1.0
59    *     is the highest.
60    * @return The value. For contrast ratios, a number between 1.0 and 21.0.
61    */
get(double contrastLevel)62   public double get(double contrastLevel) {
63     if (contrastLevel <= -1.0) {
64       return this.low;
65     } else if (contrastLevel < 0.0) {
66       return MathUtils.lerp(this.low, this.normal, (contrastLevel - -1) / 1);
67     } else if (contrastLevel < 0.5) {
68       return MathUtils.lerp(this.normal, this.medium, (contrastLevel - 0) / 0.5);
69     } else if (contrastLevel < 1.0) {
70       return MathUtils.lerp(this.medium, this.high, (contrastLevel - 0.5) / 0.5);
71     } else {
72       return this.high;
73     }
74   }
75 }
76