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
|
//----------------------------------------------------------------------------
// Anti-Grain Geometry - Version 2.4
// Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
//
// Permission to copy, use, modify, sell and distribute this software
// is granted provided this copyright notice appears in all copies.
// This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.
//
//----------------------------------------------------------------------------
// Contact: mcseem@antigrain.com
// mcseemagg@yahoo.com
// http://www.antigrain.com
//----------------------------------------------------------------------------
//
// Adaptation for high precision colors has been sponsored by
// Liberty Technology Systems, Inc., visit http://lib-sys.com
//
// Liberty Technology Systems, Inc. is the provider of
// PostScript and PDF technology for software developers.
//
//----------------------------------------------------------------------------
//
// color types gray8, gray16
//
//----------------------------------------------------------------------------
#ifndef AGG_COLOR_GRAY_INCLUDED
#define AGG_COLOR_GRAY_INCLUDED
#include "agg_basics.h"
#include "agg_color_rgba.h"
namespace agg
{
//===================================================================gray8
template<class Colorspace>
struct gray8T
{
typedef int8u value_type;
typedef int32u calc_type;
typedef int32 long_type;
enum base_scale_e
{
base_shift = 8,
base_scale = 1 << base_shift,
base_mask = base_scale - 1,
base_MSB = 1 << (base_shift - 1)
};
typedef gray8T self_type;
value_type v;
value_type a;
static value_type luminance(const rgba& c)
{
// Calculate grayscale value as per ITU-R BT.709.
return value_type(uround((0.2126 * c.r + 0.7152 * c.g + 0.0722 * c.b) * base_mask));
}
static value_type luminance(const rgba8& c)
{
// Calculate grayscale value as per ITU-R BT.709.
return value_type((55u * c.r + 184u * c.g + 18u * c.b) >> 8);
}
static void convert(gray8T<linear>& dst, const gray8T<sRGB>& src)
{
dst.v = sRGB_conv<value_type>::rgb_from_sRGB(src.v);
dst.a = src.a;
}
static void convert(gray8T<sRGB>& dst, const gray8T<linear>& src)
{
dst.v = sRGB_conv<value_type>::rgb_to_sRGB(src.v);
dst.a = src.a;
}
static void convert(gray8T<linear>& dst, const rgba8& src)
{
dst.v = luminance(src);
dst.a = src.a;
}
static void convert(gray8T<linear>& dst, const srgba8& src)
{
// The RGB weights are only valid for linear values.
convert(dst, rgba8(src));
}
static void convert(gray8T<sRGB>& dst, const rgba8& src)
{
dst.v = sRGB_conv<value_type>::rgb_to_sRGB(luminance(src));
dst.a = src.a;
}
static void convert(gray8T<sRGB>& dst, const srgba8& src)
{
// The RGB weights are only valid for linear values.
convert(dst, rgba8(src));
}
//--------------------------------------------------------------------
gray8T() {}
//--------------------------------------------------------------------
explicit gray8T(unsigned v_, unsigned a_ = base_mask) :
v(int8u(v_)), a(int8u(a_)) {}
//--------------------------------------------------------------------
gray8T(const self_type& c, unsigned a_) :
v(c.v), a(value_type(a_)) {}
//--------------------------------------------------------------------
gray8T(const rgba& c) :
v(luminance(c)),
a(value_type(uround(c.a * base_mask))) {}
//--------------------------------------------------------------------
template<class T>
gray8T(const gray8T<T>& c)
{
convert(*this, c);
}
//--------------------------------------------------------------------
template<class T>
gray8T(const rgba8T<T>& c)
{
convert(*this, c);
}
//--------------------------------------------------------------------
template<class T>
T convert_from_sRGB() const
{
typename T::value_type y = sRGB_conv<typename T::value_type>::rgb_from_sRGB(v);
return T(y, y, y, sRGB_conv<typename T::value_type>::alpha_from_sRGB(a));
}
template<class T>
T convert_to_sRGB() const
{
typename T::value_type y = sRGB_conv<typename T::value_type>::rgb_to_sRGB(v);
return T(y, y, y, sRGB_conv<typename T::value_type>::alpha_to_sRGB(a));
}
//--------------------------------------------------------------------
rgba8 make_rgba8(const linear&) const
{
return rgba8(v, v, v, a);
}
rgba8 make_rgba8(const sRGB&) const
{
return convert_from_sRGB<srgba8>();
}
operator rgba8() const
{
return make_rgba8(Colorspace());
}
//--------------------------------------------------------------------
srgba8 make_srgba8(const linear&) const
{
return convert_to_sRGB<rgba8>();
}
srgba8 make_srgba8(const sRGB&) const
{
return srgba8(v, v, v, a);
}
operator srgba8() const
{
return make_rgba8(Colorspace());
}
//--------------------------------------------------------------------
rgba16 make_rgba16(const linear&) const
{
rgba16::value_type rgb = (v << 8) | v;
return rgba16(rgb, rgb, rgb, (a << 8) | a);
}
rgba16 make_rgba16(const sRGB&) const
{
return convert_from_sRGB<rgba16>();
}
operator rgba16() const
{
return make_rgba16(Colorspace());
}
//--------------------------------------------------------------------
rgba32 make_rgba32(const linear&) const
{
rgba32::value_type v32 = v / 255.0f;
return rgba32(v32, v32, v32, a / 255.0f);
}
rgba32 make_rgba32(const sRGB&) const
{
return convert_from_sRGB<rgba32>();
}
operator rgba32() const
{
return make_rgba32(Colorspace());
}
//--------------------------------------------------------------------
static AGG_INLINE double to_double(value_type a)
{
return double(a) / base_mask;
}
//--------------------------------------------------------------------
static AGG_INLINE value_type from_double(double a)
{
return value_type(uround(a * base_mask));
}
//--------------------------------------------------------------------
static AGG_INLINE value_type empty_value()
{
return 0;
}
//--------------------------------------------------------------------
static AGG_INLINE value_type full_value()
{
return base_mask;
}
//--------------------------------------------------------------------
AGG_INLINE bool is_transparent() const
{
return a == 0;
}
//--------------------------------------------------------------------
AGG_INLINE bool is_opaque() const
{
return a == base_mask;
}
//--------------------------------------------------------------------
// Fixed-point multiply, exact over int8u.
static AGG_INLINE value_type multiply(value_type a, value_type b)
{
calc_type t = a * b + base_MSB;
return value_type(((t >> base_shift) + t) >> base_shift);
}
//--------------------------------------------------------------------
static AGG_INLINE value_type demultiply(value_type a, value_type b)
{
if (a * b == 0)
{
return 0;
}
else if (a >= b)
{
return base_mask;
}
else return value_type((a * base_mask + (b >> 1)) / b);
}
//--------------------------------------------------------------------
template<typename T>
static AGG_INLINE T downscale(T a)
{
return a >> base_shift;
}
//--------------------------------------------------------------------
template<typename T>
static AGG_INLINE T downshift(T a, unsigned n)
{
return a >> n;
}
//--------------------------------------------------------------------
// Fixed-point multiply, exact over int8u.
// Specifically for multiplying a color component by a cover.
static AGG_INLINE value_type mult_cover(value_type a, value_type b)
{
return multiply(a, b);
}
//--------------------------------------------------------------------
static AGG_INLINE cover_type scale_cover(cover_type a, value_type b)
{
return multiply(b, a);
}
//--------------------------------------------------------------------
// Interpolate p to q by a, assuming q is premultiplied by a.
static AGG_INLINE value_type prelerp(value_type p, value_type q, value_type a)
{
return p + q - multiply(p, a);
}
//--------------------------------------------------------------------
// Interpolate p to q by a.
static AGG_INLINE value_type lerp(value_type p, value_type q, value_type a)
{
int t = (q - p) * a + base_MSB - (p > q);
return value_type(p + (((t >> base_shift) + t) >> base_shift));
}
//--------------------------------------------------------------------
self_type& clear()
{
v = a = 0;
return *this;
}
//--------------------------------------------------------------------
self_type& transparent()
{
a = 0;
return *this;
}
//--------------------------------------------------------------------
self_type& opacity(double a_)
{
if (a_ < 0) a = 0;
else if (a_ > 1) a = 1;
else a = (value_type)uround(a_ * double(base_mask));
return *this;
}
//--------------------------------------------------------------------
double opacity() const
{
return double(a) / double(base_mask);
}
//--------------------------------------------------------------------
self_type& premultiply()
{
if (a < base_mask)
{
if (a == 0) v = 0;
else v = multiply(v, a);
}
return *this;
}
//--------------------------------------------------------------------
self_type& demultiply()
{
if (a < base_mask)
{
if (a == 0)
{
v = 0;
}
else
{
calc_type v_ = (calc_type(v) * base_mask) / a;
v = value_type((v_ > base_mask) ? (value_type)base_mask : v_);
}
}
return *this;
}
//--------------------------------------------------------------------
self_type gradient(self_type c, double k) const
{
self_type ret;
calc_type ik = uround(k * base_scale);
ret.v = lerp(v, c.v, ik);
ret.a = lerp(a, c.a, ik);
return ret;
}
//--------------------------------------------------------------------
AGG_INLINE void add(const self_type& c, unsigned cover)
{
calc_type cv, ca;
if (cover == cover_mask)
{
if (c.a == base_mask)
{
*this = c;
return;
}
else
{
cv = v + c.v;
ca = a + c.a;
}
}
else
{
cv = v + mult_cover(c.v, cover);
ca = a + mult_cover(c.a, cover);
}
v = (value_type)((cv > calc_type(base_mask)) ? calc_type(base_mask) : cv);
a = (value_type)((ca > calc_type(base_mask)) ? calc_type(base_mask) : ca);
}
//--------------------------------------------------------------------
static self_type no_color() { return self_type(0,0); }
};
typedef gray8T<linear> gray8;
typedef gray8T<sRGB> sgray8;
//==================================================================gray16
struct gray16
{
typedef int16u value_type;
typedef int32u calc_type;
typedef int64 long_type;
enum base_scale_e
{
base_shift = 16,
base_scale = 1 << base_shift,
base_mask = base_scale - 1,
base_MSB = 1 << (base_shift - 1)
};
typedef gray16 self_type;
value_type v;
value_type a;
static value_type luminance(const rgba& c)
{
// Calculate grayscale value as per ITU-R BT.709.
return value_type(uround((0.2126 * c.r + 0.7152 * c.g + 0.0722 * c.b) * base_mask));
}
static value_type luminance(const rgba16& c)
{
// Calculate grayscale value as per ITU-R BT.709.
return value_type((13933u * c.r + 46872u * c.g + 4732u * c.b) >> 16);
}
static value_type luminance(const rgba8& c)
{
return luminance(rgba16(c));
}
static value_type luminance(const srgba8& c)
{
return luminance(rgba16(c));
}
static value_type luminance(const rgba32& c)
{
return luminance(rgba(c));
}
//--------------------------------------------------------------------
gray16() {}
//--------------------------------------------------------------------
explicit gray16(unsigned v_, unsigned a_ = base_mask) :
v(int16u(v_)), a(int16u(a_)) {}
//--------------------------------------------------------------------
gray16(const self_type& c, unsigned a_) :
v(c.v), a(value_type(a_)) {}
//--------------------------------------------------------------------
gray16(const rgba& c) :
v(luminance(c)),
a((value_type)uround(c.a * double(base_mask))) {}
//--------------------------------------------------------------------
gray16(const rgba8& c) :
v(luminance(c)),
a((value_type(c.a) << 8) | c.a) {}
//--------------------------------------------------------------------
gray16(const srgba8& c) :
v(luminance(c)),
a((value_type(c.a) << 8) | c.a) {}
//--------------------------------------------------------------------
gray16(const rgba16& c) :
v(luminance(c)),
a(c.a) {}
//--------------------------------------------------------------------
gray16(const gray8& c) :
v((value_type(c.v) << 8) | c.v),
a((value_type(c.a) << 8) | c.a) {}
//--------------------------------------------------------------------
gray16(const sgray8& c) :
v(sRGB_conv<value_type>::rgb_from_sRGB(c.v)),
a(sRGB_conv<value_type>::alpha_from_sRGB(c.a)) {}
//--------------------------------------------------------------------
operator rgba8() const
{
return rgba8(v >> 8, v >> 8, v >> 8, a >> 8);
}
//--------------------------------------------------------------------
operator srgba8() const
{
value_type y = sRGB_conv<value_type>::rgb_to_sRGB(v);
return srgba8(y, y, y, sRGB_conv<value_type>::alpha_to_sRGB(a));
}
//--------------------------------------------------------------------
operator rgba16() const
{
return rgba16(v, v, v, a);
}
//--------------------------------------------------------------------
operator rgba32() const
{
rgba32::value_type v32 = v / 65535.0f;
return rgba32(v32, v32, v32, a / 65535.0f);
}
//--------------------------------------------------------------------
operator gray8() const
{
return gray8(v >> 8, a >> 8);
}
//--------------------------------------------------------------------
operator sgray8() const
{
return sgray8(
sRGB_conv<value_type>::rgb_to_sRGB(v),
sRGB_conv<value_type>::alpha_to_sRGB(a));
}
//--------------------------------------------------------------------
static AGG_INLINE double to_double(value_type a)
{
return double(a) / base_mask;
}
//--------------------------------------------------------------------
static AGG_INLINE value_type from_double(double a)
{
return value_type(uround(a * base_mask));
}
//--------------------------------------------------------------------
static AGG_INLINE value_type empty_value()
{
return 0;
}
//--------------------------------------------------------------------
static AGG_INLINE value_type full_value()
{
return base_mask;
}
//--------------------------------------------------------------------
AGG_INLINE bool is_transparent() const
{
return a == 0;
}
//--------------------------------------------------------------------
AGG_INLINE bool is_opaque() const
{
return a == base_mask;
}
//--------------------------------------------------------------------
// Fixed-point multiply, exact over int16u.
static AGG_INLINE value_type multiply(value_type a, value_type b)
{
calc_type t = a * b + base_MSB;
return value_type(((t >> base_shift) + t) >> base_shift);
}
//--------------------------------------------------------------------
static AGG_INLINE value_type demultiply(value_type a, value_type b)
{
if (a * b == 0)
{
return 0;
}
else if (a >= b)
{
return base_mask;
}
else return value_type((a * base_mask + (b >> 1)) / b);
}
//--------------------------------------------------------------------
template<typename T>
static AGG_INLINE T downscale(T a)
{
return a >> base_shift;
}
//--------------------------------------------------------------------
template<typename T>
static AGG_INLINE T downshift(T a, unsigned n)
{
return a >> n;
}
//--------------------------------------------------------------------
// Fixed-point multiply, almost exact over int16u.
// Specifically for multiplying a color component by a cover.
static AGG_INLINE value_type mult_cover(value_type a, cover_type b)
{
return multiply(a, b << 8 | b);
}
//--------------------------------------------------------------------
static AGG_INLINE cover_type scale_cover(cover_type a, value_type b)
{
return mult_cover(b, a) >> 8;
}
//--------------------------------------------------------------------
// Interpolate p to q by a, assuming q is premultiplied by a.
static AGG_INLINE value_type prelerp(value_type p, value_type q, value_type a)
{
return p + q - multiply(p, a);
}
//--------------------------------------------------------------------
// Interpolate p to q by a.
static AGG_INLINE value_type lerp(value_type p, value_type q, value_type a)
{
int t = (q - p) * a + base_MSB - (p > q);
return value_type(p + (((t >> base_shift) + t) >> base_shift));
}
//--------------------------------------------------------------------
self_type& clear()
{
v = a = 0;
return *this;
}
//--------------------------------------------------------------------
self_type& transparent()
{
a = 0;
return *this;
}
//--------------------------------------------------------------------
self_type& opacity(double a_)
{
if (a_ < 0) a = 0;
else if(a_ > 1) a = 1;
else a = (value_type)uround(a_ * double(base_mask));
return *this;
}
//--------------------------------------------------------------------
double opacity() const
{
return double(a) / double(base_mask);
}
//--------------------------------------------------------------------
self_type& premultiply()
{
if (a < base_mask)
{
if(a == 0) v = 0;
else v = multiply(v, a);
}
return *this;
}
//--------------------------------------------------------------------
self_type& demultiply()
{
if (a < base_mask)
{
if (a == 0)
{
v = 0;
}
else
{
calc_type v_ = (calc_type(v) * base_mask) / a;
v = value_type((v_ > base_mask) ? base_mask : v_);
}
}
return *this;
}
//--------------------------------------------------------------------
self_type gradient(self_type c, double k) const
{
self_type ret;
calc_type ik = uround(k * base_scale);
ret.v = lerp(v, c.v, ik);
ret.a = lerp(a, c.a, ik);
return ret;
}
//--------------------------------------------------------------------
AGG_INLINE void add(const self_type& c, unsigned cover)
{
calc_type cv, ca;
if (cover == cover_mask)
{
if (c.a == base_mask)
{
*this = c;
return;
}
else
{
cv = v + c.v;
ca = a + c.a;
}
}
else
{
cv = v + mult_cover(c.v, cover);
ca = a + mult_cover(c.a, cover);
}
v = (value_type)((cv > calc_type(base_mask)) ? calc_type(base_mask) : cv);
a = (value_type)((ca > calc_type(base_mask)) ? calc_type(base_mask) : ca);
}
//--------------------------------------------------------------------
static self_type no_color() { return self_type(0,0); }
};
//===================================================================gray32
struct gray32
{
typedef float value_type;
typedef double calc_type;
typedef double long_type;
typedef gray32 self_type;
value_type v;
value_type a;
// Calculate grayscale value as per ITU-R BT.709.
static value_type luminance(double r, double g, double b)
{
return value_type(0.2126 * r + 0.7152 * g + 0.0722 * b);
}
static value_type luminance(const rgba& c)
{
return luminance(c.r, c.g, c.b);
}
static value_type luminance(const rgba32& c)
{
return luminance(c.r, c.g, c.b);
}
static value_type luminance(const rgba8& c)
{
return luminance(c.r / 255.0, c.g / 255.0, c.g / 255.0);
}
static value_type luminance(const rgba16& c)
{
return luminance(c.r / 65535.0, c.g / 65535.0, c.g / 65535.0);
}
//--------------------------------------------------------------------
gray32() {}
//--------------------------------------------------------------------
explicit gray32(value_type v_, value_type a_ = 1) :
v(v_), a(a_) {}
//--------------------------------------------------------------------
gray32(const self_type& c, value_type a_) :
v(c.v), a(a_) {}
//--------------------------------------------------------------------
gray32(const rgba& c) :
v(luminance(c)),
a(value_type(c.a)) {}
//--------------------------------------------------------------------
gray32(const rgba8& c) :
v(luminance(c)),
a(value_type(c.a / 255.0)) {}
//--------------------------------------------------------------------
gray32(const srgba8& c) :
v(luminance(rgba32(c))),
a(value_type(c.a / 255.0)) {}
//--------------------------------------------------------------------
gray32(const rgba16& c) :
v(luminance(c)),
a(value_type(c.a / 65535.0)) {}
//--------------------------------------------------------------------
gray32(const rgba32& c) :
v(luminance(c)),
a(value_type(c.a)) {}
//--------------------------------------------------------------------
gray32(const gray8& c) :
v(value_type(c.v / 255.0)),
a(value_type(c.a / 255.0)) {}
//--------------------------------------------------------------------
gray32(const sgray8& c) :
v(sRGB_conv<value_type>::rgb_from_sRGB(c.v)),
a(sRGB_conv<value_type>::alpha_from_sRGB(c.a)) {}
//--------------------------------------------------------------------
gray32(const gray16& c) :
v(value_type(c.v / 65535.0)),
a(value_type(c.a / 65535.0)) {}
//--------------------------------------------------------------------
operator rgba() const
{
return rgba(v, v, v, a);
}
//--------------------------------------------------------------------
operator gray8() const
{
return gray8(uround(v * 255.0), uround(a * 255.0));
}
//--------------------------------------------------------------------
operator sgray8() const
{
// Return (non-premultiplied) sRGB values.
return sgray8(
sRGB_conv<value_type>::rgb_to_sRGB(v),
sRGB_conv<value_type>::alpha_to_sRGB(a));
}
//--------------------------------------------------------------------
operator gray16() const
{
return gray16(uround(v * 65535.0), uround(a * 65535.0));
}
//--------------------------------------------------------------------
operator rgba8() const
{
rgba8::value_type y = uround(v * 255.0);
return rgba8(y, y, y, uround(a * 255.0));
}
//--------------------------------------------------------------------
operator srgba8() const
{
srgba8::value_type y = sRGB_conv<value_type>::rgb_to_sRGB(v);
return srgba8(y, y, y, sRGB_conv<value_type>::alpha_to_sRGB(a));
}
//--------------------------------------------------------------------
operator rgba16() const
{
rgba16::value_type y = uround(v * 65535.0);
return rgba16(y, y, y, uround(a * 65535.0));
}
//--------------------------------------------------------------------
operator rgba32() const
{
return rgba32(v, v, v, a);
}
//--------------------------------------------------------------------
static AGG_INLINE double to_double(value_type a)
{
return a;
}
//--------------------------------------------------------------------
static AGG_INLINE value_type from_double(double a)
{
return value_type(a);
}
//--------------------------------------------------------------------
static AGG_INLINE value_type empty_value()
{
return 0;
}
//--------------------------------------------------------------------
static AGG_INLINE value_type full_value()
{
return 1;
}
//--------------------------------------------------------------------
AGG_INLINE bool is_transparent() const
{
return a <= 0;
}
//--------------------------------------------------------------------
AGG_INLINE bool is_opaque() const
{
return a >= 1;
}
//--------------------------------------------------------------------
static AGG_INLINE value_type invert(value_type x)
{
return 1 - x;
}
//--------------------------------------------------------------------
static AGG_INLINE value_type multiply(value_type a, value_type b)
{
return value_type(a * b);
}
//--------------------------------------------------------------------
static AGG_INLINE value_type demultiply(value_type a, value_type b)
{
return (b == 0) ? 0 : value_type(a / b);
}
//--------------------------------------------------------------------
template<typename T>
static AGG_INLINE T downscale(T a)
{
return a;
}
//--------------------------------------------------------------------
template<typename T>
static AGG_INLINE T downshift(T a, unsigned n)
{
return n > 0 ? a / (1 << n) : a;
}
//--------------------------------------------------------------------
static AGG_INLINE value_type mult_cover(value_type a, cover_type b)
{
return value_type(a * b / cover_mask);
}
//--------------------------------------------------------------------
static AGG_INLINE cover_type scale_cover(cover_type a, value_type b)
{
return cover_type(uround(a * b));
}
//--------------------------------------------------------------------
// Interpolate p to q by a, assuming q is premultiplied by a.
static AGG_INLINE value_type prelerp(value_type p, value_type q, value_type a)
{
return (1 - a) * p + q; // more accurate than "p + q - p * a"
}
//--------------------------------------------------------------------
// Interpolate p to q by a.
static AGG_INLINE value_type lerp(value_type p, value_type q, value_type a)
{
// The form "p + a * (q - p)" avoids a multiplication, but may produce an
// inaccurate result. For example, "p + (q - p)" may not be exactly equal
// to q. Therefore, stick to the basic expression, which at least produces
// the correct result at either extreme.
return (1 - a) * p + a * q;
}
//--------------------------------------------------------------------
self_type& clear()
{
v = a = 0;
return *this;
}
//--------------------------------------------------------------------
self_type& transparent()
{
a = 0;
return *this;
}
//--------------------------------------------------------------------
self_type& opacity(double a_)
{
if (a_ < 0) a = 0;
else if (a_ > 1) a = 1;
else a = value_type(a_);
return *this;
}
//--------------------------------------------------------------------
double opacity() const
{
return a;
}
//--------------------------------------------------------------------
self_type& premultiply()
{
if (a < 0) v = 0;
else if(a < 1) v *= a;
return *this;
}
//--------------------------------------------------------------------
self_type& demultiply()
{
if (a < 0) v = 0;
else if (a < 1) v /= a;
return *this;
}
//--------------------------------------------------------------------
self_type gradient(self_type c, double k) const
{
return self_type(
value_type(v + (c.v - v) * k),
value_type(a + (c.a - a) * k));
}
//--------------------------------------------------------------------
static self_type no_color() { return self_type(0,0); }
};
}
#endif
|