This source file includes following definitions.
- init
- createAndAddNewGradient
- setListener
- getColor
- setColor
- notifyColorChanged
- onProgressChanged
- updateHueGradient
- updateSaturationGradient
- updateValueGradient
- refreshGradientComponents
- onStartTrackingTouch
- onStopTrackingTouch
package org.chromium.ui;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class ColorPickerAdvanced extends LinearLayout implements OnSeekBarChangeListener {
private static final int HUE_SEEK_BAR_MAX = 360;
private static final int HUE_COLOR_COUNT = 7;
private static final int SATURATION_SEEK_BAR_MAX = 100;
private static final int SATURATION_COLOR_COUNT = 2;
private static final int VALUE_SEEK_BAR_MAX = 100;
private static final int VALUE_COLOR_COUNT = 2;
ColorPickerAdvancedComponent mHueDetails;
ColorPickerAdvancedComponent mSaturationDetails;
ColorPickerAdvancedComponent mValueDetails;
private OnColorChangedListener mOnColorChangedListener;
private int mCurrentColor;
private final float[] mCurrentHsvValues = new float[3];
public ColorPickerAdvanced(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ColorPickerAdvanced(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public ColorPickerAdvanced(Context context) {
super(context);
init();
}
private void init() {
setOrientation(LinearLayout.VERTICAL);
mHueDetails = createAndAddNewGradient(R.string.color_picker_hue,
HUE_SEEK_BAR_MAX, this);
mSaturationDetails = createAndAddNewGradient(R.string.color_picker_saturation,
SATURATION_SEEK_BAR_MAX, this);
mValueDetails = createAndAddNewGradient(R.string.color_picker_value,
VALUE_SEEK_BAR_MAX, this);
refreshGradientComponents();
}
public ColorPickerAdvancedComponent createAndAddNewGradient(int textResourceId,
int seekBarMax,
OnSeekBarChangeListener seekBarListener) {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View newComponent = inflater.inflate(R.layout.color_picker_advanced_component, null);
addView(newComponent);
return new ColorPickerAdvancedComponent(newComponent,
textResourceId,
seekBarMax,
seekBarListener);
}
public void setListener(OnColorChangedListener onColorChangedListener) {
mOnColorChangedListener = onColorChangedListener;
}
public int getColor() {
return mCurrentColor;
}
public void setColor(int color) {
mCurrentColor = color;
Color.colorToHSV(mCurrentColor, mCurrentHsvValues);
refreshGradientComponents();
}
private void notifyColorChanged() {
if (mOnColorChangedListener != null) {
mOnColorChangedListener.onColorChanged(getColor());
}
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
mCurrentHsvValues[0] = mHueDetails.getValue();
mCurrentHsvValues[1] = mSaturationDetails.getValue() / 100.0f;
mCurrentHsvValues[2] = mValueDetails.getValue() / 100.0f;
mCurrentColor = Color.HSVToColor(mCurrentHsvValues);
updateHueGradient();
updateSaturationGradient();
updateValueGradient();
notifyColorChanged();
}
}
private void updateHueGradient() {
float[] tempHsvValues = new float[3];
tempHsvValues[1] = mCurrentHsvValues[1];
tempHsvValues[2] = mCurrentHsvValues[2];
int[] newColors = new int[HUE_COLOR_COUNT];
for (int i = 0; i < HUE_COLOR_COUNT; ++i) {
tempHsvValues[0] = i * 60.0f;
newColors[i] = Color.HSVToColor(tempHsvValues);
}
mHueDetails.setGradientColors(newColors);
}
private void updateSaturationGradient() {
float[] tempHsvValues = new float[3];
tempHsvValues[0] = mCurrentHsvValues[0];
tempHsvValues[1] = 0.0f;
tempHsvValues[2] = mCurrentHsvValues[2];
int[] newColors = new int[SATURATION_COLOR_COUNT];
newColors[0] = Color.HSVToColor(tempHsvValues);
tempHsvValues[1] = 1.0f;
newColors[1] = Color.HSVToColor(tempHsvValues);
mSaturationDetails.setGradientColors(newColors);
}
private void updateValueGradient() {
float[] tempHsvValues = new float[3];
tempHsvValues[0] = mCurrentHsvValues[0];
tempHsvValues[1] = mCurrentHsvValues[1];
tempHsvValues[2] = 0.0f;
int[] newColors = new int[VALUE_COLOR_COUNT];
newColors[0] = Color.HSVToColor(tempHsvValues);
tempHsvValues[2] = 1.0f;
newColors[1] = Color.HSVToColor(tempHsvValues);
mValueDetails.setGradientColors(newColors);
}
private void refreshGradientComponents() {
int saturationValue = Math.round(mCurrentHsvValues[1] * 100.0f);
saturationValue = Math.min(saturationValue, SATURATION_SEEK_BAR_MAX);
saturationValue = Math.max(saturationValue, 0);
int valueValue = Math.round(mCurrentHsvValues[2] * 100.0f);
valueValue = Math.min(valueValue, VALUE_SEEK_BAR_MAX);
valueValue = Math.max(valueValue, 0);
mHueDetails.setValue(mCurrentHsvValues[0]);
mSaturationDetails.setValue(saturationValue);
mValueDetails.setValue(valueValue);
updateHueGradient();
updateSaturationGradient();
updateValueGradient();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}