package com.gordonlu.colorutilities; import android.app.Activity; import android.content.Context; import com.google.appinventor.components.annotations.*; import com.google.appinventor.components.common.ComponentCategory; import com.google.appinventor.components.runtime.AndroidNonvisibleComponent; import com.google.appinventor.components.runtime.ComponentContainer; import com.google.appinventor.components.runtime.EventDispatcher; import androidx.core.graphics.ColorUtils; import android.graphics.Color; @DesignerComponent( version = 1, description = "A non-visible extension that provides some tools to work with colors.", category = ComponentCategory.EXTENSION, nonVisible = true, iconName = "https://docs.google.com/drawings/d/e/2PACX-1vQCI87PHLBF0jb8QWyYmIRQSjjNW3EFXf-qpsWCvBYkUQ9vEgPAB8SpxcMpblxNpbIYrjCjLrRLIU2c/pub?w=16&h=16") @SimpleObject(external = true) //Libraries @UsesLibraries(libraries = "") //Permissions @UsesPermissions(permissionNames = "") public class ColorUtilities extends AndroidNonvisibleComponent { //Activity and Context private Context context; private Activity activity; public ColorUtilities(ComponentContainer container){ super(container.$form()); this.activity = container.$context(); this.context = container.$context(); } @SimpleFunction(description = "Convert a hex color to a integer color, which is used in the standard App Inventor color system.") public int ConvertHexToInt(String hexColor) { return Color.parseColor(hexColor); } @SimpleFunction(description = "Convert a integer color to a hex color.") public String ConvertIntToHex(int intColor) { return "#" + Integer.toHexString(intColor).toUpperCase(); } @SimpleFunction(description = "Returns the luminance of a color as a float number between 0 and 1.") public double GetLuminance(int color) { double luminance = ColorUtils.calculateLuminance(color); return luminance; } @SimpleFunction(description = "Checks whether the color is a dark color or a light color.") public boolean IsDarkColor(int color) { return ColorUtils.calculateLuminance(color) < 0.5d; } @SimpleFunction(description = "Darkens the color with the given factor.") public int DarkenColor(int color, double factor) { return darkenColor(color, factor); } public static int darkenColor(int color, double factor) { float f = (float) factor; int a = Color.alpha(color); int r = Math.round(Color.red(color) * f); int g = Math.round(Color.green(color) * f); int b = Math.round(Color.blue(color) * f); return Color.argb(a, Math.min(r,255), Math.min(g,255), Math.min(b,255)); } @SimpleFunction(description = "Blends two colors together. The ratio parameter should be a value between 0 and 1.") public int BlendColors(int color1, int color2, double ratio) { float rat = (float) ratio; float inverseRation = 1.0f - rat; float r = (float)Color.red((int)color1) * rat + (float)Color.red((int)color2) * inverseRation; float g = (float)Color.green((int)color1) * rat + (float)Color.green((int)color2) * inverseRation; float b = (float)Color.blue((int)color1) * rat + (float)Color.blue((int)color2) * inverseRation; return Color.rgb((int)((int)r), (int)((int)g), (int)((int)b)); } @SimpleFunction(description = "Lightens the color with the given factor.") public int LightenColor(int color, double factor) { return lightenColor(color, factor); } private static int lightenColor(int color, double fraction) { return (int) Math.min(color + (color * fraction), 255); } }