@@ -791,15 +791,58 @@ suite('p5.Color', function() {
791791 } ) ;
792792 } ) ;
793793
794- suite . todo ( 'p5.Color.prototype.toString' , function ( ) {
795- // var colorStr;
794+ suite ( 'p5.Color.prototype.toString' , function ( ) {
795+ suite ( '#rrggbb format' , function ( ) {
796+ test ( 'should expand short hex (#rgb) to full 6-digit format' , function ( ) {
797+ mockP5Prototype . colorMode ( mockP5Prototype . RGB , 255 , 255 , 255 , 255 ) ;
798+ const c = mockP5Prototype . color ( '#f06' ) ;
799+ const result = c . toString ( '#rrggbb' ) ;
800+ assert . equal ( result , '#ff0066' ) ;
801+ } ) ;
802+
803+ test ( 'should truncate hex with alpha (#rrggbbaa) to 6 digits' , function ( ) {
804+ mockP5Prototype . colorMode ( mockP5Prototype . RGB , 255 , 255 , 255 , 255 ) ;
805+ const c = mockP5Prototype . color ( 255 , 0 , 102 , 128 ) ;
806+ const result = c . toString ( '#rrggbb' ) ;
807+ assert . equal ( result . length , 7 ) ;
808+ assert . equal ( result , '#ff0066' ) ;
809+ } ) ;
810+
811+ test ( 'should output lowercase hex string' , function ( ) {
812+ mockP5Prototype . colorMode ( mockP5Prototype . RGB , 255 , 255 , 255 , 255 ) ;
813+ const c = mockP5Prototype . color ( 255 , 170 , 187 ) ;
814+ const result = c . toString ( '#rrggbb' ) ;
815+ assert . equal ( result , result . toLowerCase ( ) ) ;
816+ assert . equal ( result , '#ffaabb' ) ;
817+ } ) ;
818+
819+ test ( 'should correctly format standard RGB colors' , function ( ) {
820+ mockP5Prototype . colorMode ( mockP5Prototype . RGB , 255 , 255 , 255 , 255 ) ;
821+ const c = mockP5Prototype . color ( 255 , 0 , 102 ) ;
822+ const result = c . toString ( '#rrggbb' ) ;
823+ assert . equal ( result , '#ff0066' ) ;
824+ } ) ;
825+
826+ test ( 'should correctly format black color' , function ( ) {
827+ mockP5Prototype . colorMode ( mockP5Prototype . RGB , 255 , 255 , 255 , 255 ) ;
828+ const c = mockP5Prototype . color ( 0 , 0 , 0 ) ;
829+ const result = c . toString ( '#rrggbb' ) ;
830+ assert . equal ( result , '#000000' ) ;
831+ } ) ;
796832
797- // beforeEach(function() {
798- // mockP5Prototype.colorMode(mockP5Prototype.RGB, 255, 255, 255, 255);
799- // c = mockP5Prototype.color(128, 0, 128, 128);
800- // colorStr = c.toString();
801- // });
833+ test ( 'should correctly format white color' , function ( ) {
834+ mockP5Prototype . colorMode ( mockP5Prototype . RGB , 255 , 255 , 255 , 255 ) ;
835+ const c = mockP5Prototype . color ( 255 , 255 , 255 ) ;
836+ const result = c . toString ( '#rrggbb' ) ;
837+ assert . equal ( result , '#ffffff' ) ;
838+ } ) ;
802839
803- // NOTE: need some tests here
840+ test ( 'should handle colors created from 6-digit hex string' , function ( ) {
841+ mockP5Prototype . colorMode ( mockP5Prototype . RGB , 255 , 255 , 255 , 255 ) ;
842+ const c = mockP5Prototype . color ( '#9932cc' ) ;
843+ const result = c . toString ( '#rrggbb' ) ;
844+ assert . equal ( result , '#9932cc' ) ;
845+ } ) ;
846+ } ) ;
804847 } ) ;
805848} ) ;
0 commit comments