|
| 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 | +#ifndef CPP_DYNAMICCOLOR_CONTRAST_CURVE_H_ |
| 18 | +#define CPP_DYNAMICCOLOR_CONTRAST_CURVE_H_ |
| 19 | + |
| 20 | +#include "cpp/utils/utils.h" |
| 21 | + |
| 22 | +namespace material_color_utilities { |
| 23 | + |
| 24 | +/** |
| 25 | + * Documents a constraint between two DynamicColors, in which their tones must |
| 26 | + * have a certain distance from each other. |
| 27 | + */ |
| 28 | +struct ContrastCurve { |
| 29 | + double low; |
| 30 | + double normal; |
| 31 | + double medium; |
| 32 | + double high; |
| 33 | + |
| 34 | + /** |
| 35 | + * Creates a `ContrastCurve` object. |
| 36 | + * |
| 37 | + * @param low Contrast requirement for contrast level -1.0 |
| 38 | + * @param normal Contrast requirement for contrast level 0.0 |
| 39 | + * @param medium Contrast requirement for contrast level 0.5 |
| 40 | + * @param high Contrast requirement for contrast level 1.0 |
| 41 | + */ |
| 42 | + ContrastCurve(double low, double normal, double medium, double high) |
| 43 | + : low(low), normal(normal), medium(medium), high(high) {} |
| 44 | + |
| 45 | + /** |
| 46 | + * Returns the contrast ratio at a given contrast level. |
| 47 | + * |
| 48 | + * @param contrastLevel The contrast level. 0.0 is the default (normal); |
| 49 | + * -1.0 is the lowest; 1.0 is the highest. |
| 50 | + * @return The contrast ratio, a number between 1.0 and 21.0. |
| 51 | + */ |
| 52 | + double getContrast(double contrastLevel) { |
| 53 | + if (contrastLevel <= -1.0) { |
| 54 | + return low; |
| 55 | + } else if (contrastLevel < 0.0) { |
| 56 | + return Lerp(low, normal, (contrastLevel - (-1)) / 1); |
| 57 | + } else if (contrastLevel < 0.5) { |
| 58 | + return Lerp(normal, medium, (contrastLevel - 0) / 0.5); |
| 59 | + } else if (contrastLevel < 1.0) { |
| 60 | + return Lerp(medium, high, (contrastLevel - 0.5) / 0.5); |
| 61 | + } else { |
| 62 | + return high; |
| 63 | + } |
| 64 | + } |
| 65 | +}; |
| 66 | + |
| 67 | +} // namespace material_color_utilities |
| 68 | + |
| 69 | +#endif // CPP_DYNAMICCOLOR_CONTRAST_CURVE_H_ |
0 commit comments