/
HarmonizedColorsOptions.java
135 lines (118 loc) · 4.57 KB
/
HarmonizedColorsOptions.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.material.color;
import com.google.android.material.R;
import androidx.annotation.AttrRes;
import androidx.annotation.ColorRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StyleRes;
/**
* Wrapper class for specifying harmonization options, whether to harmonize an array of color
* resources, or a {@link HarmonizedColorAttributes}, along with the color attribute provided to
* harmonize with.
*/
public class HarmonizedColorsOptions {
@NonNull @ColorRes private final int[] colorResourceIds;
@Nullable private final HarmonizedColorAttributes colorAttributes;
@AttrRes private final int colorAttributeToHarmonizeWith;
/**
* Create HarmonizedColorsOptions with Material default, with Error colors being harmonized with
* Primary.
*/
@NonNull
public static HarmonizedColorsOptions createMaterialDefaults() {
return new HarmonizedColorsOptions.Builder()
.setColorAttributes(HarmonizedColorAttributes.createMaterialDefaults())
.build();
}
private HarmonizedColorsOptions(Builder builder) {
this.colorResourceIds = builder.colorResourceIds;
this.colorAttributes = builder.colorAttributes;
this.colorAttributeToHarmonizeWith = builder.colorAttributeToHarmonizeWith;
}
/** Returns the array of color resource ids that needs to be harmonized. */
@NonNull
@ColorRes
public int[] getColorResourceIds() {
return colorResourceIds;
}
/** Returns the {@link HarmonizedColorAttributes} that needs to be harmonized. */
@Nullable
public HarmonizedColorAttributes getColorAttributes() {
return colorAttributes;
}
/**
* Returns the color attribute to harmonize color resources and {@link HarmonizedColorAttributes}
* with.
*/
@AttrRes
public int getColorAttributeToHarmonizeWith() {
return colorAttributeToHarmonizeWith;
}
/** Builder class for specifying options when harmonizing colors. */
public static class Builder {
@NonNull @ColorRes private int[] colorResourceIds = new int[] {};
@Nullable private HarmonizedColorAttributes colorAttributes;
@AttrRes private int colorAttributeToHarmonizeWith = R.attr.colorPrimary;
/**
* Sets the array of color resource ids for harmonization.
*
* @param colorResourceIds The array of color resource ids that needs to be harmonized.
*/
@NonNull
public Builder setColorResourceIds(@NonNull @ColorRes int[] colorResourceIds) {
this.colorResourceIds = colorResourceIds;
return this;
}
/**
* Sets the harmonized color attributes for harmonization.
*
* <p>This method will look up the color resource the attribute points to, and harmonizing the
* color resource directly. If you are looking to harmonize only color resources, in most cases
* when constructing {@link HarmonizedColorsOptions},
* @see #setColorResourceIds(int[]) should be enough.
*
* @param colorAttributes The {@link HarmonizedColorAttributes} that needs to be harmonized.
*/
@NonNull
public Builder setColorAttributes(@Nullable HarmonizedColorAttributes colorAttributes) {
this.colorAttributes = colorAttributes;
return this;
}
/**
* Sets the color attribute to harmonize with.
*
* @param colorAttributeToHarmonizeWith The color attribute provided to harmonize color
* resources and {@link HarmonizedColorAttributes} with.
*/
@NonNull
public Builder setColorAttributeToHarmonizeWith(@AttrRes int colorAttributeToHarmonizeWith) {
this.colorAttributeToHarmonizeWith = colorAttributeToHarmonizeWith;
return this;
}
@NonNull
public HarmonizedColorsOptions build() {
return new HarmonizedColorsOptions(this);
}
}
@StyleRes
int getThemeOverlayResourceId(@StyleRes int defaultThemeOverlay) {
return (colorAttributes != null && colorAttributes.getThemeOverlay() != 0)
? colorAttributes.getThemeOverlay()
: defaultThemeOverlay;
}
}