Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saturation range mode #59

Open
Mrezagolbaba opened this issue Dec 28, 2021 · 3 comments
Open

Saturation range mode #59

Mrezagolbaba opened this issue Dec 28, 2021 · 3 comments

Comments

@Mrezagolbaba
Copy link

How can I use saturation like hue and alpha in range mode?
I need this one
Hue
Saturation
Shade
Alpha

Screen Shot 2021-12-28 at 11 01 04 PM

@jaywcjlove
Copy link
Member

There seems to be something that is not easy to achieve. @Mrezagolbaba

https://codesandbox.io/s/react-color-example-59-f9v1f?file=/src/App.js:0-892

import { useState } from "react";
import Hue from "@uiw/react-color-hue";
import ShadeSlider from "@uiw/react-color-shade-slider";
import Alpha from "@uiw/react-color-alpha";
import { hsvaToHex } from "@uiw/color-convert";

export default function App() {
  const [hsva, setHsva] = useState({ h: 0, s: 0, v: 68, a: 1 });
  return (
    <div>
      <h1>{hsvaToHex(hsva)}</h1>
      <Hue
        hue={hsva.h}
        style={{ marginTop: 10 }}
        onChange={(newHue) => {
          setHsva({ ...hsva, ...newHue });
        }}
      />
      <ShadeSlider
        hsva={hsva}
        style={{ marginTop: 10 }}
        onChange={(newShade) => {
          setHsva({ ...hsva, ...newShade });
        }}
      />
      <Alpha
        hsva={hsva}
        style={{ marginTop: 10 }}
        onChange={(newAlpha) => {
          setHsva({ ...hsva, ...newAlpha });
        }}
      />
    </div>
  );
}

This requires writing a new component.

image

@jaywcjlove
Copy link
Member

@Mrezagolbaba Custom <Saturation /> components.

https://codesandbox.io/s/react-color-example-59-f9v1f?file=/src/App.js:0-2223

import { useState } from "react";
import Hue from "@uiw/react-color-hue";
import ShadeSlider from "@uiw/react-color-shade-slider";
import Saturation from "@uiw/react-color-saturation";
import Alpha, { BACKGROUND_IMG } from "@uiw/react-color-alpha";
import { hsvaToHex, hsvaToRgbaString } from "@uiw/color-convert";

const Pointer = ({ style, color, ...props }) => (
  <div
    {...props}
    style={{
      height: 28,
      width: 28,
      position: "absolute",
      transform: "translate(-14px, -4px)",
      boxShadow: "0 2px 4px rgb(0 0 0 / 20%)",
      borderRadius: "50%",
      background: `url(${BACKGROUND_IMG})`,
      backgroundColor: "#fff",
      border: "2px solid #fff",
      zIndex: 1,
      ...style
    }}
  >
    <div
      style={{
        backgroundColor: color,
        borderRadius: "50%",
        height: " 100%",
        width: "100%"
      }}
    />
  </div>
);

export default function App() {
  const [hsva, setHsva] = useState({ h: 0, s: 0, v: 68, a: 1 });
  return (
    <div>
      <h1
        style={{
          backgroundColor: `${hsvaToRgbaString(hsva)}`,
          backgroundImage: `url(${BACKGROUND_IMG})`
        }}
      >
        {hsvaToHex(hsva)}
      </h1>
      <Saturation
        hsva={hsva}
        radius="8px 8px 0 0"
        style={{
          width: "auto",
          height: 50,
          minWidth: 120,
          borderBottom: "12px solid #000"
        }}
        pointer={({ left, top, color }) => (
          <Pointer
            style={{ left, top, transform: "translate(-16px, -16px)" }}
            color={hsvaToHex(hsva)}
          />
        )}
        onChange={(newColor) => {
          setHsva({ ...hsva, ...newColor });
        }}
      />
      <Hue
        hue={hsva.h}
        style={{ marginTop: 10 }}
        onChange={(newHue) => {
          setHsva({ ...hsva, ...newHue });
        }}
      />
      <ShadeSlider
        hsva={hsva}
        style={{ marginTop: 10 }}
        onChange={(newShade) => {
          setHsva({ ...hsva, ...newShade });
        }}
      />
      <Alpha
        hsva={hsva}
        style={{ marginTop: 10 }}
        onChange={(newAlpha) => {
          setHsva({ ...hsva, ...newAlpha });
        }}
      />
    </div>
  );
}

@Mrezagolbaba
Copy link
Author

@jaywcjlove I think the color tonnage for saturation is incorrect. because Shade is how much black there is.
saturation is how strong/weak a color is

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants