(Bitmap bitmap, float x, float y)
| 803 | |
| 804 | // bilinear interpolation |
| 805 | public static int getColor(Bitmap bitmap, float x, float y) |
| 806 | { |
| 807 | assert (0 <= x && x < bitmap.getWidth()); |
| 808 | assert (0 <= y && y < bitmap.getHeight()); |
| 809 | // if(color == null || color.length < 3) |
| 810 | // throw new IllegalArgumentException("color should be RGB"); |
| 811 | |
| 812 | int x0 = (int) x; |
| 813 | int y0 = (int) y; |
| 814 | |
| 815 | int c00 = bitmap.getPixel(x0, y0); |
| 816 | int c01 = bitmap.getPixel(x0, y0 + 1); |
| 817 | int c10 = bitmap.getPixel(x0 + 1, y0); |
| 818 | int c11 = bitmap.getPixel(x0 + 1, y0 + 1); |
| 819 | |
| 820 | final float wx = x - x0, one_minus_wx = 1.0f - wx; |
| 821 | final float wy = y - y0, one_minus_wy = 1.0f - wy; |
| 822 | |
| 823 | float r0 = Color.red(c00) * wx + Color.red(c01) * one_minus_wx; |
| 824 | float r1 = Color.red(c10) * wx + Color.red(c11) * one_minus_wx; |
| 825 | float r = r0 * wy + r1 * one_minus_wy; |
| 826 | |
| 827 | float g0 = Color.green(c00) * wx + Color.green(c01) * one_minus_wx; |
| 828 | float g1 = Color.green(c10) * wx + Color.green(c11) * one_minus_wx; |
| 829 | float g = g0 * wy + g1 * one_minus_wy; |
| 830 | |
| 831 | float b0 = Color.blue(c00) * wx + Color.blue(c01) * one_minus_wx; |
| 832 | float b1 = Color.blue(c10) * wx + Color.blue(c11) * one_minus_wx; |
| 833 | float b = b0 * wy + b1 * one_minus_wy; |
| 834 | |
| 835 | int red = Math.round(r), green = Math.round(g), blue = Math.round(b); |
| 836 | if(bitmap.getConfig() == Bitmap.Config.RGB_565) |
| 837 | return Color.rgb(red, green, blue); |
| 838 | |
| 839 | float a0 = Color.alpha(c00) * wx + Color.alpha(c01) * one_minus_wx; |
| 840 | float a1 = Color.alpha(c10) * wx + Color.alpha(c11) * one_minus_wx; |
| 841 | float a = a0 * wy + a1 * one_minus_wy; |
| 842 | |
| 843 | int alpha = Math.round(a); |
| 844 | return Color.argb(red, green, blue, alpha); |
| 845 | } |
| 846 | |
| 847 | /** |
| 848 | * http://stackoverflow.com/questions/3035692/how-to-convert-a-drawable-to-a-bitmap |
nothing calls this directly
no outgoing calls
no test coverage detected