Skip to content

Commit 2004c69

Browse files
add test file
1 parent b5955f1 commit 2004c69

1 file changed

Lines changed: 104 additions & 0 deletions

File tree

test/unit/webgl/p5.Shader.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,110 @@ test('returns numbers for builtin globals outside hooks and a strandNode when ca
631631
assert.approximately(middle[0], 128, 10);
632632
assert.approximately(right[0], 204, 10);
633633
});
634+
test('color() with hex string returns correct vec4 in strands', () => {
635+
myp5.createCanvas(50, 50, myp5.WEBGL);
636+
const testShader = myp5.baseMaterialShader().modify(() => {
637+
myp5.getPixelInputs(inputs => {
638+
const c = myp5.color('#ff0000');
639+
inputs.color = [c.x, c.y, c.z, 1.0];
640+
return inputs;
641+
});
642+
}, { myp5 });
643+
644+
myp5.noStroke();
645+
myp5.shader(testShader);
646+
myp5.plane(myp5.width, myp5.height);
647+
648+
const pixelColor = myp5.get(25, 25);
649+
assert.approximately(pixelColor[0], 255, 5);
650+
assert.approximately(pixelColor[1], 0, 5);
651+
assert.approximately(pixelColor[2], 0, 5);
652+
});
653+
654+
test('color() with CSS named color returns correct vec4 in strands', () => {
655+
myp5.createCanvas(50, 50, myp5.WEBGL);
656+
const testShader = myp5.baseMaterialShader().modify(() => {
657+
myp5.getPixelInputs(inputs => {
658+
const c = myp5.color('blue');
659+
inputs.color = [c.x, c.y, c.z, 1.0];
660+
return inputs;
661+
});
662+
}, { myp5 });
663+
664+
myp5.noStroke();
665+
myp5.shader(testShader);
666+
myp5.plane(myp5.width, myp5.height);
667+
668+
const pixelColor = myp5.get(25, 25);
669+
assert.approximately(pixelColor[0], 0, 5);
670+
assert.approximately(pixelColor[1], 0, 5);
671+
assert.approximately(pixelColor[2], 255, 5);
672+
});
673+
674+
test('lerpColor() interpolates between two colors in strands', () => {
675+
myp5.createCanvas(50, 50, myp5.WEBGL);
676+
const testShader = myp5.baseMaterialShader().modify(() => {
677+
myp5.getPixelInputs(inputs => {
678+
const c1 = myp5.color('#ff0000');
679+
const c2 = myp5.color('#0000ff');
680+
const mixed = myp5.lerpColor(c1, c2, 0.5);
681+
inputs.color = [mixed.x, mixed.y, mixed.z, 1.0];
682+
return inputs;
683+
});
684+
}, { myp5 });
685+
686+
myp5.noStroke();
687+
myp5.shader(testShader);
688+
myp5.plane(myp5.width, myp5.height);
689+
690+
const pixelColor = myp5.get(25, 25);
691+
assert.approximately(pixelColor[0], 128, 10);
692+
assert.approximately(pixelColor[1], 0, 5);
693+
assert.approximately(pixelColor[2], 128, 10);
694+
});
695+
696+
test('red(), green(), blue(), alpha() extract correct channels in strands', () => {
697+
myp5.createCanvas(50, 50, myp5.WEBGL);
698+
const testShader = myp5.baseMaterialShader().modify(() => {
699+
myp5.getPixelInputs(inputs => {
700+
const c = myp5.color('#ff8000');
701+
const r = myp5.red(c);
702+
const g = myp5.green(c);
703+
const b = myp5.blue(c);
704+
inputs.color = [r, g, b, 1.0];
705+
return inputs;
706+
});
707+
}, { myp5 });
708+
709+
myp5.noStroke();
710+
myp5.shader(testShader);
711+
myp5.plane(myp5.width, myp5.height);
712+
713+
const pixelColor = myp5.get(25, 25);
714+
assert.approximately(pixelColor[0], 255, 5);
715+
assert.approximately(pixelColor[1], 128, 10);
716+
assert.approximately(pixelColor[2], 0, 5);
717+
});
718+
719+
test('hue() returns normalized hue value in strands', () => {
720+
myp5.createCanvas(50, 50, myp5.WEBGL);
721+
const testShader = myp5.baseMaterialShader().modify(() => {
722+
myp5.getPixelInputs(inputs => {
723+
// Pure red has hue 0
724+
const c = myp5.color('#ff0000');
725+
const h = myp5.hue(c);
726+
inputs.color = [h, h, h, 1.0];
727+
return inputs;
728+
});
729+
}, { myp5 });
730+
731+
myp5.noStroke();
732+
myp5.shader(testShader);
733+
myp5.plane(myp5.width, myp5.height);
734+
735+
const pixelColor = myp5.get(25, 25);
736+
assert.approximately(pixelColor[0], 0, 5);
737+
});
634738

635739
test('handle custom uniform names with automatic values', () => {
636740
myp5.createCanvas(50, 50, myp5.WEBGL);

0 commit comments

Comments
 (0)