| 649 | } |
| 650 | |
| 651 | color ColorAtom::getColor(string s) { |
| 652 | if (s.empty()) return _default; |
| 653 | trim(s); |
| 654 | // #AARRGGBB formatted color |
| 655 | if (s[0] == '#') return decode(s); |
| 656 | if (s.find(',') == string::npos) { |
| 657 | // find from predefined colors |
| 658 | auto it = _colors.find(tolower(s)); |
| 659 | if (it != _colors.end()) return it->second; |
| 660 | // AARRGGBB formatted color |
| 661 | if (s.find('.') == string::npos) return decode("#" + s); |
| 662 | // gray color |
| 663 | float x = 0.f; |
| 664 | valueof(s, x); |
| 665 | if (x != 0.f) { |
| 666 | float g = min(1.f, max(x, 0.f)); |
| 667 | return rgb(g, g, g); |
| 668 | } |
| 669 | return _default; |
| 670 | } |
| 671 | |
| 672 | auto en = string::npos; |
| 673 | strtokenizer toks(s, ";,"); |
| 674 | int n = toks.count_tokens(); |
| 675 | if (n == 3) { |
| 676 | // RGB model |
| 677 | string R = toks.next_token(); |
| 678 | string G = toks.next_token(); |
| 679 | string B = toks.next_token(); |
| 680 | |
| 681 | float r = 0.f, g = 0.f, b = 0.f; |
| 682 | valueof(trim(R), r); |
| 683 | valueof(trim(G), g); |
| 684 | valueof(trim(B), b); |
| 685 | |
| 686 | if (r == 0.f && g == 0.f && b == 0.f) return _default; |
| 687 | |
| 688 | if (r == (int)r && g == (int)g && b == (int)b && |
| 689 | R.find('.') == en && G.find('.') == en && B.find('.') == en) { |
| 690 | int ir = (int)min(255.f, max(0.f, r)); |
| 691 | int ig = (int)min(255.f, max(0.f, g)); |
| 692 | int ib = (int)min(255.f, max(0.f, b)); |
| 693 | return rgb(ir, ig, ib); |
| 694 | } |
| 695 | r = min(1.f, max(0.f, r)); |
| 696 | g = min(1.f, max(0.f, g)); |
| 697 | b = min(1.f, max(0.f, b)); |
| 698 | return rgb(r, g, b); |
| 699 | } else if (n == 4) { |
| 700 | // CMYK model |
| 701 | float c = 0.f, m = 0.f, y = 0.f, k = 0.f; |
| 702 | string C = toks.next_token(); |
| 703 | string M = toks.next_token(); |
| 704 | string Y = toks.next_token(); |
| 705 | string K = toks.next_token(); |
| 706 | valueof(trim(C), c); |
| 707 | valueof(trim(M), m); |
| 708 | valueof(trim(Y), y); |