aboutsummaryrefslogtreecommitdiffstats
path: root/sdk/src/java/cyanogenmod/hardware/HSIC.java
blob: df6cb5f049576848a8ee43e8d7cd6477a75222ec (plain)
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
/*
 * Copyright (C) 2016 The CyanogenMod 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 cyanogenmod.hardware;

import android.graphics.Color;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import java.util.Locale;

public class HSIC implements Parcelable {

    private final float mHue;
    private final float mSaturation;
    private final float mIntensity;
    private final float mContrast;
    private final float mSaturationThreshold;

    public HSIC(float hue, float saturation, float intensity,
                float contrast, float saturationThreshold) {
        mHue = hue;
        mSaturation = saturation;
        mIntensity = intensity;
        mContrast = contrast;
        mSaturationThreshold = saturationThreshold;
    }

    public float getHue() {
        return mHue;
    }

    public float getSaturation() {
        return mSaturation;
    }

    public float getIntensity() {
        return mIntensity;
    }

    public float getContrast() {
        return mContrast;
    }

    public float getSaturationThreshold() {
        return mSaturationThreshold;
    }

    public String flatten() {
        return String.format(Locale.US, "%f|%f|%f|%f|%f", mHue, mSaturation,
                mIntensity, mContrast, mSaturationThreshold);
    }

    public static HSIC unflattenFrom(String flat) throws NumberFormatException {
        final String[] unflat = TextUtils.split(flat, "\\|");
        if (unflat.length != 4 && unflat.length != 5) {
            throw new NumberFormatException("Failed to unflatten HSIC values: " + flat);
        }
        return new HSIC(Float.parseFloat(unflat[0]), Float.parseFloat(unflat[1]),
                Float.parseFloat(unflat[2]), Float.parseFloat(unflat[3]),
                unflat.length == 5 ? Float.parseFloat(unflat[4]) : 0.0f);
    }

    public int[] toRGB() {
        final int c = Color.HSVToColor(toFloatArray());
        return new int[] { Color.red(c), Color.green(c), Color.blue(c) };
    }

    public float[] toFloatArray() {
        return new float[] { mHue, mSaturation, mIntensity, mContrast, mSaturationThreshold };
    }

    public static HSIC fromFloatArray(float[] hsic) {
        if (hsic.length == 5) {
            return new HSIC(hsic[0], hsic[1], hsic[2], hsic[3], hsic[4]);
        } else if (hsic.length == 4) {
            return new HSIC(hsic[0], hsic[1], hsic[2], hsic[3], 0.0f);
        }
        return null;
    }

    @Override
    public String toString() {
        return String.format(Locale.US, "HSIC={ hue=%f saturation=%f intensity=%f " +
                             "contrast=%f saturationThreshold=%f }",
                mHue, mSaturation, mIntensity, mContrast, mSaturationThreshold);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeFloatArray(toFloatArray());
    }

    /**
     * @hide
     */
    public static final Parcelable.Creator<HSIC> CREATOR =
            new Parcelable.Creator<HSIC>() {
                public HSIC createFromParcel(Parcel in) {
                    float[] fromParcel = new float[5];
                    in.readFloatArray(fromParcel);
                    return HSIC.fromFloatArray(fromParcel);
                }

                @Override
                public HSIC[] newArray(int size) {
                    return new HSIC[size];
                }
            };
};