diff --git a/api/test-current.txt b/api/test-current.txt index 71f02b9bdc9a6..f8f49f832b138 100644 --- a/api/test-current.txt +++ b/api/test-current.txt @@ -509,6 +509,10 @@ package android.graphics { method public static android.graphics.ImageDecoder.Source createSource(android.content.res.Resources, java.io.InputStream, int); } + public class Paint { + method public void setColor(long); + } + } package android.graphics.drawable { diff --git a/core/jni/android/graphics/Paint.cpp b/core/jni/android/graphics/Paint.cpp index e97c9bc911dd4..b4174922aa6cb 100644 --- a/core/jni/android/graphics/Paint.cpp +++ b/core/jni/android/graphics/Paint.cpp @@ -557,6 +557,18 @@ namespace PaintGlue { return result; } + // FIXME: Should this be FastNative? + static void setColorLong(JNIEnv* env, jobject clazz, jlong paintHandle, jobject jColorSpace, + jfloat r, jfloat g, jfloat b, jfloat a) { + sk_sp cs = GraphicsJNI::getNativeColorSpace(env, jColorSpace); + if (GraphicsJNI::hasException(env)) { + return; + } + + SkColor4f color = SkColor4f{r, g, b, a}; + reinterpret_cast(paintHandle)->setColor4f(color, cs.get()); + } + // ------------------ @FastNative --------------------------- static jint setTextLocales(JNIEnv* env, jobject clazz, jlong objHandle, jstring locales) { @@ -1075,6 +1087,7 @@ static const JNINativeMethod methods[] = { {"nGetRunAdvance", "(J[CIIIIZI)F", (void*) PaintGlue::getRunAdvance___CIIIIZI_F}, {"nGetOffsetForAdvance", "(J[CIIIIZF)I", (void*) PaintGlue::getOffsetForAdvance___CIIIIZF_I}, + {"nSetColor","(JLandroid/graphics/ColorSpace;FFFF)V", (void*) PaintGlue::setColorLong}, // --------------- @FastNative ---------------------- diff --git a/graphics/java/android/graphics/Paint.java b/graphics/java/android/graphics/Paint.java index 3342fd22caab7..154c1b833b7a1 100644 --- a/graphics/java/android/graphics/Paint.java +++ b/graphics/java/android/graphics/Paint.java @@ -17,12 +17,14 @@ package android.graphics; import android.annotation.ColorInt; +import android.annotation.ColorLong; import android.annotation.IntDef; import android.annotation.IntRange; import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.Px; import android.annotation.Size; +import android.annotation.TestApi; import android.annotation.UnsupportedAppUsage; import android.graphics.fonts.FontVariationAxis; import android.os.Build; @@ -971,6 +973,31 @@ public class Paint { nSetColor(mNativePaint, color); } + /** + * Set the paint's color with a {@link ColorLong}. Note that the color is + * a long with an encoded {@link ColorSpace} as well as alpha and r,g,b. + * These values are not premultiplied, meaning that alpha can be any value, + * regardless of the values of r,g,b. See the {@link Color} class for more + * details. + * + * @param color The new color (including alpha and {@link ColorSpace}) + * to set in the paint. + * @throws IllegalArgumentException if the color space encoded in the long + * is invalid or unknown. + * + * @hide pending API approval + */ + @TestApi + public void setColor(@ColorLong long color) { + ColorSpace cs = Color.colorSpace(color); + float r = Color.red(color); + float g = Color.green(color); + float b = Color.blue(color); + float a = Color.alpha(color); + + nSetColor(mNativePaint, cs, r, g, b, a); + } + /** * Helper to getColor() that just returns the color's alpha value. This is * the same as calling getColor() >>> 24. It always returns a value between @@ -2906,6 +2933,8 @@ public class Paint { int contextStart, int contextEnd, boolean isRtl, int offset); private static native int nGetOffsetForAdvance(long paintPtr, char[] text, int start, int end, int contextStart, int contextEnd, boolean isRtl, float advance); + private static native void nSetColor(long paintPtr, ColorSpace cs, + float r, float g, float b, float a); // ---------------- @FastNative ------------------------