@@ -5,20 +5,19 @@ import { useEntityAdapterContext } from 'features/controlLayers/contexts/EntityA
5
5
import { useEntityIdentifierContext } from 'features/controlLayers/contexts/EntityIdentifierContext' ;
6
6
import { rasterLayerAdjustmentsCurvesUpdated } from 'features/controlLayers/store/canvasSlice' ;
7
7
import { selectCanvasSlice , selectEntity } from 'features/controlLayers/store/selectors' ;
8
+ import type { ChannelName , ChannelPoints } from 'features/controlLayers/store/types' ;
8
9
import React , { memo , useCallback , useEffect , useMemo , useRef , useState } from 'react' ;
9
10
import { useTranslation } from 'react-i18next' ;
10
11
import { PiArrowCounterClockwiseBold } from 'react-icons/pi' ;
11
12
12
- const DEFAULT_POINTS : Array < [ number , number ] > = [
13
+ const DEFAULT_POINTS : ChannelPoints = [
13
14
[ 0 , 0 ] ,
14
15
[ 255 , 255 ] ,
15
16
] ;
16
17
17
- type Channel = 'master' | 'r' | 'g' | 'b' ;
18
+ type ChannelHistograms = Record < ChannelName , number [ ] | null > ;
18
19
19
- type ChannelHistograms = Record < Channel , number [ ] | null > ;
20
-
21
- const channelColor : Record < Channel , string > = {
20
+ const channelColor : Record < ChannelName , string > = {
22
21
master : '#888' ,
23
22
r : '#e53e3e' ,
24
23
g : '#38a169' ,
@@ -27,7 +26,7 @@ const channelColor: Record<Channel, string> = {
27
26
28
27
const clamp = ( v : number , min : number , max : number ) => ( v < min ? min : v > max ? max : v ) ;
29
28
30
- const sortPoints = ( pts : Array < [ number , number ] > ) =>
29
+ const sortPoints = ( pts : ChannelPoints ) =>
31
30
[ ...pts ]
32
31
. sort ( ( a , b ) => {
33
32
const xDiff = a [ 0 ] - b [ 0 ] ;
@@ -63,17 +62,17 @@ const CANVAS_STYLE: React.CSSProperties = {
63
62
64
63
type CurveGraphProps = {
65
64
title : string ;
66
- channel : Channel ;
67
- points : Array < [ number , number ] > | undefined ;
65
+ channel : ChannelName ;
66
+ points : ChannelPoints | undefined ;
68
67
histogram : number [ ] | null ;
69
- onChange : ( pts : Array < [ number , number ] > ) => void ;
68
+ onChange : ( pts : ChannelPoints ) => void ;
70
69
} ;
71
70
72
71
const drawHistogram = (
73
72
c : HTMLCanvasElement ,
74
- channel : Channel ,
73
+ channel : ChannelName ,
75
74
histogram : number [ ] | null ,
76
- points : Array < [ number , number ] >
75
+ points : ChannelPoints
77
76
) => {
78
77
// Use device pixel ratio for crisp rendering on HiDPI displays.
79
78
const dpr = window . devicePixelRatio || 1 ;
@@ -207,7 +206,7 @@ const drawHistogram = (
207
206
}
208
207
} ;
209
208
210
- const getNearestPointIndex = ( c : HTMLCanvasElement , points : Array < [ number , number ] > , mx : number , my : number ) => {
209
+ const getNearestPointIndex = ( c : HTMLCanvasElement , points : ChannelPoints , mx : number , my : number ) => {
211
210
const cssWidth = c . clientWidth || CANVAS_WIDTH ;
212
211
const cssHeight = c . clientHeight || CANVAS_HEIGHT ;
213
212
const innerWidth = cssWidth - MARGIN_LEFT - MARGIN_RIGHT ;
@@ -249,7 +248,7 @@ const canvasYToValueY = (c: HTMLCanvasElement, cy: number) => {
249
248
const CurveGraph = memo ( function CurveGraph ( props : CurveGraphProps ) {
250
249
const { title, channel, points, histogram, onChange } = props ;
251
250
const canvasRef = useRef < HTMLCanvasElement | null > ( null ) ;
252
- const [ localPoints , setLocalPoints ] = useState < Array < [ number , number ] > > ( sortPoints ( points ?? DEFAULT_POINTS ) ) ;
251
+ const [ localPoints , setLocalPoints ] = useState < ChannelPoints > ( sortPoints ( points ?? DEFAULT_POINTS ) ) ;
253
252
const [ dragIndex , setDragIndex ] = useState < number | null > ( null ) ;
254
253
255
254
useEffect ( ( ) => {
@@ -333,7 +332,7 @@ const CurveGraph = memo(function CurveGraph(props: CurveGraphProps) {
333
332
) ;
334
333
335
334
const commit = useCallback (
336
- ( pts : Array < [ number , number ] > ) => {
335
+ ( pts : ChannelPoints ) => {
337
336
onChange ( sortPoints ( pts ) ) ;
338
337
} ,
339
338
[ onChange ]
@@ -534,17 +533,17 @@ export const RasterLayerCurvesAdjustmentsEditor = memo(() => {
534
533
} , [ layer ?. objects , layer ?. adjustments , recalcHistogram ] ) ;
535
534
536
535
const onChangePoints = useCallback (
537
- ( channel : Channel , pts : Array < [ number , number ] > ) => {
536
+ ( channel : ChannelName , pts : ChannelPoints ) => {
538
537
dispatch ( rasterLayerAdjustmentsCurvesUpdated ( { entityIdentifier, channel, points : pts } ) ) ;
539
538
} ,
540
539
[ dispatch , entityIdentifier ]
541
540
) ;
542
541
543
542
// Memoize per-channel change handlers to avoid inline lambdas in JSX
544
- const onChangeMaster = useCallback ( ( pts : Array < [ number , number ] > ) => onChangePoints ( 'master' , pts ) , [ onChangePoints ] ) ;
545
- const onChangeR = useCallback ( ( pts : Array < [ number , number ] > ) => onChangePoints ( 'r' , pts ) , [ onChangePoints ] ) ;
546
- const onChangeG = useCallback ( ( pts : Array < [ number , number ] > ) => onChangePoints ( 'g' , pts ) , [ onChangePoints ] ) ;
547
- const onChangeB = useCallback ( ( pts : Array < [ number , number ] > ) => onChangePoints ( 'b' , pts ) , [ onChangePoints ] ) ;
543
+ const onChangeMaster = useCallback ( ( pts : ChannelPoints ) => onChangePoints ( 'master' , pts ) , [ onChangePoints ] ) ;
544
+ const onChangeR = useCallback ( ( pts : ChannelPoints ) => onChangePoints ( 'r' , pts ) , [ onChangePoints ] ) ;
545
+ const onChangeG = useCallback ( ( pts : ChannelPoints ) => onChangePoints ( 'g' , pts ) , [ onChangePoints ] ) ;
546
+ const onChangeB = useCallback ( ( pts : ChannelPoints ) => onChangePoints ( 'b' , pts ) , [ onChangePoints ] ) ;
548
547
549
548
return (
550
549
< Flex
0 commit comments