Giter Site home page Giter Site logo

Comments (4)

lotarbo avatar lotarbo commented on May 27, 2024 1

@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.

jawnirock avatar jawnirock commented on May 27, 2024

Same problem, is there a way to set this initialvalue?

from color-input.

vin-ni avatar vin-ni commented on May 27, 2024

@lotarbo How do you import it?

from color-input.

Mtillmann avatar Mtillmann commented on May 27, 2024

@lotarbo thanks for the effort! However, I think the devs should address this issue

from color-input.

Related Issues (5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.