Skip to content

Commit

Permalink
🦊 getGradientObject function added to useColorPicker hook
Browse files Browse the repository at this point in the history
  • Loading branch information
hxf31891 committed Nov 16, 2022
1 parent b67777a commit 3a4221d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-best-gradient-color-picker",
"version": "2.1.10",
"version": "2.1.11",
"description": "An easy to use color/gradient picker for React.js",
"main": "lib/index.js",
"scripts": {
Expand Down
30 changes: 29 additions & 1 deletion src/useColorPicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ const { defaultColor, defaultGradient } = config;
var tc = require("tinycolor2");

export const useColorPicker = (value, onChange) => {

if (!value || !onChange) {
console.log('RBGCP ERROR - YOU MUST PASS A VALUE AND CALLBACK TO THE useColorPicker HOOK')
}

const isGradient = value?.includes("gradient");
const gradientType = getGradientType(value);
const degrees = getDegrees(value);
Expand All @@ -22,6 +27,28 @@ export const useColorPicker = (value, onChange) => {
const currentLeft = currentColorObj?.left;
const [previousColors, setPreviousColors] = useState([]);

const getGradientObject = () => {
if (value) {
if (isGradient) {
return {
isGradient: true,
gradientType: gradientType,
degrees: degreeStr,
colors: colors?.map((c) => ({ ...c, value: c.value?.toLowerCase() }))
}
} else {
return {
isGradient: false,
gradientType: null,
degrees: null,
colors: colors?.map((c) => ({ ...c, value: c.value?.toLowerCase() }))
}
}
} else {
console.log('RBGCP ERROR - YOU MUST PASS A VALUE AND CALLBACK TO THE useColorPicker HOOK')
}
}

const tiny = tc(currentColor);
const { r, g, b, a } = tiny.toRgb();
const { h, s, l } = tiny.toHsl();
Expand Down Expand Up @@ -234,6 +261,7 @@ export const useColorPicker = (value, onChange) => {
currentLeft,
rgbaArr,
hslArr,
previousColors
previousColors,
getGradientObject
};
};

0 comments on commit 3a4221d

Please sign in to comment.