@@ -484,67 +484,53 @@ _hextoint(char *hex, Uint8 *val)
484
484
static tristate
485
485
_hexcolor (PyObject * color , Uint8 rgba [])
486
486
{
487
- size_t len ;
488
- tristate rcode = TRISTATE_FAIL ;
489
- char * name ;
490
- PyObject * ascii = PyUnicode_AsASCIIString (color );
491
- if (ascii == NULL ) {
492
- rcode = TRISTATE_ERROR ;
493
- goto Fail ;
494
- }
495
- name = PyBytes_AsString (ascii );
487
+ Py_ssize_t len ;
488
+ char * name = (char * )PyUnicode_AsUTF8AndSize (color , & len );
496
489
if (name == NULL ) {
497
- goto Fail ;
490
+ return TRISTATE_ERROR ;
498
491
}
499
492
500
- len = strlen (name );
501
493
/* hex colors can be
502
494
* #RRGGBB
503
495
* #RRGGBBAA
504
496
* 0xRRGGBB
505
497
* 0xRRGGBBAA
506
498
*/
507
499
if (len < 7 ) {
508
- goto Fail ;
500
+ return TRISTATE_FAIL ;
509
501
}
510
502
511
503
if (name [0 ] == '#' ) {
512
504
if (len != 7 && len != 9 )
513
- goto Fail ;
505
+ return TRISTATE_FAIL ;
514
506
if (!_hextoint (name + 1 , & rgba [0 ]))
515
- goto Fail ;
507
+ return TRISTATE_FAIL ;
516
508
if (!_hextoint (name + 3 , & rgba [1 ]))
517
- goto Fail ;
509
+ return TRISTATE_FAIL ;
518
510
if (!_hextoint (name + 5 , & rgba [2 ]))
519
- goto Fail ;
511
+ return TRISTATE_FAIL ;
520
512
rgba [3 ] = 255 ;
521
513
if (len == 9 && !_hextoint (name + 7 , & rgba [3 ])) {
522
- goto Fail ;
514
+ return TRISTATE_FAIL ;
523
515
}
524
- goto Success ;
516
+ return TRISTATE_SUCCESS ;
525
517
}
526
518
else if (name [0 ] == '0' && name [1 ] == 'x' ) {
527
519
if (len != 8 && len != 10 )
528
- goto Fail ;
520
+ return TRISTATE_FAIL ;
529
521
if (!_hextoint (name + 2 , & rgba [0 ]))
530
- goto Fail ;
522
+ return TRISTATE_FAIL ;
531
523
if (!_hextoint (name + 4 , & rgba [1 ]))
532
- goto Fail ;
524
+ return TRISTATE_FAIL ;
533
525
if (!_hextoint (name + 6 , & rgba [2 ]))
534
- goto Fail ;
526
+ return TRISTATE_FAIL ;
535
527
rgba [3 ] = 255 ;
536
528
if (len == 10 && !_hextoint (name + 8 , & rgba [3 ])) {
537
- goto Fail ;
529
+ return TRISTATE_FAIL ;
538
530
}
539
- goto Success ;
531
+ return TRISTATE_SUCCESS ;
540
532
}
541
- goto Fail ;
542
-
543
- Success :
544
- rcode = TRISTATE_SUCCESS ;
545
- Fail :
546
- Py_XDECREF (ascii );
547
- return rcode ;
533
+ return TRISTATE_FAIL ;
548
534
}
549
535
550
536
static int
@@ -601,6 +587,17 @@ _parse_color_from_text(PyObject *str_obj, Uint8 *rgba)
601
587
color = PyDict_GetItem (_COLORDICT ,
602
588
str_obj ); // optimize for correct color names
603
589
if (!color ) {
590
+ switch (_hexcolor (str_obj , rgba )) {
591
+ case TRISTATE_FAIL :
592
+ /* Do re-handling of colordict path below */
593
+ break ;
594
+ case TRISTATE_ERROR :
595
+ /* Some python error raised, so forward it */
596
+ return -1 ;
597
+ default :
598
+ /* rgba is set, we are done here */
599
+ return 0 ;
600
+ }
604
601
name1 = PyObject_CallMethod (str_obj , "replace" , "(ss)" , " " , "" );
605
602
if (!name1 ) {
606
603
return -1 ;
@@ -613,15 +610,8 @@ _parse_color_from_text(PyObject *str_obj, Uint8 *rgba)
613
610
color = PyDict_GetItem (_COLORDICT , name2 );
614
611
Py_DECREF (name2 );
615
612
if (!color ) {
616
- switch (_hexcolor (str_obj , rgba )) {
617
- case TRISTATE_FAIL :
618
- PyErr_SetString (PyExc_ValueError , "invalid color name" );
619
- return -1 ;
620
- case TRISTATE_ERROR :
621
- return -1 ;
622
- default :
623
- return 0 ;
624
- }
613
+ PyErr_SetString (PyExc_ValueError , "invalid color name" );
614
+ return -1 ;
625
615
}
626
616
}
627
617
0 commit comments