-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2298 from MahtabBukhari/color_picker_modal_part_2
Color picker popover part 2 && [Icons] Add icon column and icon to post request
- Loading branch information
Showing
12 changed files
with
644 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...rintModal/Body/Editor/ColorPickerPopover/ColorPickerPopoverView/ColorPicker/ColorUtils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
export const hexToHsl = (hex: string) => { | ||
let r = 0 | ||
let g = 0 | ||
let b = 0 | ||
|
||
if (hex.length === 4) { | ||
r = parseInt(hex[1] + hex[1], 16) | ||
g = parseInt(hex[2] + hex[2], 16) | ||
b = parseInt(hex[3] + hex[3], 16) | ||
} else if (hex.length === 7) { | ||
r = parseInt(hex[1] + hex[2], 16) | ||
g = parseInt(hex[3] + hex[4], 16) | ||
b = parseInt(hex[5] + hex[6], 16) | ||
} | ||
|
||
r /= 255 | ||
g /= 255 | ||
b /= 255 | ||
|
||
const max = Math.max(r, g, b) | ||
const min = Math.min(r, g, b) | ||
|
||
let h = 0 | ||
let s = 0 | ||
const l = (max + min) / 2 | ||
|
||
if (max !== min) { | ||
const d = max - min | ||
|
||
s = l > 0.5 ? d / (2 - max - min) : d / (max + min) | ||
|
||
// eslint-disable-next-line default-case | ||
switch (max) { | ||
case r: | ||
h = (g - b) / d + (g < b ? 6 : 0) | ||
break | ||
case g: | ||
h = (b - r) / d + 2 | ||
break | ||
case b: | ||
h = (r - g) / d + 4 | ||
break | ||
} | ||
|
||
h /= 6 | ||
} | ||
|
||
return { | ||
h: Math.round(h * 360), | ||
s: Math.round(s * 100), | ||
l: Math.round(l * 100), | ||
} | ||
} | ||
|
||
export const hslToHex = (h: number, s: number, l: number) => { | ||
const saturation = s / 100 | ||
|
||
const lightness = l / 100 | ||
|
||
const k = (n: number) => (n + h / 30) % 12 | ||
const a = saturation * Math.min(lightness, 1 - lightness) | ||
|
||
const f = (n: number) => Math.round(255 * (lightness - a * Math.max(-1, Math.min(k(n) - 3, 9 - k(n), 1)))) | ||
|
||
const r = f(0) | ||
const g = f(8) | ||
const b = f(4) | ||
|
||
// Construct the hex value without bitwise operations | ||
const hex = `#${[r, g, b] | ||
.map((val) => val.toString(16).padStart(2, '0')) | ||
.join('') | ||
.toUpperCase()}` | ||
|
||
return hex | ||
} |
118 changes: 118 additions & 0 deletions
118
...al/Body/Editor/ColorPickerPopover/ColorPickerPopoverView/ColorPicker/SaturationPicker.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import React, { useRef, useEffect, useState, useCallback } from 'react' | ||
import styled from 'styled-components' | ||
import { hslToHex } from './ColorUtils' | ||
|
||
interface SaturationPickerProps { | ||
hue: number | ||
onChange: (color: string) => void | ||
} | ||
|
||
const Container = styled.div` | ||
position: relative; | ||
` | ||
|
||
const Pointer = styled.div<{ x: number; y: number }>` | ||
position: absolute; | ||
top: ${(props) => props.y - 7}px; | ||
left: ${(props) => props.x - 7}px; | ||
width: 8px; | ||
height: 8px; | ||
border-radius: 50%; | ||
border: 2px solid white; | ||
background-color: transparent; | ||
box-shadow: inset 0 0 0 3px rgba(0, 0, 0, 0.3); | ||
pointer-events: none; | ||
transition: top 0.1s ease, left 0.1s ease; | ||
` | ||
|
||
const SaturationPicker: React.FC<SaturationPickerProps> = ({ hue, onChange }) => { | ||
const canvasRef = useRef<HTMLCanvasElement | null>(null) | ||
const [pointerPos, setPointerPos] = useState<{ x: number; y: number }>({ x: 80, y: 50 }) | ||
const [isDragging, setIsDragging] = useState(false) | ||
|
||
useEffect(() => { | ||
const canvas = canvasRef.current | ||
|
||
if (canvas) { | ||
const ctx = canvas.getContext('2d') | ||
|
||
if (ctx) { | ||
const { width, height } = canvas | ||
|
||
// eslint-disable-next-line no-plusplus | ||
for (let x = 0; x < width; x++) { | ||
// eslint-disable-next-line no-plusplus | ||
for (let y = 0; y < height; y++) { | ||
const saturation = x / width | ||
const brightness = 1 - y / height | ||
const color = `hsl(${hue}, ${saturation * 100}%, ${brightness * 100}%)` | ||
|
||
ctx.fillStyle = color | ||
ctx.fillRect(x, y, 1, 1) | ||
} | ||
} | ||
} | ||
} | ||
}, [hue]) | ||
|
||
const handleCanvasInteraction = useCallback( | ||
(e: React.MouseEvent | MouseEvent) => { | ||
const canvas = canvasRef.current | ||
|
||
if (canvas) { | ||
const rect = canvas.getBoundingClientRect() | ||
const x = e.clientX - rect.left | ||
const y = e.clientY - rect.top | ||
|
||
const saturation = x / canvas.width | ||
const brightness = 1 - y / canvas.height | ||
|
||
const hexColor = hslToHex(hue, saturation * 100, brightness * 100) | ||
|
||
setPointerPos({ x, y }) | ||
onChange(hexColor) | ||
} | ||
}, | ||
[hue, onChange], | ||
) | ||
|
||
const handleMouseDown = (e: React.MouseEvent<HTMLCanvasElement>) => { | ||
setIsDragging(true) | ||
handleCanvasInteraction(e) | ||
} | ||
|
||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
const handleMouseMove = (e: MouseEvent) => { | ||
if (isDragging) { | ||
handleCanvasInteraction(e) | ||
} | ||
} | ||
|
||
const handleMouseUp = () => { | ||
setIsDragging(false) | ||
} | ||
|
||
useEffect(() => { | ||
if (isDragging) { | ||
window.addEventListener('mousemove', handleMouseMove) | ||
window.addEventListener('mouseup', handleMouseUp) | ||
} else { | ||
window.removeEventListener('mousemove', handleMouseMove) | ||
window.removeEventListener('mouseup', handleMouseUp) | ||
} | ||
|
||
return () => { | ||
window.removeEventListener('mousemove', handleMouseMove) | ||
window.removeEventListener('mouseup', handleMouseUp) | ||
} | ||
}, [isDragging, handleMouseMove]) | ||
|
||
return ( | ||
<Container> | ||
<canvas ref={canvasRef} height={162} onMouseDown={handleMouseDown} width={260} /> | ||
<Pointer x={pointerPos.x} y={pointerPos.y} /> | ||
</Container> | ||
) | ||
} | ||
|
||
export default SaturationPicker |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.