Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invert colors of drawable

Is it possible to "invert" colors in a Drawable? Kind of what you have in a negative, you know?

I know it's possible to change a Drawable to black and white, but what about inverting colors?

like image 396
Carlos Pereira Avatar asked Jul 24 '13 18:07

Carlos Pereira


People also ask

How can I invert colors on a picture?

Invert the image by pressing the shortcut key Ctrl + I .

How do you invert colors in preview?

Steps: select all, right click, invert color. Preview (Mac): If you're using a Mac, then preview makes inverting colors a breeze. Steps: In the menu, follow tools --> adjust color… . Reverse the dark and light sliders in the adjustment window that opens (as shown in the image above).

How do you invert colors on Chrome?

From the Advanced menu on the left, select Accessibility > Manage accessibility features. Scroll to the Display section of the Accessibility window, and click Use high contrast mode to toggle invert screen colors. To turn it off, click the toggle again to switch back to its original position.


2 Answers

After some research I found out that solution is much simpler than I thought.

Here it is:

  /**    * Color matrix that flips the components (<code>-1.0f * c + 255 = 255 - c</code>)    * and keeps the alpha intact.    */   private static final float[] NEGATIVE = {      -1.0f,     0,     0,    0, 255, // red         0, -1.0f,     0,    0, 255, // green         0,     0, -1.0f,    0, 255, // blue         0,     0,     0, 1.0f,   0  // alpha     };    drawable.setColorFilter(new ColorMatrixColorFilter(NEGATIVE)); 
like image 55
Carlos Pereira Avatar answered Oct 02 '22 13:10

Carlos Pereira


Best way to do this would be to convert your drawable into a bitmap:

Bitmap fromDrawable = BitmapFactory.decodeResource(context.getResources(),R.drawable.drawable_resource); 

And then invert it as per:

https://stackoverflow.com/a/4625618/1154026

And then back to a drawable if you need to:

Drawable invertedDrawable = new BitmapDrawable(getResources(),fromDrawable); 
like image 23
VicVu Avatar answered Oct 02 '22 12:10

VicVu