Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(editor): gate hex RGB input until digit count is complete

Richie McIlroy91a5b4f7e0fe
diff --git a/apps/desktop/src/routes/editor/color-utils.tsx b/apps/desktop/src/routes/editor/color-utils.tsx
index b273470c7..b8c7e804e 100644
--- a/apps/desktop/src/routes/editor/color-utils.tsx
+++ b/apps/desktop/src/routes/editor/color-utils.tsx
@@ -1,6 +1,9 @@
import { createWritableMemo } from "@solid-primitives/memo";
+import { getHexColorDigitCount, hexToRgb } from "~/utils/hex-color";
import { TextInput } from "./TextInput";
+export { hexToRgb } from "~/utils/hex-color";
+
export function getColorPreviewBorderColor(color: string) {
return `color-mix(in srgb, ${color} 82%, black)`;
}
@@ -12,26 +15,6 @@ export function rgbToHex(rgb: [number, number, number]) {
.toUpperCase()}`;
}
-export function hexToRgb(hex: string): [number, number, number, number] | null {
- const match = hex.match(
- /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})?$/i,
- );
- if (!match) return null;
-
- const [, r, g, b, a] = match;
- const rgb = [
- Number.parseInt(r, 16),
- Number.parseInt(g, 16),
- Number.parseInt(b, 16),
- ] as const;
-
- if (a) {
- return [...rgb, Number.parseInt(a, 16)];
- }
-
- return [...rgb, 255];
-}
-
export function RgbInput(props: {
value: [number, number, number];
onChange: (value: [number, number, number]) => void;
@@ -97,6 +80,9 @@ export function RgbInput(props: {
onInput={(e) => {
setText(e.currentTarget.value);
+ const digitCount = getHexColorDigitCount(e.currentTarget.value);
+ if (digitCount !== 6 && digitCount !== 8) return;
+
const value = hexToRgb(e.currentTarget.value.trim());
if (!value) return;
diff --git a/apps/desktop/src/routes/editor/text-style.tsx b/apps/desktop/src/routes/editor/text-style.tsx
index e819ae6ce..eea166b5e 100644
--- a/apps/desktop/src/routes/editor/text-style.tsx
+++ b/apps/desktop/src/routes/editor/text-style.tsx
@@ -1,4 +1,8 @@
import { createWritableMemo } from "@solid-primitives/memo";
+import {
+ getHexColorDigitCount,
+ normalizeOpaqueHexColor,
+} from "~/utils/hex-color";
import { getColorPreviewBorderColor } from "./color-utils";
import { TextInput } from "./TextInput";
@@ -48,9 +52,8 @@ export function HexColorInput(props: {
let colorInput!: HTMLInputElement;
const commitValue = (raw: string) => {
- const trimmed = raw.trim();
- if (/^#[0-9A-F]{6}$/i.test(trimmed)) {
- const normalized = `#${trimmed.slice(1).toUpperCase()}`;
+ const normalized = normalizeOpaqueHexColor(raw);
+ if (normalized) {
props.onChange(normalized);
setText(normalized);
return true;
@@ -96,9 +99,10 @@ export function HexColorInput(props: {
}}
onInput={(e) => {
setText(e.currentTarget.value);
- const trimmed = e.currentTarget.value.trim();
- if (/^#[0-9A-F]{6}$/i.test(trimmed)) {
- const normalized = `#${trimmed.slice(1).toUpperCase()}`;
+ if (getHexColorDigitCount(e.currentTarget.value) !== 6) return;
+
+ const normalized = normalizeOpaqueHexColor(e.currentTarget.value);
+ if (normalized) {
props.onChange(normalized);
}
}}
diff --git a/apps/desktop/src/routes/screenshot-editor/ColorPicker.tsx b/apps/desktop/src/routes/screenshot-editor/ColorPicker.tsx
index a210bb35f..5e7f4b6c6 100644
--- a/apps/desktop/src/routes/screenshot-editor/ColorPicker.tsx
+++ b/apps/desktop/src/routes/screenshot-editor/ColorPicker.tsx
@@ -1,6 +1,9 @@
import { createWritableMemo } from "@solid-primitives/memo";
+import { getHexColorDigitCount, hexToRgb } from "~/utils/hex-color";
import { TextInput } from "./TextInput";
+export { hexToRgb } from "~/utils/hex-color";
+
export const BACKGROUND_COLORS = [
"#FF0000", // Red
"#FF4500", // Orange-Red
@@ -80,6 +83,9 @@ export function RgbInput(props: {
}}
onInput={(e) => {
setText(e.currentTarget.value);
+ const digitCount = getHexColorDigitCount(e.currentTarget.value);
+ if (digitCount !== 6 && digitCount !== 8) return;
+
const value = hexToRgb(e.currentTarget.value.trim());
if (!value) return;
const [r, g, b] = value;
@@ -102,20 +108,3 @@ export function rgbToHex(rgb: [number, number, number]) {
.join("")
.toUpperCase()}`;
}
-
-export function hexToRgb(hex: string): [number, number, number, number] | null {
- const match = hex.match(
- /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})?$/i,
- );
- if (!match) return null;
- const [, r, g, b, a] = match;
- const rgb = [
- Number.parseInt(r, 16),
- Number.parseInt(g, 16),
- Number.parseInt(b, 16),
- ] as const;
- if (a) {
- return [...rgb, Number.parseInt(a, 16)];
- }
- return [...rgb, 255];
-}