1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
|
/*=========================================================================
Program: Visualization Toolkit
Module: TestNamedColors.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkNamedColors.h"
#include "vtkSmartPointer.h"
#include "vtkTestDriver.h"
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <cmath>
#include <sstream>
#include <string>
#include <vector>
const int NUMBER_OF_SYNONYMS = 81;
const int NUMBER_OF_COLORS = 283;
const int PRINT_SELF_STRING_SIZE = 9243;
// For comparing unsigned char converted to double precision.
const double EPS1 = 0.004; // 1/255 = 0.0039
// For comparing two double precision numbers.
const double EPS2 = 1.0e-9;
// Forward declaration of the test functions.
// A test to see if black is returned if the color name is empty.
bool TestEmptyColorName();
// A test to see if empty vectors are returned when the color name
// does not match a known one.
bool TestNoSuchColor(vtkStdString const & name);
// A test to see if returning an array matches the individual values.
bool TestUnsignedChar(vtkStdString const & name);
// A test to see if returning an array matches the individual values.
bool TestDouble(vtkStdString const & name);
// A test to see if returning an array matches the individual values.
// Alpha is ignored.
bool TestDoubleRGB(vtkStdString const & name);
// A test to see if the unsigned char conversion to double
// matches the double vector.
bool TestUCharToDouble(vtkStdString const & name);
// A test to see if adding a color works.
bool TestAddingAColor(vtkStdString name, const double dcolor[4],
const unsigned char ucolor[4]);
// Parse the color names returning a std::vector<std::string>
// colorNames is a string formatted with each name separated
// with a linefeed.
std::vector<vtkStdString> ParseColorNames(const vtkStdString & colorNames);
// Parse the synonyms returning a std::vector<std::vector<std::string> >
// synonyms is a string of synonyms separated by a double linefeed where
// each synonym is two or more color names separated by a linefeed
std::vector<std::vector<vtkStdString> > ParseSynonyms(
const vtkStdString & synonyms);
// A test to see if searching for synonyms works.
bool TestSearchForSynonyms();
// A test to see if parsing of HTML string color works.
bool TestHTMLColorToRGBA();
// A test to see if transforming a vtkColor3ub into an HTML color string works.
bool TestRGBToHTMLColor();
// A test to see if transforming a vtkColor4ub into an HTML color string works.
bool TestRGBAToHTMLColor();
//-----------------------------------------------------------------------------
bool TestEmptyColorName()
{
vtkSmartPointer<vtkNamedColors> nc = vtkSmartPointer<vtkNamedColors>::New();
vtkStdString name;
// Reference color
unsigned char rr, rg, rb;
rr = rb = rg = 0;
unsigned char ra = 255;
vtkColor4ub v = nc->GetColor4ub(name);
if ( v[0] != rr || v[1] != rg || v[2] != rb || v[3] != ra )
{
vtkGenericWarningMacro(
<< "Fail: an empty color name "
<< "returned an unsigned char color other than black."
);
return false;
}
vtkColor3ub v3 = nc->GetColor3ub(name);
if ( v3[0] != rr || v3[1] != rg || v3[2] != rb )
{
vtkGenericWarningMacro(
<< "Fail: an empty color name "
<< "returned an unsigned char color other than black."
);
return false;
}
unsigned char ur, ug, ub;
ur = ug = ub = 0;
unsigned char ua = 0;
nc->GetColor(name,ur,ug,ub,ua);
if ( ur != rr || ug != rg || ub != rb || ua != ra )
{
vtkGenericWarningMacro(
<< "Fail: an empty color name "
<< "returned an unsigned char color other than black."
);
return false;
}
// Reference color
double rrd, rgd, rbd;
rrd = rgd = rbd = 0;
double rad = 1;
vtkColor4d vd = nc->GetColor4d(name);
if ( vd[0] != rrd || vd[1] != rgd || vd[2] != rbd || vd[3] != rad )
{
vtkGenericWarningMacro(
<< "Fail: an empty color name "
<< "returned a double color other than black."
);
return false;
}
vtkColor3d vd3 = nc->GetColor3d(name);
if ( vd3[0] != rrd || vd3[1] != rgd || vd3[2] != rbd )
{
vtkGenericWarningMacro(
<< "Fail: an empty color name "
<< "returned a double color other than black."
);
return false;
}
double dr, dg, db;
dr = dg = db = 1;
double da = 0;
nc->GetColor(name,dr,dg,db,da);
if ( dr != rrd || dg != rgd || db != rbd || da != rad )
{
vtkGenericWarningMacro(
<< "Fail: an empty color name "
<< "returned an double color other than black."
);
return false;
}
nc->GetColor(name,dr,dg,db);
if ( ur != rrd || dg != rgd || db != rbd )
{
vtkGenericWarningMacro(
<< "Fail: an empty color name "
<< "returned an double color other than black."
);
return false;
}
return true;
}
//-----------------------------------------------------------------------------
bool TestNoSuchColor(vtkStdString const & name)
{
vtkSmartPointer<vtkNamedColors> nc = vtkSmartPointer<vtkNamedColors>::New();
if ( nc->ColorExists(name) )
{
vtkGenericWarningMacro(
<< "Fail: the color "
<< name << " exists when it shouldn't."
);
return false;
}
return true;
}
//-----------------------------------------------------------------------------
bool TestUnsignedChar(vtkStdString const & name)
{
vtkSmartPointer<vtkNamedColors> nc = vtkSmartPointer<vtkNamedColors>::New();
vtkColor4ub v = nc->GetColor4ub(name);
unsigned char cv[4];
nc->GetColor(name,cv);
bool sameElements = true;
for ( int i = 0; i < 4; ++i )
{
if ( v[i] != cv[i] )
{
sameElements &= false;
break;
}
}
if (!sameElements)
{
vtkGenericWarningMacro(
<< "Fail: arrays are not the same "
<< "for color: " << name
);
}
sameElements = true;
nc->GetColor(name,v);
for ( int i = 0; i < 4; ++i )
{
if ( v[i] != cv[i] )
{
sameElements &= false;
break;
}
}
if (!sameElements)
{
vtkGenericWarningMacro(
<< "Fail: arrays are not the same "
<< "for color: " << name
);
}
sameElements = true;
vtkColor3ub vv;
nc->GetColor(name,vv);
for ( int i = 0; i < 3; ++i )
{
if ( vv[i] != cv[i] )
{
sameElements &= false;
break;
}
}
if (!sameElements)
{
vtkGenericWarningMacro(
<< "Fail: arrays are not the same "
<< "for color: " << name
);
return false;
}
unsigned char red;
unsigned char green;
unsigned char blue;
unsigned char alpha;
nc->GetColor(name, red, green, blue, alpha);
if ( red != v[0] && blue != v[1] && green != v[2] && alpha != v[3] )
{
vtkGenericWarningMacro(
<< "Fail: One of red, green blue or alpha do not match the array "
<< "for color: " << name
);
return false;
}
return true;
}
//-----------------------------------------------------------------------------
bool TestDouble(vtkStdString const & name)
{
vtkSmartPointer<vtkNamedColors> nc = vtkSmartPointer<vtkNamedColors>::New();
vtkColor4d v = nc->GetColor4d(name);
double cv[4];
nc->GetColor(name,cv);
bool sameElements = true;
for ( int i = 0; i < 4; ++i )
{
if ( v[i] != cv[i] )
{
sameElements &= false;
break;
}
}
if (!sameElements)
{
vtkGenericWarningMacro(
<< "Fail: arrays are not the same "
<< "for color: " << name
);
}
sameElements = true;
nc->GetColor(name,v);
for ( int i = 0; i < 4; ++i )
{
if ( v[i] != cv[i] )
{
sameElements &= false;
break;
}
}
if (!sameElements)
{
vtkGenericWarningMacro(
<< "Fail: arrays are not the same "
<< "for color: " << name
);
}
sameElements = true;
vtkColor3d vv;
nc->GetColor(name,vv);
for ( int i = 0; i < 3; ++i )
{
if ( vv[i] != cv[i] )
{
sameElements &= false;
break;
}
}
if (!sameElements)
{
vtkGenericWarningMacro(
<< "Fail: arrays are not the same "
<< "for color: " << name
);
return false;
}
double red;
double green;
double blue;
double alpha;
nc->GetColor(name, red, green, blue, alpha);
if ( red != v[0] && blue != v[1] && green != v[2] && alpha != v[3] )
{
vtkGenericWarningMacro(
<< "Fail: One of red, green blue or alpha do not match the array "
<< "for color: " << name
);
return false;
}
return true;
}
//-----------------------------------------------------------------------------
bool TestDoubleRGB(vtkStdString const & name)
{
vtkSmartPointer<vtkNamedColors> nc = vtkSmartPointer<vtkNamedColors>::New();
vtkColor3d v = nc->GetColor3d(name);
double cv[3];
nc->GetColorRGB(name,cv);
bool sameElements = true;
for ( int i = 0; i < 3; ++i )
{
if ( v[i] != cv[i] )
{
sameElements &= false;
break;
}
}
if (!sameElements)
{
vtkGenericWarningMacro(
<< "Fail: arrays are not the same "
<< "for color: " << name
);
}
double red;
double green;
double blue;
nc->GetColor(name, red, green, blue);
if ( red != v[0] && blue != v[1] && green != v[2] )
{
vtkGenericWarningMacro(
<< "Fail: One of red, green or blue do not match the array "
<< "for color: " << name
);
return false;
}
return true;
}
//-----------------------------------------------------------------------------
bool TestUCharToDouble(vtkStdString const & name)
{
vtkSmartPointer<vtkNamedColors> nc = vtkSmartPointer<vtkNamedColors>::New();
vtkColor4ub vu = nc->GetColor4ub(name);
vtkColor4d vd = nc->GetColor4d(name);
double vdu[4];
for ( int i = 0; i < 4; ++i )
{
vdu[i] = static_cast<double>(vu[i]) / 255.0;
}
bool sameElements = true;
for ( int i = 0; i < 4; ++i )
{
if ( std::abs(vd[i] - vdu[i]) > EPS2 )
{
sameElements &= false;
break;
}
}
if (!sameElements)
{
vtkGenericWarningMacro(
<< "Fail: arrays are not the same "
<< "for color: " << name
);
}
return sameElements;
}
//-----------------------------------------------------------------------------
bool TestAddingAColor(vtkStdString name, const double dcolor[4],
const unsigned char ucolor[4])
{
vtkSmartPointer<vtkNamedColors> nc = vtkSmartPointer<vtkNamedColors>::New();
int sz = nc->GetNumberOfColors();
vtkColor4ub ub4(ucolor);
vtkColor4d d4(dcolor);
vtkColor3ub ub3;
vtkColor3d d3;
for(int i = 0; i < 3; ++i)
{
ub3[i] = ub4[i];
d3[i] = d4[i];
}
// Test for adding empty names.
nc->SetColor("",dcolor);
nc->SetColor("",dcolor[0],dcolor[1],dcolor[2],dcolor[3]);
if(sz != nc->GetNumberOfColors())
{
vtkGenericWarningMacro(
<< "Fail: Setting a double color with an empty name."
);
nc->ResetColors();
return false;
}
nc->SetColor("",ucolor);
nc->SetColor("",ucolor[0],ucolor[1],ucolor[2],ucolor[3]);
if(sz != nc->GetNumberOfColors())
{
vtkGenericWarningMacro(
<< "Fail: Setting an unsigned char color with an empty name."
);
nc->ResetColors();
return false;
}
nc->SetColor(name,dcolor);
vtkColor4ub vu = nc->GetColor4ub(name);
bool sameElements = true;
for ( int i = 0; i < 4; ++i )
{
if ( vu[i] != ucolor[i] )
{
sameElements &= false;
break;
}
}
if (!sameElements)
{
vtkGenericWarningMacro(
<< "Fail: Set as double get as unsigned char, colors do not match "
<< "for color: " << name
);
nc->ResetColors();
return false;
}
nc->SetColor(name,ucolor);
vtkColor4d vd = nc->GetColor4d(name);
sameElements = true;
for ( int i = 0; i < 4; ++i )
{
if ( std::abs(vd[i] - dcolor[i]) > EPS1 )
{
sameElements &= false;
}
}
if ( !sameElements )
{
vtkGenericWarningMacro(
<< "Fail: Set as unsigned char get as double, colors do not match "
<< "for color: " << name
);
nc->ResetColors();
return false;
}
// Set/Get as unsigned char.
unsigned char uc[4];
for( size_t i = 0; i < 4; ++i )
{
uc[i] = ucolor[i];
}
unsigned char ur, ug, ub, ua;
ur = ucolor[0];
ug = ucolor[1];
ub = ucolor[2];
ua = ucolor[3];
nc->SetColor(name,uc);
vu = nc->GetColor4ub(name);
sameElements = true;
for ( int i = 0; i < 4; ++i )
{
if ( vu[i] != uc[i] )
{
sameElements &= false;
}
}
if ( !sameElements )
{
vtkGenericWarningMacro(
<< "Fail: Set as unsigned char array get as vtkColor4ub, "
<< "colors do not match "
<< "for color: " << name
);
nc->ResetColors();
return false;
}
nc->SetColor(name,ub4);
vu = nc->GetColor4ub(name);
sameElements = true;
for ( int i = 0; i < 4; ++i )
{
if ( vu[i] != uc[i] )
{
sameElements &= false;
}
}
if ( !sameElements )
{
vtkGenericWarningMacro(
<< "Fail: Set as vtkColor4ub array get as vtkColor4ub, "
<< "colors do not match "
<< "for color: " << name
);
nc->ResetColors();
return false;
}
nc->SetColor(name,ub3);
vu = nc->GetColor4ub(name);
sameElements = true;
for ( int i = 0; i < 3; ++i )
{
if ( vu[i] != uc[i] )
{
sameElements &= false;
}
}
if ( !sameElements )
{
vtkGenericWarningMacro(
<< "Fail: Set as vtkColor3ub array get as vtkColor4ub, "
<< "colors do not match "
<< "for color: " << name
);
nc->ResetColors();
return false;
}
nc->SetColor(name,ur,ug,ub,ua);
vu = nc->GetColor4ub(name);
sameElements = true;
for ( int i = 0; i < 4; ++i )
{
if ( vu[i] != uc[i] )
{
sameElements &= false;
}
}
if ( !sameElements )
{
vtkGenericWarningMacro(
<< "Fail: Set as unsigned char values get as vtkColor4ub, "
<< "colors do not match "
<< "for color: " << name
);
nc->ResetColors();
return false;
}
// Set/Get as double.
double d[4];
for( size_t i = 0; i < 4; ++i )
{
d[i] = dcolor[i];
}
double dr, dg, db, da;
dr = dcolor[0];
dg = dcolor[1];
db = dcolor[2];
da = dcolor[3];
nc->SetColor(name,d);
vd = nc->GetColor4d(name);
sameElements = true;
for ( int i = 0; i < 4; ++i )
{
if ( std::abs(vd[i] - d[i]) > EPS2 )
{
sameElements &= false;
break;
}
}
if(!sameElements)
{
vtkGenericWarningMacro(
<< "Fail: Set as double array get as vtkColor4d, colors do not match "
<< "for color: " << name
);
nc->ResetColors();
return false;
}
nc->SetColor(name,d4);
vd = nc->GetColor4d(name);
sameElements = true;
for ( int i = 0; i < 4; ++i )
{
if ( std::abs(vd[i] - d[i]) > EPS2 )
{
sameElements &= false;
break;
}
}
if(!sameElements)
{
vtkGenericWarningMacro(
<< "Fail: Set as vtkColor4d get as vtkColor4d, colors do not match "
<< "for color: " << name
);
nc->ResetColors();
return false;
}
nc->SetColor(name,dr,dg,db,da);
vd = nc->GetColor4d(name);
sameElements = true;
for ( int i = 0; i < 4; ++i )
{
if ( std::abs(vd[i] - d[i]) > EPS2 )
{
sameElements &= false;
break;
}
}
if(!sameElements)
{
vtkGenericWarningMacro(
<< "Fail: Set as double values get as vtkColor4d, colors do not match "
<< "for color: " << name
);
nc->ResetColors();
return false;
}
nc->SetColor(name,d3);
vd = nc->GetColor4d(name);
sameElements = true;
for ( int i = 0; i < 3; ++i )
{
if ( std::abs(vd[i] - d[i]) > EPS2 )
{
sameElements &= false;
break;
}
}
if(!sameElements)
{
vtkGenericWarningMacro(
<< "Fail: Set as vtkColor3d get as vtkColor4d, colors do not match "
<< "for color: " << name
);
nc->ResetColors();
return false;
}
nc->RemoveColor(name);
sz = nc->GetNumberOfColors();
if (sz != NUMBER_OF_COLORS)
{
vtkGenericWarningMacro(
<< "Fail: Incorrect number of colors found, expected "
<< nc->GetNumberOfColors() << ", got "
<< NUMBER_OF_COLORS << " instead after inserting/deleting the color "
<< name
);
nc->ResetColors();
return false;
}
return true;
}
//-----------------------------------------------------------------------------
std::vector<vtkStdString> ParseColorNames(const vtkStdString & colorNames)
{
// The delimiter for a color.
const std::string colorDelimiter = "\n";
std::vector<vtkStdString> cn;
size_t start = 0;
size_t end = colorNames.find(colorDelimiter);
while(end != std::string::npos)
{
cn.push_back(colorNames.substr(start,end - start));
start = end + 1;
end = colorNames.find(colorDelimiter,start);
}
// Get the last color.
if (!colorNames.empty())
{
cn.push_back(colorNames.substr(start,colorNames.size() - start));
}
return cn;
}
//-----------------------------------------------------------------------------
std::vector<std::vector<vtkStdString> > ParseSynonyms(
const vtkStdString & synonyms)
{
// The delimiter for a string of synonyms.
const std::string synonymDelimiter = "\n\n";
size_t start = 0;
size_t end = synonyms.find("\n\n"); // The delimiter for a string of synonyms.
std::vector<vtkStdString> cn;
std::vector<std::vector<vtkStdString> > syn;
vtkStdString str;
while(end != std::string::npos)
{
str = synonyms.substr(start,end - start);
cn = ParseColorNames(str);
syn.push_back(cn);
start = end + 2;
end = synonyms.find(synonymDelimiter,start);
}
// Get the last set of synonyms.
if(!synonyms.empty())
{
str = synonyms.substr(start,end - start);
cn = ParseColorNames(str);
syn.push_back(cn);
}
// Sanity check!
//for(std::vector<std::vector<vtkStdString> >::const_iterator p =
// syn.begin(); p != syn.end(); ++p)
// {
// for(std::vector<vtkStdString>::const_iterator q =
// p->begin(); q != p->end(); ++q)
// {
// std::cout << *q << " ";
// }
// std::cout << std::endl;
// }
return syn;
}
//-----------------------------------------------------------------------------
bool TestSearchForSynonyms()
{
vtkSmartPointer<vtkNamedColors> nc = vtkSmartPointer<vtkNamedColors>::New();
std::vector<std::vector<vtkStdString> > synonyms =
ParseSynonyms(nc->GetSynonyms());
return static_cast<int>(synonyms.size()) == NUMBER_OF_SYNONYMS;
}
//-----------------------------------------------------------------------------
struct ColorDataMap
{
const char* colorString;
unsigned char colorVector[4];
};
bool TestHTMLColorToRGBA()
{
bool testResult = true;
static const ColorDataMap dataList[] = {
// Valid hexadecimal string.
{ "#000", {0, 0, 0, 255} },
{ "#70f", {0x77, 0x00, 0xFF, 0xFF} },
{ " #70f ", {0x77, 0x00, 0xFF, 0xFF} },
{ "#70faCC", {0x70, 0xFA, 0xCC, 0xFF} },
{ " #70faCC ", {0x70, 0xFA, 0xCC, 0xFF} },
// Non-valid hexadecimal string.
{ "#", {0, 0, 0, 0} },
{ "#70faC", {0, 0, 0, 0} },
{ "#70faCCF088", {0, 0, 0, 0} },
{ "# 70faCCF0", {0, 0, 0, 0} },
{ "#70 faCCF0", {0, 0, 0, 0} },
{ "#70f aCCF0", {0, 0, 0, 0} },
{ "#70faCC w", {0, 0, 0, 0} },
{ "#70faCw", {0, 0, 0, 0} },
{ "70facd", {0, 0, 0, 0} },
{ "#70fa", {0, 0, 0, 0} },
{ "#70faCCF0", {0, 0, 0, 0} },
{ " #70faCCF0 ", {0, 0, 0, 0} },
// Valid rgb() string.
{ "rgb(16, 0 , 250)", {16, 0, 250, 255} },
{ "RGB(16, 0 , 250)", {16, 0, 250, 255} },
{ "Rgb(16, 0 , 250)", {16, 0, 250, 255} },
{ "rgB(16, 0 , 250)", {16, 0, 250, 255} },
{ "rgb ( 020, 0 , 255 ) ", {20, 0, 255, 255} },
{ "rgb(20,0,255)", {20, 0, 255, 255} },
{ "rgb (20, 0 , 2558)", {20, 0, 255, 255} },
{ "rgb(0, 0 , 256)", {0, 0, 255, 255} },
{ "rgb(10, 0 , -2)", {10, 0, 0, 255} },
{ "rgb(10%, 0%, 100%)", {25, 0, 255, 255} },
{ "rgb ( 010%, 0% , 100% ) ", {25, 0, 255, 255} },
{ "rgb(10%,0%,100%)", {25, 0, 255, 255} },
{ "rgb (10%, 0%, 200%)", {25, 0, 255, 255} },
{ "rgb(0%, 0% , -2%)", {0, 0, 0, 255} },
{ "rgb(0%, 0% , 10.4%)", {0, 0, 26, 255} },
// Non-valid rgb() string.
{ "rgb (20, 0 , 25, 58)", {0, 0, 0, 0} },
{ "rgb (20, 0 , 25, 0.8)", {0, 0, 0, 0} },
{ "rgb ( ", {0, 0, 0, 0} },
{ "rgb (20,,25)", {0, 0, 0, 0} },
{ "rgb (, 20,25)", {0, 0, 0, 0} },
{ "rgb(10%%, 0%, 100%)", {0, 0, 0, 0} },
{ "rgb(10%, %, 100%)", {0, 0, 0, 0} },
{ "rgb(10%, 0%, 100 %)", {0, 0, 0, 0} },
{ "rgb(10%, 0%, 100% %)", {0, 0, 0, 0} },
{ "rgb(10%, 0%, 100%, 0.8)", {0, 0, 0, 0} },
{ "rgb(10%, 0%, 100, )", {0, 0, 0, 0} },
// Valid rgba() string.
{ "rgba ( 020, 0 , 255, 0 )", {20, 0, 255, 0} },
{ "rgba (20, 0 , 255, 1.0 ) ", {20, 0, 255, 255} },
{ "rgba(20, 0 , 255, 0.8)", {20, 0, 255, 204} },
{ "rgba(20, 0 , 255, 1.2)", {20, 0, 255, 255} },
{ "rgba(20, 0 , 255, -0.2)", {20, 0, 255, 0} },
{ "rgba(10%, 0%, 100%, 0.8)", {25, 0, 255, 204} },
// Non-valid rgba() string.
{ "rgba(20, 0 , 255)", {0, 0, 0, 0} },
{ "rgba(10%, 0%, 100, 0.8)", {0, 0, 0, 0} },
// Valid named color string.
{ "steelblue", {70, 130, 180, 255} },
// Non-valid color string.
{ "xcnvvb", {0, 0, 0, 0} },
{ "", {0, 0, 0, 0} },
// End element.
{ "\n", {0, 0, 0, 0} }
};
vtkSmartPointer<vtkNamedColors> color = vtkSmartPointer<vtkNamedColors>::New();
const char* inputString = "";
const unsigned char* expectedOutput;
vtkColor4ub outputColor;
unsigned int i = 0;
while (inputString[0] != '\n' )
{
inputString = dataList[i].colorString;
expectedOutput = dataList[i].colorVector;
outputColor = color->HTMLColorToRGBA(inputString);
if (outputColor != vtkColor4ub(expectedOutput))
{
vtkGenericWarningMacro(
<< "Fail: input `" << inputString << "`"
<< ", found " << outputColor
<< ", expected " << vtkColor4ub(expectedOutput) << " instead."
);
testResult &= false;
}
++i;
}
return testResult;
}
//-----------------------------------------------------------------------------
bool TestRGBToHTMLColor()
{
bool testResult = true;
static const ColorDataMap dataList[] = {
{ "#70facc", {0x70, 0xFA, 0xCC, 0xFF} },
{ "#00facc", {0x00, 0xFA, 0xCC, 0xFF} },
{ "#7000cc", {0x70, 0x00, 0xCC, 0xFF} },
{ "#70fa00", {0x70, 0xFA, 0x00, 0xFF} },
{ "#000000", {0x00, 0x00, 0x00, 0xFF} },
{ "#ffffff", {0xFF, 0xFF, 0xFF, 0xFF} },
// End element.
{ "\n", {0, 0, 0, 0} }
};
vtkSmartPointer<vtkNamedColors> color = vtkSmartPointer<vtkNamedColors>::New();
vtkStdString outputString;
const char* expectedOutput = "";
unsigned int i = 0;
while ( dataList[i].colorString[0] != '\n' )
{
vtkColor3ub inputColor(dataList[i].colorVector);
expectedOutput = dataList[i].colorString;
outputString = color->RGBToHTMLColor(inputColor);
if (outputString.compare(expectedOutput) != 0)
{
vtkGenericWarningMacro(
<< "Fail: input `" << inputColor << "`"
<< ", found '" << outputString
<< "', expected '" << expectedOutput << "' instead."
);
testResult &= false;
}
++i;
}
return testResult;
}
//-----------------------------------------------------------------------------
bool TestRGBAToHTMLColor()
{
bool testResult = true;
static const ColorDataMap dataList[] = {
{ "rgba(70,200,140,1)", {70, 200, 140, 255} },
{ "rgba(70,200,140,0)", {70, 200, 140, 0} },
{ "rgba(70,200,140,0.392)", {70, 200, 140, 100} },
{ "rgba(70,200,140,0.502)", {70, 200, 140, 128} },
{ "rgba(0,0,0,0.784)", {0, 0, 0, 200} },
{ "rgba(255,255,255,0)", {255, 255, 255, 0} },
{ "rgba(0,0,0,0.00392)", {0, 0, 0, 1} },
{ "rgba(0,0,0,0.996)", {0, 0, 0, 254} },
// End element.
{ "\n", {0, 0, 0, 0} }
};
vtkSmartPointer<vtkNamedColors> color = vtkSmartPointer<vtkNamedColors>::New();
vtkStdString outputString;
const char* expectedOutput = "";
unsigned int i = 0;
while ( dataList[i].colorString[0] != '\n' )
{
vtkColor4ub inputColor(dataList[i].colorVector);
expectedOutput = dataList[i].colorString;
outputString = color->RGBAToHTMLColor(inputColor);
if (outputString.compare(expectedOutput) != 0)
{
vtkGenericWarningMacro(
<< "Fail: input `" << inputColor << "`"
<< ", found '" << outputString
<< "', expected '" << expectedOutput << "' instead."
);
testResult &= false;
}
++i;
}
return testResult;
}
//-----------------------------------------------------------------------------
int TestNamedColors(int vtkNotUsed(argc), char* vtkNotUsed(argv)[])
{
vtkSmartPointer<vtkNamedColors> nc = vtkSmartPointer<vtkNamedColors>::New();
bool testResult = TestEmptyColorName();
if ( !testResult )
{
vtkGenericWarningMacro(
<< "Fail: TestNoSuchColor()"
);
}
testResult &= TestNoSuchColor("no_such_color"); // This color does not exist.
if ( !testResult )
{
vtkGenericWarningMacro(
<< "Fail: TestNoSuchColor()"
);
}
int counter = -1;
const int colorsToSkip = 20;
std::vector<vtkStdString> cn = ParseColorNames(nc->GetColorNames());
for ( std::vector<vtkStdString>::const_iterator
p = cn.begin(); p != cn.end(); ++p )
{
counter++;
// Skip some colors to make testing faster.
if ( counter % colorsToSkip != 0 )
{
continue;
}
if ( !TestUnsignedChar(*p) )
{
vtkGenericWarningMacro(
<< "Fail: TestUnsignedChar(), with color "
<< *p
);
testResult &= false;
}
if ( !TestDouble(*p) )
{
vtkGenericWarningMacro(
<< "Fail: TestDouble(), with color "
<< *p
);
testResult &= false;
}
if ( !TestDoubleRGB(*p) )
{
vtkGenericWarningMacro(
<< "Fail: TestDoubleRGB(), with color "
<< *p
);
testResult &= false;
}
if ( !TestUCharToDouble(*p) )
{
vtkGenericWarningMacro(
<< "Fail: TestUCharToDouble(), with color "
<< *p
);
testResult &= false;
}
}
unsigned char ucolor[4];
double dcolor[4];
vtkStdString name("Weird Color"); // Choose a name with spaces.
unsigned char ur = 51;
double r = 0.2;
for ( size_t i = 0; i < 3; ++i )
{
ucolor[i] = static_cast<unsigned char>(i+1) * ur;
dcolor[i] = (i+1) * r;
}
ucolor[3] = 0;
dcolor[3] = 0;
if ( !TestAddingAColor(name,dcolor,ucolor) )
{
vtkGenericWarningMacro(
<< "Fail: TestAddingAColor(), with color "
<< name
);
testResult &= false;
}
if ( !TestSearchForSynonyms() )
{
vtkGenericWarningMacro(
<< "Fail: TestSearchForSynonyms() - incorrect number of synonyms found, "
<< "expected "
<< NUMBER_OF_SYNONYMS << " instead."
);
testResult &= false;
}
if ( static_cast<int>(cn.size()) != NUMBER_OF_COLORS )
{
vtkGenericWarningMacro(
<< "Fail: Incorrect number of colors"
<< "found " <<
cn.size() << ", expected "
<< NUMBER_OF_COLORS << " instead."
);
testResult &= false;
}
nc->ResetColors();
if ( nc->GetNumberOfColors() != NUMBER_OF_COLORS )
{
vtkGenericWarningMacro(
<< "Fail: GetNumberOfColors(), incorrect number of colors"
<< "found " <<
cn.size() << ", expected "
<< NUMBER_OF_COLORS << " instead."
);
testResult &= false;
}
vtkSmartPointer<vtkStringArray> vs =
vtkSmartPointer<vtkStringArray>::New();
nc->GetColorNames(vs);
if ( vs->GetNumberOfValues() != NUMBER_OF_COLORS )
{
vtkGenericWarningMacro(
<< "Fail: GetColorNames(), incorrect number of colors"
<< "found " <<
vs->GetNumberOfValues() << ", expected "
<< NUMBER_OF_COLORS << " instead."
);
testResult &= false;
}
std::ostringstream os;
nc->PrintSelf(os,vtkIndent(2));
//std::cout << os.str() << std::endl;
if ( static_cast<int>(os.str().size()) != PRINT_SELF_STRING_SIZE )
{
vtkGenericWarningMacro(
<< "Fail: PrintSelf() - a string of size " <<
PRINT_SELF_STRING_SIZE << " was expected, got "
<< os.str().size() << " instead."
);
testResult &= false;
}
testResult &= TestHTMLColorToRGBA();
if ( !testResult )
{
vtkGenericWarningMacro(
<< "Fail: TestHTMLColorToRGBA()"
);
}
testResult &= TestRGBToHTMLColor();
if ( !testResult )
{
vtkGenericWarningMacro(
<< "Fail: TestRGBToHTMLColor()"
);
}
testResult &= TestRGBAToHTMLColor();
if ( !testResult )
{
vtkGenericWarningMacro(
<< "Fail: TestRGBAToHTMLColor()"
);
}
if ( !testResult )
{
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
|