1 /*
2  * Copyright (C) 2006 The Android Open Source Project
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 android.text.style;
18 
19 import android.annotation.ColorInt;
20 import android.annotation.NonNull;
21 import android.os.Parcel;
22 import android.text.ParcelableSpan;
23 import android.text.TextPaint;
24 import android.text.TextUtils;
25 
26 /**
27  * Changes the color of the text to which the span is attached.
28  * <p>
29  * For example, to set a green text color you would create a {@link
30  * android.text.SpannableString} based on the text and set the span.
31  * <pre>{@code
32  * SpannableString string = new SpannableString("Text with a foreground color span");
33  *string.setSpan(new ForegroundColorSpan(color), 12, 28, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);}</pre>
34  * <img src="{@docRoot}reference/android/images/text/style/foregroundcolorspan.png" />
35  * <figcaption>Set a text color.</figcaption>
36  */
37 public class ForegroundColorSpan extends CharacterStyle
38         implements UpdateAppearance, ParcelableSpan {
39 
40     private final int mColor;
41 
42     /**
43      * Creates a {@link ForegroundColorSpan} from a color integer.
44      * <p>
45      * To get the color integer associated with a particular color resource ID, use
46      * {@link android.content.res.Resources#getColor(int, Resources.Theme)}
47      *
48      * @param color color integer that defines the text color
49      */
ForegroundColorSpan(@olorInt int color)50     public ForegroundColorSpan(@ColorInt int color) {
51         mColor = color;
52     }
53 
54     /**
55      * Creates a {@link ForegroundColorSpan} from a parcel.
56      */
ForegroundColorSpan(@onNull Parcel src)57     public ForegroundColorSpan(@NonNull Parcel src) {
58         mColor = src.readInt();
59     }
60 
61     @Override
getSpanTypeId()62     public int getSpanTypeId() {
63         return getSpanTypeIdInternal();
64     }
65 
66     /** @hide */
67     @Override
getSpanTypeIdInternal()68     public int getSpanTypeIdInternal() {
69         return TextUtils.FOREGROUND_COLOR_SPAN;
70     }
71 
72     @Override
describeContents()73     public int describeContents() {
74         return 0;
75     }
76 
77     @Override
writeToParcel(@onNull Parcel dest, int flags)78     public void writeToParcel(@NonNull Parcel dest, int flags) {
79         writeToParcelInternal(dest, flags);
80     }
81 
82     /** @hide */
83     @Override
writeToParcelInternal(@onNull Parcel dest, int flags)84     public void writeToParcelInternal(@NonNull Parcel dest, int flags) {
85         dest.writeInt(mColor);
86     }
87 
88     /**
89      * @return the foreground color of this span.
90      * @see ForegroundColorSpan#ForegroundColorSpan(int)
91      */
92     @ColorInt
getForegroundColor()93     public int getForegroundColor() {
94         return mColor;
95     }
96 
97     /**
98      * Updates the color of the TextPaint to the foreground color.
99      */
100     @Override
updateDrawState(@onNull TextPaint textPaint)101     public void updateDrawState(@NonNull TextPaint textPaint) {
102         textPaint.setColor(mColor);
103     }
104 }
105