MCPcopy Index your code
hub / github.com/processing/p5.js / constructor

Method constructor

src/color/p5.Color.js:87–220  ·  view source on GitHub ↗
(vals, colorMode, colorMaxes, { clamp = false } = {})

Source from the content-addressed store, hash-verified

85 }
86
87 constructor(vals, colorMode, colorMaxes, { clamp = false } = {}) {
88 // This changes with the color object
89 this._cachedMode = colorMode || RGB;
90
91 if(vals instanceof Color){
92 // Received Color object to be used for color mode conversion
93 const mode = colorMode ?
94 Color.colorMap[colorMode] :
95 Color.colorMap[vals.mode];
96 this._initialize = () => {
97 this._cachedColor = to(vals._color, mode);
98 this._cachedMode = mode;
99 };
100
101 }else if (typeof vals === 'object' && !Array.isArray(vals) && vals !== null){
102 // Received color.js object to be used internally
103 const mode = colorMode ?
104 Color.colorMap[colorMode] :
105 vals.spaceId;
106 this._initialize = () => {
107 this._cachedColor = to(vals, mode);
108 this._cachedMode = colorMode || Object.entries(Color.colorMap)
109 .find(([key, val]) => {
110 return val === this._cachedColor.spaceId;
111 });
112 };
113
114 } else if(typeof vals[0] === 'string') {
115 // Received string
116 this._defaultStringValue = vals[0];
117 this._initialize = () => {
118 try{
119 this._cachedColor = parse(vals[0]);
120 const [mode] = Object.entries(Color.colorMap).find(([key, val]) => {
121 return val === this._cachedColor.spaceId;
122 });
123 this._cachedMode = mode;
124 this._cachedColor = to(this._cachedColor, this._cachedColor.spaceId);
125 }catch(err){
126 // TODO: Invalid color string
127 throw new Error('Invalid color string');
128 }
129 };
130
131 }else{
132 // Received individual channel values
133 let mappedVals;
134
135 if(colorMaxes){
136 // NOTE: need to consider different number of arguments (eg. CMYK)
137 if(vals.length === 4){
138 mappedVals = Color.mapColorRange(vals, this._cachedMode, colorMaxes, clamp);
139 }else if(vals.length === 3){
140 mappedVals = Color.mapColorRange(
141 [vals[0], vals[1], vals[2]],
142 this._cachedMode,
143 colorMaxes,
144 clamp

Callers

nothing calls this directly

Calls 6

parseFunction · 0.85
mapFunction · 0.85
toHexComponentFunction · 0.85
mapColorRangeMethod · 0.80
sliceMethod · 0.80
pushMethod · 0.45

Tested by

no test coverage detected