Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit aefa03dd authored by Leon Scroggins III's avatar Leon Scroggins III
Browse files

Add Paint#setColor(@ColorLong)

Bug: 120904891
Test: Iba796744a9cc3652a6bc1881a9b229403ffaed31
Change-Id: Ib03212b27bc330e61b876a4be3d45c34ed551540
parent cad512fc
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -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 {
+13 −0
Original line number Diff line number Diff line
@@ -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<SkColorSpace> cs = GraphicsJNI::getNativeColorSpace(env, jColorSpace);
        if (GraphicsJNI::hasException(env)) {
            return;
        }

        SkColor4f color = SkColor4f{r, g, b, a};
        reinterpret_cast<Paint*>(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 ----------------------

+29 −0
Original line number Diff line number Diff line
@@ -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 ------------------------