Skip to content

Your app looks AI-generated. Vincent Van Gogh noticed. An agent skill that finds and fixes AI-looking UI and UX in mobile apps, web apps, and websites.

fix(detect): read SwiftUI Color(red:green:blue:) stops in slop-gradient (#162) (#169)

Pranav Arya2c391fc61250
diff --git a/skills/unslop-anything/scripts/detect.js b/skills/unslop-anything/scripts/detect.js
index 4d810d9..952c25a 100644
--- a/skills/unslop-anything/scripts/detect.js
+++ b/skills/unslop-anything/scripts/detect.js
@@ -149,6 +149,28 @@ function flutterColors(snippet) {
return out;
}
+// SwiftUI's Color(red:green:blue:) in a snippet, as uppercase hex, so it goes
+// through the same checks as Color(hex: 0x7C3AED). Components run from 0 to 1
+// and are often written as a fraction (124 / 255). A leading color space
+// (.sRGB) and a trailing opacity are skipped; a component that is not a
+// number skips the color.
+function swiftRgbColors(snippet) {
+ const out = [];
+ const number = "\\d+(?:\\.\\d+)?(?:[eE][+-]?\\d+)?";
+ const component = `(${number})(?:\\s*\\/\\s*(${number}))?`;
+ const re = new RegExp(`\\bColor\\(\\s*(?:\\.\\w+\\s*,\\s*)?red\\s*:\\s*${component}\\s*,\\s*green\\s*:\\s*${component}\\s*,\\s*blue\\s*:\\s*${component}\\s*[,)]`, "g");
+ const hex2 = (value, divisor) => {
+ const unit = divisor === undefined ? Number(value) : Number(value) / Number(divisor);
+ return Math.round(Math.min(1, Math.max(0, unit)) * 255).toString(16).padStart(2, "0");
+ };
+ let m;
+ while ((m = re.exec(snippet))) {
+ if ([m[2], m[4], m[6]].some((divisor) => Number(divisor) === 0)) continue;
+ out.push((hex2(m[1], m[2]) + hex2(m[3], m[4]) + hex2(m[5], m[6])).toUpperCase());
+ }
+ return out;
+}
+
// The purple, indigo, violet and hot-pink band that default AI palettes live in.
function isSlopHue(hex) {
const { h, s, l } = hexToHsl(hex);
@@ -641,7 +663,7 @@ const rules = [
if (/^\s*(?:import|export\s+\{|from)\b/.test(code.slice(lineStart, m.index))) continue;
// Only the gradient's own stops count, not whatever follows it.
const stops = gradientScope(code, m.index, m[0]);
- if ([...hexColors(stops, stack), ...cssFunctionColors(stops), ...flutterColors(stops)].some(isSlopHue) || SLOP_COLOR_NAME.test(stops)) hits.push(m.index);
+ if ([...hexColors(stops, stack), ...cssFunctionColors(stops), ...flutterColors(stops), ...swiftRgbColors(stops)].some(isSlopHue) || SLOP_COLOR_NAME.test(stops)) hits.push(m.index);
}
// Tailwind: bg-gradient-to-r from-violet-600 to-fuchsia-500
for (const m of matches(/["'`][^"'`\n]*\bbg-(?:gradient|linear|radial|conic)-[^"'`\n]*["'`]/, code)) {
diff --git a/test/detect.test.js b/test/detect.test.js
index c26b951..13983a5 100644
--- a/test/detect.test.js
+++ b/test/detect.test.js
@@ -1614,3 +1614,28 @@ test("slop-gradient reads Flutter Color.fromARGB and Color.fromRGBO stops (issue
];
for (const stops of quiet) assert.deepStrictEqual(rulesFor(gradient(stops)), [], stops);
});
+
+test("slop-gradient reads SwiftUI Color(red:green:blue:) stops (issue #162)", () => {
+ const rulesFor = (src) => scanSource(src, "Hero.swift").map((f) => f.rule).filter((r) => r === "slop-gradient");
+ const gradient = (stops) => `LinearGradient(colors: [${stops}], startPoint: .leading, endPoint: .trailing)\n`;
+ const flagged = [
+ "Color(red: 0.49, green: 0.23, blue: 0.93), Color(red: 0.86, green: 0.15, blue: 0.47)",
+ "Color(red: 0.49, green: 0.23, blue: 0.93, opacity: 0.8), .white",
+ "Color(.sRGB, red: 0.49, green: 0.23, blue: 0.93, opacity: 1), .white",
+ // Components written as a fraction of 255.
+ "Color(red: 124 / 255, green: 58 / 255, blue: 237 / 255), .white",
+ "Color(red: 124.0/255.0, green: 58.0/255.0, blue: 237.0/255.0), .white",
+ // Swift also accepts an exponent.
+ "Color(red: 4.9e-1, green: 2.3e-1, blue: 9.3e-1), .white",
+ "Color(\n red: 0.49,\n green: 0.23,\n blue: 0.93\n ), .white",
+ ];
+ for (const stops of flagged) assert.deepStrictEqual(rulesFor(gradient(stops)), ["slop-gradient"], stops);
+ const quiet = [
+ "Color(red: 0.05, green: 0.65, blue: 0.91), Color(red: 0.13, green: 0.77, blue: 0.37)",
+ "Color(red: 14 / 255, green: 165 / 255, blue: 233 / 255), Color(red: 34 / 255, green: 197 / 255, blue: 94 / 255)",
+ "Color(red: 0, green: 0, blue: 0), Color(red: 1, green: 1, blue: 1)",
+ // A component that is not a number cannot be read.
+ "Color(red: r, green: g, blue: b), Color(red: tint.r, green: 0.23, blue: 0.93)",
+ ];
+ for (const stops of quiet) assert.deepStrictEqual(rulesFor(gradient(stops)), [], stops);
+});