xref: /aosp_15_r20/frameworks/base/libs/hwui/jni/android_graphics_Color.cpp (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "GraphicsJNI.h"
18 
19 #include "SkColor.h"
20 
21 using namespace android;
22 
Color_RGBToHSV(JNIEnv * env,jobject,jint red,jint green,jint blue,jfloatArray hsvArray)23 static void Color_RGBToHSV(JNIEnv* env, jobject, jint red, jint green, jint blue,
24                            jfloatArray hsvArray)
25 {
26     SkScalar hsv[3];
27     SkRGBToHSV(red, green, blue, hsv);
28 
29     AutoJavaFloatArray  autoHSV(env, hsvArray, 3);
30     float* values = autoHSV.ptr();
31     for (int i = 0; i < 3; i++) {
32         values[i] = SkScalarToFloat(hsv[i]);
33     }
34 }
35 
Color_HSVToColor(JNIEnv * env,jobject,jint alpha,jfloatArray hsvArray)36 static jint Color_HSVToColor(JNIEnv* env, jobject, jint alpha, jfloatArray hsvArray)
37 {
38     AutoJavaFloatArray  autoHSV(env, hsvArray, 3);
39     SkScalar* hsv = autoHSV.ptr();
40     return static_cast<jint>(SkHSVToColor(alpha, hsv));
41 }
42 
43 static const JNINativeMethod gColorMethods[] = {
44     { "nativeRGBToHSV",    "(III[F)V", (void*)Color_RGBToHSV   },
45     { "nativeHSVToColor",  "(I[F)I",   (void*)Color_HSVToColor }
46 };
47 
48 namespace android {
49 
register_android_graphics_Color(JNIEnv * env)50 int register_android_graphics_Color(JNIEnv* env) {
51     return android::RegisterMethodsOrDie(env, "android/graphics/Color", gColorMethods,
52                                          NELEM(gColorMethods));
53 }
54 
55 }; // namespace android
56