forked from github/plane
chore: random color generator for options and number fields
This commit is contained in:
parent
1428e6b555
commit
d57e99ed30
@ -114,6 +114,7 @@ export const NumberAttributeForm: React.FC<FormComponentProps> = ({ control, wat
|
|||||||
className="hide-arrows"
|
className="hide-arrows"
|
||||||
min={0}
|
min={0}
|
||||||
step={1}
|
step={1}
|
||||||
|
defaultValue={100}
|
||||||
required
|
required
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
@ -128,23 +129,6 @@ export const NumberAttributeForm: React.FC<FormComponentProps> = ({ control, wat
|
|||||||
name="color"
|
name="color"
|
||||||
render={({ field: { onChange, value } }) => (
|
render={({ field: { onChange, value } }) => (
|
||||||
<ColorPicker onChange={onChange} selectedColor={value ?? "#000000"} size={18} />
|
<ColorPicker onChange={onChange} selectedColor={value ?? "#000000"} size={18} />
|
||||||
// <label htmlFor="numberColorPicker" className="relative block cursor-pointer">
|
|
||||||
// <Input
|
|
||||||
// type="color"
|
|
||||||
// id="numberColorPicker"
|
|
||||||
// placeholder="Accent color"
|
|
||||||
// className="hide-color cursor-pointer"
|
|
||||||
// value={value}
|
|
||||||
// onChange={(e) => onChange(e.target.value)}
|
|
||||||
// required
|
|
||||||
// />
|
|
||||||
// <span
|
|
||||||
// className="absolute top-1/2 -translate-y-[65%] left-2 h-5 w-5 rounded pointer-events-none"
|
|
||||||
// style={{
|
|
||||||
// backgroundColor: value,
|
|
||||||
// }}
|
|
||||||
// />
|
|
||||||
// </label>
|
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
@ -9,6 +9,8 @@ import { useMobxStore } from "lib/mobx/store-provider";
|
|||||||
import { ColorPicker } from "components/custom-attributes";
|
import { ColorPicker } from "components/custom-attributes";
|
||||||
// ui
|
// ui
|
||||||
import { PrimaryButton } from "components/ui";
|
import { PrimaryButton } from "components/ui";
|
||||||
|
// helpers
|
||||||
|
import { getRandomColor } from "helpers/color.helper";
|
||||||
// types
|
// types
|
||||||
import { ICustomAttribute } from "types";
|
import { ICustomAttribute } from "types";
|
||||||
|
|
||||||
@ -24,7 +26,7 @@ export const OptionForm: React.FC<Props> = observer((props) => {
|
|||||||
|
|
||||||
const [option, setOption] = useState<Partial<ICustomAttribute>>({
|
const [option, setOption] = useState<Partial<ICustomAttribute>>({
|
||||||
display_name: "",
|
display_name: "",
|
||||||
color: "#000000",
|
color: getRandomColor(),
|
||||||
});
|
});
|
||||||
const [isEditing, setIsEditing] = useState(false);
|
const [isEditing, setIsEditing] = useState(false);
|
||||||
|
|
||||||
@ -73,7 +75,7 @@ export const OptionForm: React.FC<Props> = observer((props) => {
|
|||||||
|
|
||||||
setOption({
|
setOption({
|
||||||
display_name: "",
|
display_name: "",
|
||||||
color: "#000000",
|
color: getRandomColor(),
|
||||||
});
|
});
|
||||||
|
|
||||||
if (onSubmit) onSubmit();
|
if (onSubmit) onSubmit();
|
||||||
@ -97,7 +99,7 @@ export const OptionForm: React.FC<Props> = observer((props) => {
|
|||||||
/>
|
/>
|
||||||
<ColorPicker
|
<ColorPicker
|
||||||
onChange={(val) => setOption((prev) => ({ ...prev, color: val }))}
|
onChange={(val) => setOption((prev) => ({ ...prev, color: val }))}
|
||||||
selectedColor={option.color ?? "#000000"}
|
selectedColor={option.color ?? getRandomColor()}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex-shrink-0">
|
<div className="flex-shrink-0">
|
||||||
|
@ -155,7 +155,7 @@ export const ObjectModal: React.FC<Props> = observer(
|
|||||||
leaveTo="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
|
leaveTo="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
|
||||||
>
|
>
|
||||||
<Dialog.Panel className="fixed inset-0 h-full w-full z-20">
|
<Dialog.Panel className="fixed inset-0 h-full w-full z-20">
|
||||||
<div className="flex items-center justify-center h-full w-full p-4 sm:p-0">
|
<div className="flex items-center justify-center h-full w-full p-4 sm:p-0 scale-90">
|
||||||
<div className="bg-custom-background-100 w-1/2 max-h-[85%] flex flex-col rounded-xl">
|
<div className="bg-custom-background-100 w-1/2 max-h-[85%] flex flex-col rounded-xl">
|
||||||
<h3 className="text-2xl font-semibold px-6 pt-5">New Object</h3>
|
<h3 className="text-2xl font-semibold px-6 pt-5">New Object</h3>
|
||||||
<div className="mt-5 space-y-5 h-full overflow-y-auto">
|
<div className="mt-5 space-y-5 h-full overflow-y-auto">
|
||||||
|
@ -11,6 +11,8 @@ import {
|
|||||||
Link2,
|
Link2,
|
||||||
LucideIcon,
|
LucideIcon,
|
||||||
} from "lucide-react";
|
} from "lucide-react";
|
||||||
|
// helpers
|
||||||
|
import { getRandomColor } from "helpers/color.helper";
|
||||||
// types
|
// types
|
||||||
import { ICustomAttribute, TCustomAttributeTypes, TCustomAttributeUnits } from "types";
|
import { ICustomAttribute, TCustomAttributeTypes, TCustomAttributeUnits } from "types";
|
||||||
|
|
||||||
@ -93,7 +95,7 @@ export const CUSTOM_ATTRIBUTES_LIST: {
|
|||||||
},
|
},
|
||||||
number: {
|
number: {
|
||||||
defaultFormValues: {
|
defaultFormValues: {
|
||||||
color: "#000000",
|
color: getRandomColor(),
|
||||||
default_value: "",
|
default_value: "",
|
||||||
display_name: "",
|
display_name: "",
|
||||||
extra_settings: {
|
extra_settings: {
|
||||||
@ -106,6 +108,7 @@ export const CUSTOM_ATTRIBUTES_LIST: {
|
|||||||
icon: Hash,
|
icon: Hash,
|
||||||
label: "Number",
|
label: "Number",
|
||||||
initialPayload: {
|
initialPayload: {
|
||||||
|
color: getRandomColor(),
|
||||||
extra_settings: {
|
extra_settings: {
|
||||||
representation: "numerical",
|
representation: "numerical",
|
||||||
},
|
},
|
||||||
|
@ -17,3 +17,32 @@ export const rgbToHex = (rgb: TRgb): string => {
|
|||||||
|
|
||||||
return `#${hexR}${hexG}${hexB}`;
|
return `#${hexR}${hexG}${hexB}`;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @returns {string} random hex color code
|
||||||
|
* @description function to generate a random vibrant hex color code
|
||||||
|
*/
|
||||||
|
|
||||||
|
export const getRandomColor = (): string => {
|
||||||
|
// Generate random RGB values
|
||||||
|
const r = Math.floor(Math.random() * 256);
|
||||||
|
const g = Math.floor(Math.random() * 256);
|
||||||
|
const b = Math.floor(Math.random() * 256);
|
||||||
|
|
||||||
|
// Calculate the luminance
|
||||||
|
const luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255;
|
||||||
|
|
||||||
|
// Define thresholds for luminance (adjust as needed)
|
||||||
|
const minLuminance = 0.3;
|
||||||
|
const maxLuminance = 0.7;
|
||||||
|
|
||||||
|
// Check if the luminance is within the desired range
|
||||||
|
if (luminance >= minLuminance && luminance <= maxLuminance) {
|
||||||
|
// Convert RGB to hex format
|
||||||
|
const hexColor = `#${((1 << 24) | (r << 16) | (g << 8) | b).toString(16).slice(1)}`;
|
||||||
|
return hexColor;
|
||||||
|
} else {
|
||||||
|
// Recurse to find a suitable color
|
||||||
|
return getRandomColor();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user