Java Code Examples for android.graphics.ColorMatrix#postConcat()

The following examples show how to use android.graphics.ColorMatrix#postConcat() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ColorPickerDialog.java    From geopaparazzi with GNU General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("unused")
private int rotateColor(int color, float rad) {
    float deg = rad * 180 / 3.1415927f;
    int r = Color.red(color);
    int g = Color.green(color);
    int b = Color.blue(color);

    ColorMatrix cm = new ColorMatrix();
    ColorMatrix tmp = new ColorMatrix();

    cm.setRGB2YUV();
    tmp.setRotate(0, deg);
    cm.postConcat(tmp);
    tmp.setYUV2RGB();
    cm.postConcat(tmp);

    final float[] a = cm.getArray();

    int ir = floatToByte(a[0] * r + a[1] * g + a[2] * b);
    int ig = floatToByte(a[5] * r + a[6] * g + a[7] * b);
    int ib = floatToByte(a[10] * r + a[11] * g + a[12] * b);

    return Color.argb(Color.alpha(color), pinToByte(ir),
            pinToByte(ig), pinToByte(ib));
}
 
Example 2
Source File: DefaultBitmapDescriptor.java    From android_packages_apps_GmsCore with Apache License 2.0 6 votes vote down vote up
/**
 * see http://groups.google.com/group/android-developers/browse_thread/thread/9e215c83c3819953
 * see http://gskinner.com/blog/archives/2007/12/colormatrix_cla.html
 */
public static void adjustHue(ColorMatrix cm, float value) {
    value = cleanValue(value, 180f) / 180f * (float) Math.PI;
    if (value == 0) {
        return;
    }
    float cosVal = (float) Math.cos(value);
    float sinVal = (float) Math.sin(value);
    float lumR = 0.213f;
    float lumG = 0.715f;
    float lumB = 0.072f;
    float[] mat = new float[]{lumR + cosVal * (1 - lumR) + sinVal * (-lumR),
            lumG + cosVal * (-lumG) + sinVal * (-lumG),
            lumB + cosVal * (-lumB) + sinVal * (1 - lumB), 0, 0,
            lumR + cosVal * (-lumR) + sinVal * (0.143f),
            lumG + cosVal * (1 - lumG) + sinVal * (0.140f),
            lumB + cosVal * (-lumB) + sinVal * (-0.283f), 0, 0,
            lumR + cosVal * (-lumR) + sinVal * (-(1 - lumR)),
            lumG + cosVal * (-lumG) + sinVal * (lumG),
            lumB + cosVal * (1 - lumB) + sinVal * (lumB),
            0, 0, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 0f, 0f, 1f};
    cm.postConcat(new ColorMatrix(mat));
}
 
Example 3
Source File: ColorMatrixActivity.java    From android-graphics-demo with Apache License 2.0 6 votes vote down vote up
private ColorMatrix getBinaryColorMatrix() {
  ColorMatrix colorMatrix = new ColorMatrix();
  colorMatrix.setSaturation(0);

  float m = 255f;
  float t = -255*128f;
  ColorMatrix threshold = new ColorMatrix(new float[] {
      m, 0, 0, 1, t,
      0, m, 0, 1, t,
      0, 0, m, 1, t,
      0, 0, 0, 1, 0
  });

  // Convert to grayscale, then threshold
  colorMatrix.postConcat(threshold);

  return colorMatrix;
}
 
Example 4
Source File: ColorPickerDialog.java    From AndroidDrawing with MIT License 6 votes vote down vote up
private int rotateColor(int color, float rad) {
    float deg = rad * 180 / 3.1415927f;
    int r = Color.red(color);
    int g = Color.green(color);
    int b = Color.blue(color);

    ColorMatrix cm = new ColorMatrix();
    ColorMatrix tmp = new ColorMatrix();

    cm.setRGB2YUV();
    tmp.setRotate(0, deg);
    cm.postConcat(tmp);
    tmp.setYUV2RGB();
    cm.postConcat(tmp);

    final float[] a = cm.getArray();

    int ir = floatToByte(a[0] * r + a[1] * g + a[2] * b);
    int ig = floatToByte(a[5] * r + a[6] * g + a[7] * b);
    int ib = floatToByte(a[10] * r + a[11] * g + a[12] * b);

    return Color.argb(Color.alpha(color), pinToByte(ir),
            pinToByte(ig), pinToByte(ib));
}
 
Example 5
Source File: ColorHelper.java    From Noyze with Apache License 2.0 6 votes vote down vote up
public static void adjustBrightness(ColorMatrix cm, float value) {
    value = cleanValue(value, 100);
    if (value == 0) {
        return;
    }

    float[] mat = new float[]
            {
                    1, 0, 0, 0, value,
                    0, 1, 0, 0, value,
                    0, 0, 1, 0, value,
                    0, 0, 0, 1, 0,
                    0, 0, 0, 0, 1
            };
    cm.postConcat(new ColorMatrix(mat));
}
 
Example 6
Source File: ColorHelper.java    From Noyze with Apache License 2.0 6 votes vote down vote up
/**
 * @param cm
 * @param value
 * @see http://groups.google.com/group/android-developers/browse_thread/thread/9e215c83c3819953
 * @see http://gskinner.com/blog/archives/2007/12/colormatrix_cla.html
 */
public static void adjustHue(ColorMatrix cm, float value) {
    value = cleanValue(value, 180f) / 180f * (float) Math.PI;
    if (value == 0) {
        return;
    }

    float cosVal = (float) Math.cos(value);
    float sinVal = (float) Math.sin(value);
    float lumR = 0.213f;
    float lumG = 0.715f;
    float lumB = 0.072f;
    float[] mat = new float[]
            {
                    lumR + cosVal * (1 - lumR) + sinVal * (-lumR), lumG + cosVal * (-lumG) + sinVal * (-lumG), lumB + cosVal * (-lumB) + sinVal * (1 - lumB), 0, 0,
                    lumR + cosVal * (-lumR) + sinVal * (0.143f), lumG + cosVal * (1 - lumG) + sinVal * (0.140f), lumB + cosVal * (-lumB) + sinVal * (-0.283f), 0, 0,
                    lumR + cosVal * (-lumR) + sinVal * (-(1 - lumR)), lumG + cosVal * (-lumG) + sinVal * (lumG), lumB + cosVal * (1 - lumB) + sinVal * (lumB), 0, 0,
                    0f, 0f, 0f, 1f, 0f,
                    0f, 0f, 0f, 0f, 1f};
    cm.postConcat(new ColorMatrix(mat));
}
 
Example 7
Source File: ColorHelper.java    From Noyze with Apache License 2.0 6 votes vote down vote up
public static void adjustSaturation(ColorMatrix cm, float value) {
    value = cleanValue(value, 100);
    if (value == 0) {
        return;
    }

    float x = 1 + ((value > 0) ? 3 * value / 100 : value / 100);
    float lumR = 0.3086f;
    float lumG = 0.6094f;
    float lumB = 0.0820f;

    float[] mat = new float[]
            {
                    lumR * (1 - x) + x, lumG * (1 - x), lumB * (1 - x), 0, 0,
                    lumR * (1 - x), lumG * (1 - x) + x, lumB * (1 - x), 0, 0,
                    lumR * (1 - x), lumG * (1 - x), lumB * (1 - x) + x, 0, 0,
                    0, 0, 0, 1, 0,
                    0, 0, 0, 0, 1
            };
    cm.postConcat(new ColorMatrix(mat));
}
 
Example 8
Source File: SampleWhackyColorFilter.java    From osmdroid with Apache License 2.0 6 votes vote down vote up
/**
 * http://groups.google.com/group/android-developers/browse_thread/thread/9e215c83c3819953
 * http://gskinner.com/blog/archives/2007/12/colormatrix_cla.html
 * @param cm
 * @param value
 */
public static void adjustHue(ColorMatrix cm, float value)
{
    value = cleanValue(value, 180f) / 180f * (float) Math.PI;
    if (value == 0)
    {
        return;
    }
    float cosVal = (float) Math.cos(value);
    float sinVal = (float) Math.sin(value);
    float lumR = 0.213f;
    float lumG = 0.715f;
    float lumB = 0.072f;
    float[] mat = new float[]
            {
                    lumR + cosVal * (1 - lumR) + sinVal * (-lumR), lumG + cosVal * (-lumG) + sinVal * (-lumG), lumB + cosVal * (-lumB) + sinVal * (1 - lumB), 0, 0,
                    lumR + cosVal * (-lumR) + sinVal * (0.143f), lumG + cosVal * (1 - lumG) + sinVal * (0.140f), lumB + cosVal * (-lumB) + sinVal * (-0.283f), 0, 0,
                    lumR + cosVal * (-lumR) + sinVal * (-(1 - lumR)), lumG + cosVal * (-lumG) + sinVal * (lumG), lumB + cosVal * (1 - lumB) + sinVal * (lumB), 0, 0,
                    0f, 0f, 0f, 1f, 0f
            };
    cm.postConcat(new ColorMatrix(mat));
}
 
Example 9
Source File: ColorHelper.java    From Noyze with Apache License 2.0 6 votes vote down vote up
/**
 * @param cm
 * @param value
 * @see http://groups.google.com/group/android-developers/browse_thread/thread/9e215c83c3819953
 * @see http://gskinner.com/blog/archives/2007/12/colormatrix_cla.html
 */
public static void adjustHue(ColorMatrix cm, float value) {
    value = cleanValue(value, 180f) / 180f * (float) Math.PI;
    if (value == 0) {
        return;
    }

    float cosVal = (float) Math.cos(value);
    float sinVal = (float) Math.sin(value);
    float lumR = 0.213f;
    float lumG = 0.715f;
    float lumB = 0.072f;
    float[] mat = new float[]
            {
                    lumR + cosVal * (1 - lumR) + sinVal * (-lumR), lumG + cosVal * (-lumG) + sinVal * (-lumG), lumB + cosVal * (-lumB) + sinVal * (1 - lumB), 0, 0,
                    lumR + cosVal * (-lumR) + sinVal * (0.143f), lumG + cosVal * (1 - lumG) + sinVal * (0.140f), lumB + cosVal * (-lumB) + sinVal * (-0.283f), 0, 0,
                    lumR + cosVal * (-lumR) + sinVal * (-(1 - lumR)), lumG + cosVal * (-lumG) + sinVal * (lumG), lumB + cosVal * (1 - lumB) + sinVal * (lumB), 0, 0,
                    0f, 0f, 0f, 1f, 0f,
                    0f, 0f, 0f, 0f, 1f};
    cm.postConcat(new ColorMatrix(mat));
}
 
Example 10
Source File: ColorFilterGenerator.java    From Alite with GNU General Public License v3.0 6 votes vote down vote up
private static void adjustSaturation(ColorMatrix cm, float value) {
	value = cleanValue(value, 100);
	if (value == 0) {
		return;
	}

	float x = 1 + ((value > 0) ? 3 * value / 100 : value / 100);
	float lumR = 0.3086f;
	float lumG = 0.6094f;
	float lumB = 0.0820f;

	float[] mat = new float[] { lumR * (1 - x) + x, lumG * (1 - x),
			lumB * (1 - x), 0, 0, lumR * (1 - x), lumG * (1 - x) + x,
			lumB * (1 - x), 0, 0, lumR * (1 - x), lumG * (1 - x),
			lumB * (1 - x) + x, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1 };
	cm.postConcat(new ColorMatrix(mat));
}
 
Example 11
Source File: ColorFilterGenerator.java    From Alite with GNU General Public License v3.0 6 votes vote down vote up
private static void adjustContrast(ColorMatrix cm, float value) {
	value = cleanValue(value, 100);
	if (value == 0) {
		return;
	}
	float x;
	if (value < 0) {
		x = 127 + value / 100 * 127;
	} else {
		x = value % 1;
		if (x == 0) {
			x = (float) DELTA_INDEX[(int)Math.floor(value)];
		} else {
			x = (float) DELTA_INDEX[(int) Math.floor(value)] * (1 - x)
					+ (float) DELTA_INDEX[(int) Math.floor(value) + 1] * x; 
		}
		x = x * 127 + 127;
	}

	float[] mat = new float[] { x / 127, 0, 0, 0, 0.5f * (127 - x), 0,
			x / 127, 0, 0, 0.5f * (127 - x), 0, 0, x / 127, 0,
			0.5f * (127 - x), 0, 0, 0, 1, 0, 0, 0, 0, 0, 1 };
	cm.postConcat(new ColorMatrix(mat));

}
 
Example 12
Source File: ColorFilterGenerator.java    From Alite with GNU General Public License v3.0 6 votes vote down vote up
/**
 * @see http
 *      ://groups.google.com/group/android-developers/browse_thread/thread
 *      /9e215c83c3819953
 * @see http://gskinner.com/blog/archives/2007/12/colormatrix_cla.html
 * @param cm
 * @param value
 */
private static void adjustHue(ColorMatrix cm, float value) {
	value = cleanValue(value, 180f) / 180f * (float) Math.PI;
	if (value == 0) {
		return;
	}

	float cosVal = (float) Math.cos(value);
	float sinVal = (float) Math.sin(value);
	float lumR = 0.213f;
	float lumG = 0.715f;
	float lumB = 0.072f;
	float[] mat = new float[] {
			lumR + cosVal * (1 - lumR) + sinVal * (-lumR),
			lumG + cosVal * (-lumG) + sinVal * (-lumG),
			lumB + cosVal * (-lumB) + sinVal * (1 - lumB), 0, 0,
			lumR + cosVal * (-lumR) + sinVal * (0.143f),
			lumG + cosVal * (1 - lumG) + sinVal * (0.140f),
			lumB + cosVal * (-lumB) + sinVal * (-0.283f), 0, 0,
			lumR + cosVal * (-lumR) + sinVal * (-(1 - lumR)),
			lumG + cosVal * (-lumG) + sinVal * (lumG),
			lumB + cosVal * (1 - lumB) + sinVal * (lumB), 0, 0, 0f, 0f, 0f,
			1f, 0f, 0f, 0f, 0f, 0f, 1f };
	cm.postConcat(new ColorMatrix(mat));
}
 
Example 13
Source File: ImageUtils.java    From AndroidBase with Apache License 2.0 6 votes vote down vote up
/**
 * 调整照片的色调,饱和度,亮度
 * MID_VALUE = 177; // 进度条的中间值
 * @param bm 原图
 * @param hue 色调 = (进度条进度 - MID_VALUE) * 1.0F / MIN_VALUE * 180
 * @param saturation 饱和度 = 进度条进度 * 1.0F / MIN_VALUE;
 * @param lum 亮度 = 进度条进度 * 1.0F / MIN_VALUE;
 * @return 调整后的图片
 */
public static Bitmap handleImageEffect(Bitmap bm, int hue, int saturation, int lum) {
    Bitmap bitmap = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

    ColorMatrix hueMatrix = new ColorMatrix();
    hueMatrix.setRotate(0,hue);
    hueMatrix.setRotate(1,hue);
    hueMatrix.setRotate(2, hue);

    ColorMatrix saturationMatrix = new ColorMatrix();
    saturationMatrix.setSaturation(saturation);

    ColorMatrix lumMatrix = new ColorMatrix();
    lumMatrix.setScale(lum,lum,lum,1);

    ColorMatrix imageMatrix = new ColorMatrix();
    imageMatrix.postConcat(hueMatrix);
    imageMatrix.postConcat(saturationMatrix);
    imageMatrix.postConcat(lumMatrix);

    paint.setColorFilter(new ColorMatrixColorFilter(imageMatrix));
    canvas.drawBitmap(bm,0,0,paint);
    return bitmap;
}
 
Example 14
Source File: ColorUtil.java    From xDrip-plus with GNU General Public License v3.0 6 votes vote down vote up
public static void adjustHue(ColorMatrix cm, float value) {
    value = cleanValue(value, 180f) / 180f * (float) Math.PI;
    if (value == 0) {
        return;
    }
    float cosVal = (float) Math.cos(value);
    float sinVal = (float) Math.sin(value);
    float lumR = 0.213f;
    float lumG = 0.715f;
    float lumB = 0.072f;
    float[] mat = new float[]
            {
                    lumR + cosVal * (1 - lumR) + sinVal * (-lumR), lumG + cosVal * (-lumG) + sinVal * (-lumG), lumB + cosVal * (-lumB) + sinVal * (1 - lumB), 0, 0,
                    lumR + cosVal * (-lumR) + sinVal * (0.143f), lumG + cosVal * (1 - lumG) + sinVal * (0.140f), lumB + cosVal * (-lumB) + sinVal * (-0.283f), 0, 0,
                    lumR + cosVal * (-lumR) + sinVal * (-(1 - lumR)), lumG + cosVal * (-lumG) + sinVal * (lumG), lumB + cosVal * (1 - lumB) + sinVal * (lumB), 0, 0,
                    0f, 0f, 0f, 1f, 0f,
                    0f, 0f, 0f, 0f, 1f};
    cm.postConcat(new ColorMatrix(mat));
}
 
Example 15
Source File: ColorUtil.java    From xDrip with GNU General Public License v3.0 6 votes vote down vote up
public static void adjustHue(ColorMatrix cm, float value) {
    value = cleanValue(value, 180f) / 180f * (float) Math.PI;
    if (value == 0) {
        return;
    }
    float cosVal = (float) Math.cos(value);
    float sinVal = (float) Math.sin(value);
    float lumR = 0.213f;
    float lumG = 0.715f;
    float lumB = 0.072f;
    float[] mat = new float[]
            {
                    lumR + cosVal * (1 - lumR) + sinVal * (-lumR), lumG + cosVal * (-lumG) + sinVal * (-lumG), lumB + cosVal * (-lumB) + sinVal * (1 - lumB), 0, 0,
                    lumR + cosVal * (-lumR) + sinVal * (0.143f), lumG + cosVal * (1 - lumG) + sinVal * (0.140f), lumB + cosVal * (-lumB) + sinVal * (-0.283f), 0, 0,
                    lumR + cosVal * (-lumR) + sinVal * (-(1 - lumR)), lumG + cosVal * (-lumG) + sinVal * (lumG), lumB + cosVal * (1 - lumB) + sinVal * (lumB), 0, 0,
                    0f, 0f, 0f, 1f, 0f,
                    0f, 0f, 0f, 0f, 1f};
    cm.postConcat(new ColorMatrix(mat));
}
 
Example 16
Source File: ColorFilterGenerator.java    From Alite with GNU General Public License v3.0 5 votes vote down vote up
private static void adjustBrightness(ColorMatrix cm, float value) {
	value = cleanValue(value, 100);
	if (value == 0) {
		return;
	}

	float[] mat = new float[] { 1, 0, 0, 0, value, 0, 1, 0, 0, value, 0, 0,
			1, 0, value, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1 };
	cm.postConcat(new ColorMatrix(mat));
}
 
Example 17
Source File: ColorHelper.java    From Noyze with Apache License 2.0 5 votes vote down vote up
public static void adjustContrast(ColorMatrix cm, int value) {
    value = (int) cleanValue(value, 100);
    if (value == 0) {
        return;
    }
    float x;
    if (value < 0) {
        x = 127 + value / 100 * 127;
    } else {
        x = value % 1;
        if (x == 0) {
            x = (float) DELTA_INDEX[value];
        } else {
            //x = DELTA_INDEX[(p_val<<0)]; // this is how the IDE does it.
            x = (float) DELTA_INDEX[(value << 0)] * (1 - x) + (float) DELTA_INDEX[(value << 0) + 1] * x; // use linear interpolation for more granularity.
        }
        x = x * 127 + 127;
    }

    float[] mat = new float[]
            {
                    x / 127, 0, 0, 0, 0.5f * (127 - x),
                    0, x / 127, 0, 0, 0.5f * (127 - x),
                    0, 0, x / 127, 0, 0.5f * (127 - x),
                    0, 0, 0, 1, 0,
                    0, 0, 0, 0, 1
            };
    cm.postConcat(new ColorMatrix(mat));

}
 
Example 18
Source File: ImageUtils.java    From Machine-Learning-Projects-for-Mobile-Applications with MIT License 5 votes vote down vote up
/**
 * Returns a sepia filtered version of src.
 *
 * @param src source image.
 * @return sepia filtered image.
 */
static Bitmap sepiaBitmap(Bitmap src) {

    ColorMatrix colorMatrix_Sepia = new ColorMatrix();
    colorMatrix_Sepia.setSaturation(0);

    ColorMatrix colorScale = new ColorMatrix();
    colorScale.setScale(1, 1, 0.8f, 1);
    colorMatrix_Sepia.postConcat(colorScale);

    ColorFilter ColorFilter_Sepia = new ColorMatrixColorFilter(colorMatrix_Sepia);
    Bitmap      bitmap            = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas      canvas            = new Canvas(bitmap);
    Paint       paint             = new Paint();

    paint.setColorFilter(ColorFilter_Sepia);
    canvas.drawBitmap(src, 0, 0, paint);

    return bitmap;
}
 
Example 19
Source File: ColorMatrixActivity.java    From android-graphics-demo with Apache License 2.0 5 votes vote down vote up
private ColorMatrix getSepiaColorMatrix() {
  ColorMatrix colorMatrix = new ColorMatrix();
  colorMatrix.setSaturation(0);

  ColorMatrix colorScale = new ColorMatrix();
  colorScale.setScale(1, 1, 0.8f, 1);

  // Convert to grayscale, then apply brown color
  colorMatrix.postConcat(colorScale);

  return colorMatrix;
}
 
Example 20
Source File: MainActivity.java    From auid2 with Apache License 2.0 4 votes vote down vote up
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        final View view = findViewById(R.id.view);
        final ImageView imageView = (ImageView) findViewById(R.id.image);

        // SimpleTextDrawable
//        view.setBackgroundDrawable(new SimpleTextDrawable("Hello World!"));

        // BetterTextDrawable
//        view.setBackgroundDrawable(new BetterTextDrawable("This is a really long string of text that will require multiple lines."));

        // SimpleImageDrawable
        final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
        final SimpleImageDrawable simpleImageDrawable = new SimpleImageDrawable(bitmap);
//        view.setBackgroundDrawable(simpleImageDrawable);


        // LightingColorFilter example
        //                AARRGGBB
        final int mul = 0xFF00FF00;
        final int add = 0x000000BB;
        final ColorFilter lightingColorFilter = new LightingColorFilter(mul, add);
//        simpleImageDrawable.setColorFilter(lightingColorFilter);

        // ColorMatrixColorFilter example
        final ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.setSaturation(0);
        final ColorMatrix colorScale = new ColorMatrix();
        colorScale.setScale(2f, .68f, .26f, 1f); // Orange
        colorMatrix.postConcat(colorScale);
//        simpleImageDrawable.setColorFilter(new ColorMatrixColorFilter(colorMatrix));

        // RoundedImageDrawable
        final Bitmap largeBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.giwajip);
        final RoundedImageDrawable roundedImageDrawable = new RoundedImageDrawable(largeBitmap, 200);
//        imageView.setImageDrawable(roundedImageDrawable);

        // FadedImageDrawable
        final FadedImageDrawable fadedImageDrawable = new FadedImageDrawable(largeBitmap);
        imageView.setImageDrawable(fadedImageDrawable);
    }