Comments (4)
@jawnirock i do it with this trick
import {defineField} from 'sanity'
function hex2rgb(hex: string, a: number) {
const bigint = parseInt(hex.split('#')[1], 16)
return {
_type: 'rgbaColor',
r: (bigint >> 16) & 255,
g: (bigint >> 8) & 255,
b: bigint & 255,
a: a,
}
}
function rgb2Hsl(rgb: {_type: string; r: number; g: number; b: number; a: number}) {
let r = rgb.r / 255
let g = rgb.g / 255
let b = rgb.b / 255
let max = Math.max(r, g, b)
let min = Math.min(r, g, b)
let d = max - min
let h = 0
if (d === 0) h = 0
else if (max === r) h = ((g - b) / d) % 6
else if (max === g) h = (b - r) / d + 2
else if (max === b) h = (r - g) / d + 4
let l = (min + max) / 2
let s = d === 0 ? 0 : d / (1 - Math.abs(2 * l - 1))
return {
_type: 'hslaColor',
h: h * 60,
s: s,
l: l,
a: rgb.a,
}
}
function rgb2Hsv(rgb: {_type: string; r: number; g: number; b: number; a: number}) {
let r = rgb.r
let g = rgb.g
let b = rgb.b
let v = Math.max(r, g, b),
c = v - Math.min(r, g, b)
let h = c && (v == r ? (g - b) / c : v == g ? 2 + (b - r) / c : 4 + (r - g) / c)
return {
_type: 'hsvaColor',
h: 60 * (h < 0 ? h + 6 : h),
s: v && c / v,
v: v,
a: rgb.a,
}
}
function createColorInputFromHex(hex: string, a: number) {
const rgba = hex2rgb(hex, a)
return {
_type: 'color',
hex: hex,
alpha: a,
hsl: rgb2Hsl(rgba),
hsv: rgb2Hsv(rgba),
rgb: rgba,
}
}
export const projectShadowColorField = (hex?: string, a: number = 1) => {
return defineField({
name: 'shadowColor',
type: 'color',
title: 'Shadow Color',
initialValue: hex ? createColorInputFromHex(hex, a) : {},
})
}
and after that use this field
projectShadowColorField('#113F45', 1),
from color-input.
Same problem, is there a way to set this initialvalue?
from color-input.
@lotarbo How do you import it?
from color-input.
@lotarbo thanks for the effort! However, I think the devs should address this issue
from color-input.
Related Issues (5)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from color-input.