下面列出了android.graphics.Color#colorToHSV ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public Integer evaluate(float fraction, Integer startValue, Integer endValue)
{
float[] startHsv = new float[3];
float[] endHsv = new float[3];
float[] currentHsv = new float[3];
Color.colorToHSV(startValue, startHsv);
Color.colorToHSV(endValue, endHsv);
for (int i=0; i<3; i++)
currentHsv[i] = (1-fraction)*startHsv[i] + fraction*endHsv[i];
while (currentHsv[0]>=360.0f) currentHsv[0] -= 360.0f;
while (currentHsv[0]<0.0f) currentHsv[0] += 360.0f;
return Color.HSVToColor(currentHsv);
}
/**
* Set the bar color. <br>
* <br>
* Its discouraged to use this method.
*
* @param color
*/
public void setColor(int color) {
int x1, y1;
if(mOrientation == ORIENTATION_HORIZONTAL) {
x1 = (mBarLength + mBarPointerHaloRadius);
y1 = mBarThickness;
}
else {
x1 = mBarThickness;
y1 = (mBarLength + mBarPointerHaloRadius);
}
Color.colorToHSV(color, mHSVColor);
shader = new LinearGradient(mBarPointerHaloRadius, 0,
x1, y1, new int[] {
Color.HSVToColor(0x00, mHSVColor), color }, null,
Shader.TileMode.CLAMP);
mBarPaint.setShader(shader);
calculateColor(mBarPointerPosition);
mBarPointerPaint.setColor(mColor);
if (mPicker != null) {
mPicker.setNewCenterColor(mColor);
}
invalidate();
}
private ColorCircle findNearestByColor(int color) {
float[] hsv = new float[3];
Color.colorToHSV(color, hsv);
ColorCircle near = null;
double minDiff = Double.MAX_VALUE;
double x = hsv[1] * Math.cos(hsv[0] * Math.PI / 180);
double y = hsv[1] * Math.sin(hsv[0] * Math.PI / 180);
for (ColorCircle colorCircle : renderer.getColorCircleList()) {
float[] hsv1 = colorCircle.getHsv();
double x1 = hsv1[1] * Math.cos(hsv1[0] * Math.PI / 180);
double y1 = hsv1[1] * Math.sin(hsv1[0] * Math.PI / 180);
double dx = x - x1;
double dy = y - y1;
double dist = dx * dx + dy * dy;
if (dist < minDiff) {
minDiff = dist;
near = colorCircle;
}
}
return near;
}
/**Gets a Rectangular Bitmap representing the Gradient of Sat and Val copied from the given BitmapDrawable for the given Hue
* @param bitmapDrawable A BitmapDrawable to use as a base for the gradient of Sat and Val
* @param hue Value of Hue to use for the bitmap generation of SatVal Gradient bitmap
* @param width Width of the SatValPicker
* @param height Height of the SatValPicker
* @param skipCount Number of pixels to skip when generating Bitmap (increasing this results in faster bitmap generation but reduces bitmap quality)
* @return A Rectangular Bitmap representing the Gradient of Sat and Val copied from the given BitmapDrawable for the given Hue
*/
@Deprecated
public static Bitmap getSatValBitmapFrom(BitmapDrawable bitmapDrawable,float hue, int width, int height, int skipCount){
int[] pixels=new int[width*height];
Bitmap bitmap=bitmapDrawable.getBitmap();
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
for(int i=0;i<pixels.length;i++){
float[] hsv=new float[3];
Color.colorToHSV(pixels[i],hsv);
hsv[0]=hue;
pixels[i]=Color.HSVToColor(hsv);
}
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
/**
* Set the bar color. <br>
* <br>
* Its discouraged to use this method.
*
* @param color
*/
public void setColor(int color) {
int x1, y1;
if(mOrientation) {
x1 = (mBarLength + mBarPointerHaloRadius);
y1 = mBarThickness;
} else {
x1 = mBarThickness;
y1 = (mBarLength + mBarPointerHaloRadius);
}
Color.colorToHSV(color, mHSVColor);
shader = new LinearGradient(mBarPointerHaloRadius, 0,
x1, y1, new int[] {Color.WHITE, color, Color.BLACK}, null,
Shader.TileMode.CLAMP);
mBarPaint.setShader(shader);
calculateColor(mBarPointerPosition);
mBarPointerPaint.setColor(mColor);
if (mPicker != null) {
mPicker.setNewCenterColor(mColor);
if(mPicker.hasOpacityBar())
mPicker.changeOpacityBarColor(mColor);
}
invalidate();
}
/**
* Given a color returns two new colors which can be used as start and end colors of a gradient. The two colors are
* darkened and lightened version of the original color. The first is darkened, the second lightened.
*
* @param color A given color.
* @return An array of two colors.
*/
public static int[] colorSelectionButtonBackgroundGradient(int color) {
int[] gradientColors = new int[2];
float[] hsv = new float[3];
// darken
Color.colorToHSV(color, hsv);
hsv[2] *= DARKEN_LIGHTEN_FACTOR;
gradientColors[0] = Color.HSVToColor(hsv);
// lighten
Color.colorToHSV(color, hsv);
hsv[2] = 1 - DARKEN_LIGHTEN_FACTOR * (1 - hsv[2]);
gradientColors[1] = Color.HSVToColor(hsv);
return gradientColors;
}
private void applyBackgroundColorResource() {
mBackgroundColorResId = SkinCompatHelper.checkResourceId(mBackgroundColorResId);
mThemeColorBackgroundResId = SkinCompatHelper.checkResourceId(mThemeColorBackgroundResId);
ColorStateList backgroundColor;
if (mBackgroundColorResId != INVALID_ID) {
backgroundColor = SkinCompatResources.getColorStateList(getContext(), mBackgroundColorResId);
setCardBackgroundColor(backgroundColor);
} else if (mThemeColorBackgroundResId != INVALID_ID) {
int themeColorBackground = SkinCompatResources.getColor(getContext(), mThemeColorBackgroundResId);
final float[] hsv = new float[3];
Color.colorToHSV(themeColorBackground, hsv);
backgroundColor = ColorStateList.valueOf(hsv[2] > 0.5f
? getResources().getColor(R.color.cardview_light_background)
: getResources().getColor(R.color.cardview_dark_background));
setCardBackgroundColor(backgroundColor);
}
}
@Override
public Integer evaluate(float fraction, Integer startValue, Integer endValue) {
float[] startHsv = new float[3];
float[] endHsv = new float[3];
float[] currentHsv = new float[3];
Color.colorToHSV(startValue, startHsv);
Color.colorToHSV(endValue, endHsv);
for (int i = 0; i < 3; i++) {
currentHsv[i] = (1 - fraction) * startHsv[i] + fraction * endHsv[i];
}
while (currentHsv[0] >= 360.0f) {
currentHsv[0] -= 360.0f;
}
while (currentHsv[0] < 0.0f) {
currentHsv[0] += 360.0f;
}
return Color.HSVToColor(currentHsv);
}
private void newColor(int color){
this.color = color;
// Sets action bar colors
if (getSupportActionBar() == null) return;
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(0xFF000000 | color));
boolean dark = Color.red(color) * 0.299 + Color.green(color) * 0.587 + Color.blue(color) * 0.114 < 180;
SpannableString s = new SpannableString(getSupportActionBar().getTitle());
s.setSpan(new ForegroundColorSpan(dark ? Color.WHITE : Color.BLACK), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
getSupportActionBar().setTitle(s);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
float[] hsv = new float[3];
Color.colorToHSV(color, hsv);
hsv[2] *= 0.75;
getWindow().setStatusBarColor(Color.HSVToColor(hsv));
}
}
@Override
protected Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
Bundle state = new Bundle();
state.putParcelable(STATE_PARENT, superState);
state.putFloatArray(STATE_COLOR, mHSVColor);
float[] hsvColor = new float[3];
Color.colorToHSV(mColor, hsvColor);
state.putFloat(STATE_VALUE, hsvColor[2]);
state.putBoolean(STATE_ORIENTATION, ORIENTATION_HORIZONTAL);
return state;
}
@Override
protected void drawBar(Canvas barCanvas) {
int width = barCanvas.getWidth();
int height = barCanvas.getHeight();
float[] hsv = new float[3];
Color.colorToHSV(color, hsv);
int l = Math.max(2, width / 256);
for (int x = 0; x <= width; x += l) {
hsv[2] = (float) x / (width - 1);
barPaint.setColor(Color.HSVToColor(hsv));
barCanvas.drawRect(x, 0, x + l, height, barPaint);
}
}
@Override
public Integer evaluate(float fraction, Integer startValue, Integer endValue) {
// 把 ARGB 转换成 HSV
Color.colorToHSV(startValue, startHsv);
Color.colorToHSV(endValue, endHsv);
// 计算当前动画完成度(fraction)所对应的颜色值
if (endHsv[0] - startHsv[0] > 180) {
endHsv[0] -= 360;
} else if (endHsv[0] - startHsv[0] < -180) {
endHsv[0] += 360;
}
outHsv[0] = startHsv[0] + (endHsv[0] - startHsv[0]) * fraction;
if (outHsv[0] > 360) {
outHsv[0] -= 360;
} else if (outHsv[0] < 0) {
outHsv[0] += 360;
}
outHsv[1] = startHsv[1] + (endHsv[1] - startHsv[1]) * fraction;
outHsv[2] = startHsv[2] + (endHsv[2] - startHsv[2]) * fraction;
// 计算当前动画完成度(fraction)所对应的透明度
int alpha = startValue >> 24 + (int) ((endValue >> 24 - startValue >> 24) * fraction);
// 把 HSV 转换回 ARGB 返回
return Color.HSVToColor(alpha, outHsv);
}
private int generateGradientColors(int color) {
float[] hsv = new float[3];
Color.colorToHSV(color, hsv);
if (hsv[1] > 0.5f) {
hsv[1] -= 0.15f;
} else {
hsv[1] += 0.15f;
}
if (hsv[0] > 180) {
hsv[0] -= 20;
} else {
hsv[0] += 20;
}
return Color.HSVToColor(255, hsv);
}
/**
* Used for altering color in touch up/down events
*
* @param color color
* @return adjusted color
*/
private int adjustColor(int color) {
if (isActivated()) {
final double valueShift = 0.07;
float[] hsbVals = new float[3];
Color.colorToHSV(color, hsbVals);
hsbVals[2] += valueShift;
color = Color.HSVToColor(hsbVals);
}
return color;
}
public static int getDarkerColor(int color) {
float[] hsv = new float[3];
Color.colorToHSV(color, hsv);
hsv[2] *= 0.8f;
color = Color.HSVToColor(hsv);
return color;
}
/**
* Use HSV to calculate the color transition.
* @param startColor startColor
* @param endColor endColor
* @param fraction fraction
* @return int current color.
*/
private int currentColorDuringTransition(int startColor, int endColor, float fraction){
final float[] from = new float[3], to = new float[3];
//fetch startColor
Color.colorToHSV(startColor, from);
//fetch endColor
Color.colorToHSV(endColor, to);
final float[] hsv = new float[3];
hsv[0] = from[0] + (to[0] - from[0]) * fraction;
hsv[1] = from[1] + (to[1] - from[1]) * fraction;
hsv[2] = from[2] + (to[2] - from[2]) * fraction;
return Color.HSVToColor(hsv);
}
private int darken(int color) {
float[] hsv = new float[3];
Color.colorToHSV(color, hsv);
hsv[2] *= 0.85f;
color = Color.HSVToColor(hsv);
return color;
}
public void setColor(int color) {
Color.colorToHSV(color, colorHSV);
}
public static int darkColor(int color) {
float[] hsv = new float[3];
Color.colorToHSV(color, hsv);
hsv[2] *= 0.8f;
return Color.HSVToColor(hsv);
}
/**
* Returns bytes to print a bitmap in black-and-white. Any pixel on the bitmap that is not
* transparent or white will be rendered as black on the printer. The bitmaps should not be
* larger than 384 pixels.
*
* @param bitmap The bitmap to be printed.
* @return Command to execute.
*/
public static byte[] commandPrintBitmap(@NonNull Bitmap bitmap) {
final int BAND_HEIGHT = 24;
int width = bitmap.getWidth();
int height = bitmap.getHeight();
ByteBuffer buffer = ByteBuffer.allocateDirect(width * 3 * (height / BAND_HEIGHT) * 10 + 3);
// Send control bytes in big endian order.
final byte[] controlByte = {(byte) (0x00ff & width), (byte) ((0xff00 & width) >> 8)};
int[] pixels = new int[width * height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
// Bands of pixels are sent that are 8 pixels high. Iterate through bitmap
// 24 rows of pixels at a time, capturing bytes representing vertical slices 1 pixel wide.
// Each bit indicates if the pixel at that position in the slice should be dark or not.
boolean[] isDark = new boolean[3];
byte[] bandBytes = new byte[3];
int[] pixelSlice = new int[3];
float[] pixelSliceHsv = new float[3];
for (int row = 0; row < height - 8; row += BAND_HEIGHT) {
buffer.put(BITMAP_SET_LINE_SPACE_24);
// Need to send these two sets of bytes at the beginning of each row.
buffer.put(BITMAP_SELECT_BIT_IMAGE_MODE);
buffer.put(controlByte);
// Columns, unlike rows, are one at a time.
for (int col = 0; col < width; col++) {
// Reset the values of bandBytes for a new column
bandBytes[0] = 0;
bandBytes[1] = 0;
bandBytes[2] = 0;
// For each starting row/col position, evaluate each pixel in a column, or "band",
// 24 pixels high. Convert into 3 bytes.
for (int rowOffset = 0; rowOffset < 8; rowOffset++) {
// Because the printer only maintains correct height/width ratio
// at the highest density, where it takes 24 bit-deep slices, process
// a 24-bit-deep slice as 3 bytes.
int pixel2Row = row + rowOffset + 8;
int pixel3Row = row + rowOffset + 16;
// If we go past the bottom of the image, just send white pixels so the printer
// doesn't do anything. Everything still needs to be sent in sets of 3 rows.
pixelSlice[0] = bitmap.getPixel(col, row + rowOffset);
pixelSlice[1] = (pixel2Row >= bitmap.getHeight()) ?
Color.TRANSPARENT : bitmap.getPixel(col, pixel2Row);
pixelSlice[2] = (pixel3Row >= bitmap.getHeight()) ?
Color.TRANSPARENT : bitmap.getPixel(col, pixel3Row);
for (int slice = 0; slice < 3; slice++) {
Color.colorToHSV(pixelSlice[slice], pixelSliceHsv);
isDark[slice] = pixelSliceHsv[2] < 25; // Hsv[2] -> Value should be 10% dark
if (Color.alpha(pixelSlice[slice]) < 25) {
isDark[slice] = false;
}
if (isDark[slice]) {
bandBytes[slice] |= 1 << (7 - rowOffset);
}
}
}
// Write row's pixel data
buffer.put(bandBytes);
}
// Finished row
buffer.put(commandFeedLines((byte) 1));
}
// Finish image
buffer.put(commandFeedLines((byte) 1));
return buffer.array();
}