Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 4 additions & 14 deletions src/components/GradientInput/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Select, Slider, InputNumber } from "antd";
import { Select, Slider } from "antd";
import { GradientOption } from "../../types";
import {
useSelectedRecipeId,
useUpdateRecipeObj,
useGetCurrentValue,
useCurrentRecipeString,
} from "../../state/store";
import { getSelectedGradient, deriveGradientStrength, round2, toStore } from "../../utils/gradient";
import { getSelectedGradient, deriveGradientStrength, round2 } from "../../utils/gradient";
import "./style.css";

interface GradientInputProps {
Expand Down Expand Up @@ -48,8 +48,7 @@ const GradientInput = (props: GradientInputProps): JSX.Element => {
const handleStrengthChange = (val: number | null) => {
if (val == null || !selectedRecipeId || !gradientStrengthData) return;
const uiVal = round2(val);
const storeVal = toStore(uiVal);
updateRecipeObj(selectedRecipeId, { [gradientStrengthData.path]: storeVal });
updateRecipeObj(selectedRecipeId, { [gradientStrengthData.path]: uiVal });
};

const selectOptions = gradientOptions.map((option) => ({
Expand Down Expand Up @@ -87,16 +86,7 @@ const GradientInput = (props: GradientInputProps): JSX.Element => {
onChange={(val) => handleStrengthChange(val)}
value={gradientStrengthData.uiValue}
step={0.01}
style={{ width: "60%" }}
/>
<InputNumber
id={gradientStrengthData.displayName + " Input"}
min={gradientStrengthData.min}
max={gradientStrengthData.max}
value={gradientStrengthData.uiValue}
onChange={(val) => handleStrengthChange(val)}
step={0.01}
style={{ margin: "0 6px" }}
style={{ width: "100%" }}
/>
</div>
</div>
Expand Down
22 changes: 8 additions & 14 deletions src/utils/gradient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@ interface GradientStrength {
displayName: string;
description: string;
path: string;
uiValue: number; // current slider value in UI domain
uiValue: number;
min: number;
max: number;
};
}

// Helpers: store <-> UI mapping
// Store: "smaller = stronger" (e.g., decay length). UI: "bigger = stronger" (0..1).
export const toUi = (storeVal: number) => Number((1 - storeVal).toFixed(2));
export const toStore = (uiVal: number) => Number((1 - uiVal).toFixed(2));
export const round2 = (n: number) => Number(n.toFixed(2));


Expand Down Expand Up @@ -61,24 +57,22 @@ export function deriveGradientStrength(
if (!opt?.strength_path) return undefined;

const storeMin = opt.strength_min ?? 0;
const storeMax = opt.strength_max ?? 0.99;
const storeMax = opt.strength_max ?? 5;

const uiMin = toUi(storeMax);
const uiMax = toUi(storeMin);
const uiMin = storeMin;
const uiMax = storeMax;

const clampUi = (v: number) => Math.min(uiMax, Math.max(uiMin, v));
const storeRaw = getCurrentValue(opt.strength_path);
const storeNum =
typeof storeRaw === "number"
? storeRaw
: opt.strength_default ?? storeMin;
const uiValue = round2(clampUi(toUi(storeNum)));
const uiValue = round2(clampUi(storeNum));

return {
displayName: opt.strength_display_name ?? `${opt.display_name} Strength`,
description:
opt.strength_description ??
"Higher values will make the gradient stronger",
displayName: `Decay Length`,
description: "Higher values will increase the decay length",
path: opt.strength_path,
uiValue,
min: uiMin,
Expand Down