@@ -30,8 +30,14 @@ import {
30
30
} from 'colorjs.io/fn' ;
31
31
import HSBSpace from './color_spaces/hsb.js' ;
32
32
33
- const map = ( n , start1 , stop1 , start2 , stop2 ) =>
34
- ( ( n - start1 ) / ( stop1 - start1 ) * ( stop2 - start2 ) + start2 ) ;
33
+ const map = ( n , start1 , stop1 , start2 , stop2 , clamp ) => {
34
+ let result = ( ( n - start1 ) / ( stop1 - start1 ) * ( stop2 - start2 ) + start2 ) ;
35
+ if ( clamp ) {
36
+ result = Math . max ( result , Math . min ( start2 , stop2 ) ) ;
37
+ result = Math . min ( result , Math . max ( start2 , stop2 ) ) ;
38
+ }
39
+ return result ;
40
+ }
35
41
36
42
const serializationMap = { } ;
37
43
@@ -63,7 +69,7 @@ class Color {
63
69
Color . #grayscaleMap[ mode ] = definition . fromGray ;
64
70
}
65
71
66
- constructor ( vals , colorMode , colorMaxes ) {
72
+ constructor ( vals , colorMode , colorMaxes , { clamp = false } = { } ) {
67
73
// This changes with the color object
68
74
this . mode = colorMode || RGB ;
69
75
@@ -106,16 +112,16 @@ class Color {
106
112
if ( colorMaxes ) {
107
113
// NOTE: need to consider different number of arguments (eg. CMYK)
108
114
if ( vals . length === 4 ) {
109
- mappedVals = Color . mapColorRange ( vals , this . mode , colorMaxes ) ;
115
+ mappedVals = Color . mapColorRange ( vals , this . mode , colorMaxes , clamp ) ;
110
116
} else if ( vals . length === 3 ) {
111
- mappedVals = Color . mapColorRange ( [ vals [ 0 ] , vals [ 1 ] , vals [ 2 ] ] , this . mode , colorMaxes ) ;
117
+ mappedVals = Color . mapColorRange ( [ vals [ 0 ] , vals [ 1 ] , vals [ 2 ] ] , this . mode , colorMaxes , clamp ) ;
112
118
mappedVals . push ( 1 ) ;
113
119
} else if ( vals . length === 2 ) {
114
120
// Grayscale with alpha
115
121
if ( Color . #grayscaleMap[ this . mode ] ) {
116
- mappedVals = Color . #grayscaleMap[ this . mode ] ( vals [ 0 ] , colorMaxes ) ;
122
+ mappedVals = Color . #grayscaleMap[ this . mode ] ( vals [ 0 ] , colorMaxes , clamp ) ;
117
123
} else {
118
- mappedVals = Color . mapColorRange ( [ vals [ 0 ] , vals [ 0 ] , vals [ 0 ] ] , this . mode , colorMaxes ) ;
124
+ mappedVals = Color . mapColorRange ( [ vals [ 0 ] , vals [ 0 ] , vals [ 0 ] ] , this . mode , colorMaxes , clamp ) ;
119
125
}
120
126
const alphaMaxes = Array . isArray ( colorMaxes [ colorMaxes . length - 1 ] ) ?
121
127
colorMaxes [ colorMaxes . length - 1 ] :
@@ -126,15 +132,16 @@ class Color {
126
132
alphaMaxes [ 0 ] ,
127
133
alphaMaxes [ 1 ] ,
128
134
0 ,
129
- 1
135
+ 1 ,
136
+ clamp
130
137
)
131
138
) ;
132
139
} else if ( vals . length === 1 ) {
133
140
// Grayscale only
134
141
if ( Color . #grayscaleMap[ this . mode ] ) {
135
- mappedVals = Color . #grayscaleMap[ this . mode ] ( vals [ 0 ] , colorMaxes ) ;
142
+ mappedVals = Color . #grayscaleMap[ this . mode ] ( vals [ 0 ] , colorMaxes , clamp ) ;
136
143
} else {
137
- mappedVals = Color . mapColorRange ( [ vals [ 0 ] , vals [ 0 ] , vals [ 0 ] ] , this . mode , colorMaxes ) ;
144
+ mappedVals = Color . mapColorRange ( [ vals [ 0 ] , vals [ 0 ] , vals [ 0 ] ] , this . mode , colorMaxes , clamp ) ;
138
145
}
139
146
mappedVals . push ( 1 ) ;
140
147
} else {
@@ -157,7 +164,7 @@ class Color {
157
164
}
158
165
159
166
// Convert from p5 color range to color.js color range
160
- static mapColorRange ( origin , mode , maxes ) {
167
+ static mapColorRange ( origin , mode , maxes , clamp ) {
161
168
const p5Maxes = maxes . map ( ( max ) => {
162
169
if ( ! Array . isArray ( max ) ) {
163
170
return [ 0 , max ] ;
@@ -168,7 +175,7 @@ class Color {
168
175
const colorjsMaxes = Color . #colorjsMaxes[ mode ] ;
169
176
170
177
return origin . map ( ( channel , i ) => {
171
- const newval = map ( channel , p5Maxes [ i ] [ 0 ] , p5Maxes [ i ] [ 1 ] , colorjsMaxes [ i ] [ 0 ] , colorjsMaxes [ i ] [ 1 ] ) ;
178
+ const newval = map ( channel , p5Maxes [ i ] [ 0 ] , p5Maxes [ i ] [ 1 ] , colorjsMaxes [ i ] [ 0 ] , colorjsMaxes [ i ] [ 1 ] , clamp ) ;
172
179
return newval ;
173
180
} ) ;
174
181
}
@@ -680,7 +687,7 @@ function color(p5, fn, lifecycles){
680
687
*/
681
688
p5 . Color = Color ;
682
689
683
- sRGB . fromGray = P3 . fromGray = function ( val , maxes ) {
690
+ sRGB . fromGray = P3 . fromGray = function ( val , maxes , clamp ) {
684
691
// Use blue max
685
692
const p5Maxes = maxes . map ( ( max ) => {
686
693
if ( ! Array . isArray ( max ) ) {
@@ -690,11 +697,11 @@ function color(p5, fn, lifecycles){
690
697
}
691
698
} ) ;
692
699
693
- const v = map ( val , p5Maxes [ 2 ] [ 0 ] , p5Maxes [ 2 ] [ 1 ] , 0 , 1 ) ;
700
+ const v = map ( val , p5Maxes [ 2 ] [ 0 ] , p5Maxes [ 2 ] [ 1 ] , 0 , 1 , clamp ) ;
694
701
return [ v , v , v ] ;
695
702
} ;
696
703
697
- HSBSpace . fromGray = HSLSpace . fromGray = function ( val , maxes ) {
704
+ HSBSpace . fromGray = HSLSpace . fromGray = function ( val , maxes , clamp ) {
698
705
// Use brightness max
699
706
const p5Maxes = maxes . map ( ( max ) => {
700
707
if ( ! Array . isArray ( max ) ) {
@@ -704,11 +711,11 @@ function color(p5, fn, lifecycles){
704
711
}
705
712
} ) ;
706
713
707
- const v = map ( val , p5Maxes [ 2 ] [ 0 ] , p5Maxes [ 2 ] [ 1 ] , 0 , 100 ) ;
714
+ const v = map ( val , p5Maxes [ 2 ] [ 0 ] , p5Maxes [ 2 ] [ 1 ] , 0 , 100 , clamp ) ;
708
715
return [ 0 , 0 , v ] ;
709
716
} ;
710
717
711
- HWBSpace . fromGray = function ( val , maxes ) {
718
+ HWBSpace . fromGray = function ( val , maxes , clamp ) {
712
719
// Use Whiteness and Blackness to create number line
713
720
const p5Maxes = maxes . map ( ( max ) => {
714
721
if ( ! Array . isArray ( max ) ) {
@@ -738,7 +745,7 @@ function color(p5, fn, lifecycles){
738
745
LCHSpace . fromGray =
739
746
OKLab . fromGray =
740
747
OKLCHSpace . fromGray =
741
- function ( val , maxes ) {
748
+ function ( val , maxes , clamp ) {
742
749
// Use lightness max
743
750
const p5Maxes = maxes . map ( ( max ) => {
744
751
if ( ! Array . isArray ( max ) ) {
@@ -748,7 +755,7 @@ function color(p5, fn, lifecycles){
748
755
}
749
756
} ) ;
750
757
751
- const v = map ( val , p5Maxes [ 0 ] [ 0 ] , p5Maxes [ 0 ] [ 1 ] , 0 , 100 ) ;
758
+ const v = map ( val , p5Maxes [ 0 ] [ 0 ] , p5Maxes [ 0 ] [ 1 ] , 0 , 100 , clamp ) ;
752
759
return [ v , 0 , 0 ] ;
753
760
} ;
754
761
0 commit comments