Skip to content

Commit

Permalink
feat: Use lStared colors for default colors
Browse files Browse the repository at this point in the history
Used reflection API to access private method
  • Loading branch information
aikrq committed Dec 1, 2024
1 parent 53c96bb commit 83b0e94
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 11 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/a/a/a/lC.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ public static HashMap<String, Object> b(String str) {

public static void b(String str, HashMap<String, Object> hashMap) {
HashMap<String, Object> hashMap2 = hashMap;
String str1 = "color_background";
String str2 = "color_control_highlight";
String str3 = "color_primary_dark";
String str4 = "color_primary";
Expand Down Expand Up @@ -208,6 +209,7 @@ public static void b(String str, HashMap<String, Object> hashMap) {
a.put(str5, hashMap2.get(str5));
a.put(str4, hashMap2.get(str4));
a.put(str3, hashMap2.get(str3));
a.put(str1, hashMap2.get(str1));
path = str2;
a.put(path, hashMap2.get(path));
path = str15;
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/java/a/a/a/yq.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ public class yq {

public final int colorControlNormal;

public final int colorBackground;

public final String versionCode;

public final String versionName;
Expand Down Expand Up @@ -227,6 +229,7 @@ public yq(Context context, String myscFolderPath, HashMap<String, Object> metada
colorPrimaryDark = yB.a(metadata, ProjectFile.COLOR_PRIMARY_DARK, getDefaultColor(ProjectFile.COLOR_PRIMARY_DARK));
colorControlHighlight = yB.a(metadata, ProjectFile.COLOR_CONTROL_HIGHLIGHT, getDefaultColor(ProjectFile.COLOR_CONTROL_HIGHLIGHT));
colorControlNormal = yB.a(metadata, ProjectFile.COLOR_CONTROL_NORMAL, getDefaultColor(ProjectFile.COLOR_CONTROL_NORMAL));
colorBackground = yB.a(metadata, ProjectFile.COLOR_BACKGROUND, getDefaultColor(ProjectFile.COLOR_BACKGROUND));

projectSettings = new ProjectSettings(sc_id);
fileUtil = new oB(true);
Expand Down Expand Up @@ -837,11 +840,13 @@ public ArrayList<SrcCodeBean> a(hC projectFileManager, eC projectDataManager, Bu
colorsFileBuilder.addColor("colorAccent", String.format("#%06X", colorAccent & 0xffffff));
colorsFileBuilder.addColor("colorControlHighlight", String.format("#%06X", colorControlHighlight & 0xffffff));
colorsFileBuilder.addColor("colorControlNormal", String.format("#%06X", colorControlNormal & 0xffffff));
colorsFileBuilder.addColor("colorBackground", String.format("#%06X", colorBackground & 0xffffff));
srcCodeBeans.add(new SrcCodeBean("colors.xml",
CommandBlock.applyCommands("colors.xml", colorsFileBuilder.toCode())));

XmlBuilderHelper stylesFileBuilder = new XmlBuilderHelper();
stylesFileBuilder.addStyle("AppTheme", "Theme.MaterialComponents.Light.NoActionBar" + (useNewMaterialComponentsTheme ? "" : ".Bridge"));
stylesFileBuilder.addItemToStyle("AppTheme", "android:colorBackground", "@color/colorBackground");
stylesFileBuilder.addItemToStyle("AppTheme", "colorPrimary", "@color/colorPrimary");
stylesFileBuilder.addItemToStyle("AppTheme", "colorPrimaryDark", "@color/colorPrimaryDark");
stylesFileBuilder.addItemToStyle("AppTheme", "colorAccent", "@color/colorAccent");
Expand All @@ -857,18 +862,21 @@ public ArrayList<SrcCodeBean> a(hC projectFileManager, eC projectDataManager, Bu
} else {
XmlBuilderHelper stylesFileBuilder = new XmlBuilderHelper();
stylesFileBuilder.addStyle("AppTheme", "@android:style/Theme.Material.Light.DarkActionBar");
stylesFileBuilder.addItemToStyle("AppTheme", "android:colorBackground", "@color/colorBackground");
stylesFileBuilder.addItemToStyle("AppTheme", "android:colorPrimary", "@color/colorPrimary");
stylesFileBuilder.addItemToStyle("AppTheme", "android:colorPrimaryDark", "@color/colorPrimaryDark");
stylesFileBuilder.addItemToStyle("AppTheme", "android:colorAccent", "@color/colorAccent");
stylesFileBuilder.addItemToStyle("AppTheme", "android:colorControlHighlight", "@color/colorControlHighlight");
stylesFileBuilder.addItemToStyle("AppTheme", "android:colorControlNormal", "@color/colorControlNormal");
stylesFileBuilder.addStyle("FullScreen", "@android:style/Theme.Material.Light.NoActionBar.Fullscreen");
stylesFileBuilder.addItemToStyle("FullScreen", "android:colorBackground", "@color/colorBackground");
stylesFileBuilder.addItemToStyle("FullScreen", "android:colorPrimary", "@color/colorPrimary");
stylesFileBuilder.addItemToStyle("FullScreen", "android:colorPrimaryDark", "@color/colorPrimaryDark");
stylesFileBuilder.addItemToStyle("FullScreen", "android:colorAccent", "@color/colorAccent");
stylesFileBuilder.addItemToStyle("FullScreen", "android:colorControlHighlight", "@color/colorControlHighlight");
stylesFileBuilder.addItemToStyle("FullScreen", "android:colorControlNormal", "@color/colorControlNormal");
stylesFileBuilder.addStyle("NoActionBar", "@android:style/Theme.Material.Light.NoActionBar");
stylesFileBuilder.addItemToStyle("NoActionBar", "android:colorBackground", "@color/colorBackground");
stylesFileBuilder.addItemToStyle("NoActionBar", "android:colorPrimary", "@color/colorPrimary");
stylesFileBuilder.addItemToStyle("NoActionBar", "android:colorPrimaryDark", "@color/colorPrimaryDark");
stylesFileBuilder.addItemToStyle("NoActionBar", "android:colorAccent", "@color/colorAccent");
Expand All @@ -880,6 +888,7 @@ public ArrayList<SrcCodeBean> a(hC projectFileManager, eC projectDataManager, Bu
CommandBlock.applyCommands("styles.xml", stylesFileBuilder.toCode())));

XmlBuilderHelper colorsFileBuilder = new XmlBuilderHelper();
colorsFileBuilder.addColor("colorBackground", String.format("#%06X", colorBackground & 0xffffff));
colorsFileBuilder.addColor("colorPrimary", String.format("#%06X", colorPrimary & 0xffffff));
colorsFileBuilder.addColor("colorPrimaryDark", String.format("#%06X", colorPrimaryDark & 0xffffff));
colorsFileBuilder.addColor("colorAccent", String.format("#%06X", colorAccent & 0xffffff));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,7 @@ private void setPreviewColors(String str) {
k.setBackgroundColor(ProjectFile.getColor(str, ProjectFile.COLOR_PRIMARY_DARK));
imgPhoneTopBg.setBackgroundColor(ProjectFile.getColor(str, ProjectFile.COLOR_PRIMARY_DARK));
toolbar.setBackgroundColor(ProjectFile.getColor(str, ProjectFile.COLOR_PRIMARY));
viewPane.setRootBackgroundColor(ProjectFile.getColor(str, ProjectFile.COLOR_BACKGROUND));
}

private void b(boolean z) {
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/com/besome/sketch/editor/view/ViewPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.ColorInt;

import com.besome.sketch.beans.ImageBean;
import com.besome.sketch.beans.LayoutBean;
import com.besome.sketch.beans.ProjectResourceBean;
Expand Down Expand Up @@ -322,6 +324,10 @@ private void addRootLayout() {
addView(rootView);
}

public void setRootBackgroundColor(@ColorInt int backgroundColor) {
rootLayout.setBackgroundColor(backgroundColor);
}

private void updateItemView(View view, ViewBean viewBean) {
ImageBean imageBean;
String str;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package com.besome.sketch.projects;

import static mod.hey.studios.util.ProjectFile.COLOR_ACCENT;
import static mod.hey.studios.util.ProjectFile.COLOR_BACKGROUND;
import static mod.hey.studios.util.ProjectFile.COLOR_CONTROL_HIGHLIGHT;
import static mod.hey.studios.util.ProjectFile.COLOR_CONTROL_NORMAL;
import static mod.hey.studios.util.ProjectFile.COLOR_PRIMARY;
import static mod.hey.studios.util.ProjectFile.COLOR_PRIMARY_DARK;
import static mod.hey.studios.util.ProjectFile.getDefaultColor;

import android.content.Context;
Expand Down Expand Up @@ -57,8 +63,8 @@ public class MyProjectSettingActivity extends BaseAppCompatActivity implements V
public MyprojectSettingBinding binding;

private static final int REQUEST_CODE_CREATE_ICON = 200212;
private final String[] themeColorKeys = {"color_accent", "color_primary", "color_primary_dark", "color_control_highlight", "color_control_normal"};
private final String[] themeColorLabels = {"colorAccent", "colorPrimary", "colorPrimaryDark", "colorControlHighlight", "colorControlNormal"};
private final String[] themeColorKeys = {COLOR_ACCENT, COLOR_PRIMARY, COLOR_PRIMARY_DARK, COLOR_CONTROL_HIGHLIGHT, COLOR_CONTROL_NORMAL, COLOR_BACKGROUND};
private final String[] themeColorLabels = {"colorAccent", "colorPrimary", "colorPrimaryDark", "colorControlHighlight", "colorControlNormal", "colorBackground"};
private final int[] projectThemeColors = new int[themeColorKeys.length];
private UB projectPackageNameValidator;
private VB projectNameValidator;
Expand Down Expand Up @@ -116,6 +122,7 @@ public void onCreate(Bundle savedInstanceState) {
projectThemeColors[2] = getDefaultColor(ProjectFile.COLOR_PRIMARY_DARK);
projectThemeColors[3] = getDefaultColor(ProjectFile.COLOR_CONTROL_HIGHLIGHT);
projectThemeColors[4] = getDefaultColor(ProjectFile.COLOR_CONTROL_NORMAL);
projectThemeColors[5] = getDefaultColor(ProjectFile.COLOR_BACKGROUND);

for (int i = 0; i < themeColorKeys.length; i++) {
ThemeColorView colorView = new ThemeColorView(this, i);
Expand Down
42 changes: 33 additions & 9 deletions app/src/main/java/mod/hey/studios/util/ProjectFile.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
package mod.hey.studios.util;

import static pro.sketchware.utility.ThemeUtils.isDarkThemeEnabled;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;

import androidx.core.content.res.ColorStateListInflaterCompat;

import pro.sketchware.SketchApplication;

import a.a.a.lC;
import a.a.a.yB;

import java.lang.reflect.Method;

public class ProjectFile {
public static final String COLOR_ACCENT = "color_accent";
public static final String COLOR_PRIMARY = "color_primary";
public static final String COLOR_PRIMARY_DARK = "color_primary_dark";
public static final String COLOR_CONTROL_HIGHLIGHT = "color_control_highlight";
public static final String COLOR_CONTROL_NORMAL = "color_control_normal";

// Android Framework
public static final String COLOR_BACKGROUND = "color_background";

public static int getColor(String sc_id, String color) {
return yB.a(lC.b(sc_id), color, getDefaultColor(color));
/*
Expand Down Expand Up @@ -42,21 +52,35 @@ public static int a(Map<String, Object> paramMap, String paramString, int paramI
*/

public static int getDefaultColor(String color) {
// Check if the device is running Android 12 or above
final Context ctx = SketchApplication.getContext();
final boolean isDark = isDarkThemeEnabled(ctx);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
// Android 12+ (API level 31): Use Material 3 dynamic colors
return switch (color) {
case COLOR_PRIMARY_DARK -> SketchApplication.getContext().getColor(android.R.color.system_accent1_500);
case COLOR_CONTROL_HIGHLIGHT -> SketchApplication.getContext().getColor(android.R.color.system_accent1_100);
default -> SketchApplication.getContext().getColor(android.R.color.system_accent1_500);
case COLOR_CONTROL_HIGHLIGHT -> ctx.getColor(isDark ? android.R.color.system_accent1_100 : android.R.color.system_accent1_900);
case COLOR_BACKGROUND ->
getLStaredColor(ctx, android.R.color.system_neutral2_600,
0xfffafafa, isDark ? 6.0f : 98.0f);
default -> ctx.getColor(android.R.color.system_accent1_600);
};
} else {
// For Android versions below 12: use static fallback colors
return switch (color) {
case COLOR_PRIMARY_DARK -> Color.parseColor("#ff1976d2");
case COLOR_CONTROL_HIGHLIGHT -> Color.parseColor("#202196f3");
default -> Color.parseColor("#ff2196f3");
case COLOR_CONTROL_HIGHLIGHT -> 0x202196f3;
case COLOR_BACKGROUND -> isDark ? 0xff121212 : 0xfffafafa;
default -> 0xff2196f3;
};
}
}

private static int getLStaredColor(Context ctx, int colorId, int defaultColor, float lStar) {
try {
Method method = ColorStateListInflaterCompat.class.getDeclaredMethod(
"modulateColorAlpha", int.class, float.class, float.class);
method.setAccessible(true);
return (int) method.invoke(ColorStateListInflaterCompat.class, ctx.getColor(colorId), 1.0f, lStar);
} catch (Exception e) {
e.printStackTrace();
return defaultColor;
}
}
}

0 comments on commit 83b0e94

Please sign in to comment.