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
|
/////////////////////////////////////////////////////////////////////////////////
// Paint.NET //
// Copyright (C) Rick Brewster, Tom Jackson, and past contributors. //
// Portions Copyright (C) Microsoft Corporation. All Rights Reserved. //
// See license-pdn.txt for full licensing and attribution details. //
/////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.Runtime.InteropServices;
namespace Pinta.Core
{
/// <summary>
/// This is our pixel format that we will work with. It is always 32-bits / 4-bytes and is
/// always laid out in BGRA order.
/// Generally used with the Surface class.
/// </summary>
[Serializable]
[StructLayout(LayoutKind.Explicit)]
public struct ColorBgra
{
[FieldOffset(0)]
public byte B;
[FieldOffset(1)]
public byte G;
[FieldOffset(2)]
public byte R;
[FieldOffset(3)]
public byte A;
/// <summary>
/// Lets you change B, G, R, and A at the same time.
/// </summary>
[NonSerialized]
[FieldOffset(0)]
public uint Bgra;
public const int BlueChannel = 0;
public const int GreenChannel = 1;
public const int RedChannel = 2;
public const int AlphaChannel = 3;
public const int SizeOf = 4;
public static ColorBgra ParseHexString(string hexString)
{
uint value = Convert.ToUInt32(hexString, 16);
return ColorBgra.FromUInt32(value);
}
public string ToHexString()
{
int rgbNumber = (this.R << 16) | (this.G << 8) | this.B;
string colorString = Convert.ToString(rgbNumber, 16);
while (colorString.Length < 6)
{
colorString = "0" + colorString;
}
string alphaString = System.Convert.ToString(this.A, 16);
while (alphaString.Length < 2)
{
alphaString = "0" + alphaString;
}
colorString = alphaString + colorString;
return colorString.ToUpper();
}
/// <summary>
/// Gets or sets the byte value of the specified color channel.
/// </summary>
public unsafe byte this[int channel]
{
get
{
if (channel < 0 || channel > 3)
{
throw new ArgumentOutOfRangeException("channel", channel, "valid range is [0,3]");
}
fixed (byte *p = &B)
{
return p[channel];
}
}
set
{
if (channel < 0 || channel > 3)
{
throw new ArgumentOutOfRangeException("channel", channel, "valid range is [0,3]");
}
fixed (byte *p = &B)
{
p[channel] = value;
}
}
}
/// <summary>
/// Gets the luminance intensity of the pixel based on the values of the red, green, and blue components. Alpha is ignored.
/// </summary>
/// <returns>A value in the range 0 to 1 inclusive.</returns>
public double GetIntensity()
{
return ((0.114 * (double)B) + (0.587 * (double)G) + (0.299 * (double)R)) / 255.0;
}
/// <summary>
/// Gets the luminance intensity of the pixel based on the values of the red, green, and blue components. Alpha is ignored.
/// </summary>
/// <returns>A value in the range 0 to 255 inclusive.</returns>
public byte GetIntensityByte()
{
return (byte)((7471 * B + 38470 * G + 19595 * R) >> 16);
}
/// <summary>
/// Returns the maximum value out of the B, G, and R values. Alpha is ignored.
/// </summary>
/// <returns></returns>
public byte GetMaxColorChannelValue()
{
return Math.Max(this.B, Math.Max(this.G, this.R));
}
/// <summary>
/// Returns the average of the B, G, and R values. Alpha is ignored.
/// </summary>
/// <returns></returns>
public byte GetAverageColorChannelValue()
{
return (byte)((this.B + this.G + this.R) / 3);
}
/// <summary>
/// Compares two ColorBgra instance to determine if they are equal.
/// </summary>
public static bool operator == (ColorBgra lhs, ColorBgra rhs)
{
return lhs.Bgra == rhs.Bgra;
}
/// <summary>
/// Compares two ColorBgra instance to determine if they are not equal.
/// </summary>
public static bool operator != (ColorBgra lhs, ColorBgra rhs)
{
return lhs.Bgra != rhs.Bgra;
}
/// <summary>
/// Compares two ColorBgra instance to determine if they are equal.
/// </summary>
public override bool Equals(object obj)
{
if (obj != null && obj is ColorBgra && ((ColorBgra)obj).Bgra == this.Bgra)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// Returns a hash code for this color value.
/// </summary>
/// <returns></returns>
public override int GetHashCode()
{
unchecked
{
return (int)Bgra;
}
}
/// <summary>
/// Gets the equivalent GDI+ PixelFormat.
/// </summary>
/// <remarks>
/// This property always returns PixelFormat.Format32bppArgb.
/// </remarks>
// public static PixelFormat PixelFormat
// {
// get
// {
// return PixelFormat.Format32bppArgb;
// }
// }
/// <summary>
/// Returns a new ColorBgra with the same color values but with a new alpha component value.
/// </summary>
public ColorBgra NewAlpha(byte newA)
{
return ColorBgra.FromBgra(B, G, R, newA);
}
/// <summary>
/// Creates a new ColorBgra instance with the given color and alpha values.
/// </summary>
[Obsolete ("Use FromBgra() instead (make sure to swap the order of your b and r parameters)")]
public static ColorBgra FromRgba(byte r, byte g, byte b, byte a)
{
return FromBgra(b, g, r, a);
}
/// <summary>
/// Creates a new ColorBgra instance with the given color values, and 255 for alpha.
/// </summary>
[Obsolete ("Use FromBgr() instead (make sure to swap the order of your b and r parameters)")]
public static ColorBgra FromRgb(byte r, byte g, byte b)
{
return FromBgr(b, g, r);
}
/// <summary>
/// Creates a new ColorBgra instance with the given color and alpha values.
/// </summary>
public static ColorBgra FromBgra(byte b, byte g, byte r, byte a)
{
ColorBgra color = new ColorBgra();
color.Bgra = BgraToUInt32(b, g, r, a);
return color;
}
/// <summary>
/// Creates a new ColorBgra instance with the given color and alpha values.
/// </summary>
public static ColorBgra FromBgraClamped(int b, int g, int r, int a)
{
return FromBgra(
ClampToByte(b),
ClampToByte(g),
ClampToByte(r),
ClampToByte(a));
}
/// <summary>
/// Creates a new ColorBgra instance with the given color and alpha values.
/// </summary>
public static ColorBgra FromBgraClamped(float b, float g, float r, float a)
{
return FromBgra(
ClampToByte(b),
ClampToByte(g),
ClampToByte(r),
ClampToByte(a));
}
public static byte ClampToByte(float x)
{
if (x > 255)
{
return 255;
}
else if (x < 0)
{
return 0;
}
else
{
return (byte)x;
}
}
/// <summary>
/// Packs color and alpha values into a 32-bit integer.
/// </summary>
public static UInt32 BgraToUInt32(byte b, byte g, byte r, byte a)
{
return (uint)b + ((uint)g << 8) + ((uint)r << 16) + ((uint)a << 24);
}
/// <summary>
/// Packs color and alpha values into a 32-bit integer.
/// </summary>
public static UInt32 BgraToUInt32(int b, int g, int r, int a)
{
return (uint)b + ((uint)g << 8) + ((uint)r << 16) + ((uint)a << 24);
}
/// <summary>
/// Creates a new ColorBgra instance with the given color values, and 255 for alpha.
/// </summary>
public static ColorBgra FromBgr(byte b, byte g, byte r)
{
return FromBgra(b, g, r, 255);
}
/// <summary>
/// Constructs a new ColorBgra instance with the given 32-bit value.
/// </summary>
public static ColorBgra FromUInt32(UInt32 bgra)
{
ColorBgra color = new ColorBgra();
color.Bgra = bgra;
return color;
}
public static byte ClampToByte(int x)
{
if (x > 255)
{
return 255;
}
else if (x < 0)
{
return 0;
}
else
{
return (byte)x;
}
}
/// <summary>
/// Constructs a new ColorBgra instance from the values in the given Color instance.
/// </summary>
// public static ColorBgra FromColor(Color c)
// {
// return FromBgra(c.B, c.G, c.R, c.A);
// }
/// <summary>
/// Converts this ColorBgra instance to a Color instance.
/// </summary>
// public Color ToColor()
// {
// return Color.FromArgb(A, R, G, B);
// }
/// <summary>
/// Smoothly blends between two colors.
/// </summary>
public static ColorBgra Blend(ColorBgra ca, ColorBgra cb, byte cbAlpha)
{
uint caA = (uint)Utility.FastScaleByteByByte((byte)(255 - cbAlpha), ca.A);
uint cbA = (uint)Utility.FastScaleByteByByte(cbAlpha, cb.A);
uint cbAT = caA + cbA;
uint r;
uint g;
uint b;
if (cbAT == 0)
{
r = 0;
g = 0;
b = 0;
}
else
{
r = ((ca.R * caA) + (cb.R * cbA)) / cbAT;
g = ((ca.G * caA) + (cb.G * cbA)) / cbAT;
b = ((ca.B * caA) + (cb.B * cbA)) / cbAT;
}
return ColorBgra.FromBgra((byte)b, (byte)g, (byte)r, (byte)cbAT);
}
/// <summary>
/// Linearly interpolates between two color values.
/// </summary>
/// <param name="from">The color value that represents 0 on the lerp number line.</param>
/// <param name="to">The color value that represents 1 on the lerp number line.</param>
/// <param name="frac">A value in the range [0, 1].</param>
/// <remarks>
/// This method does a simple lerp on each color value and on the alpha channel. It does
/// not properly take into account the alpha channel's effect on color blending.
/// </remarks>
public static ColorBgra Lerp(ColorBgra from, ColorBgra to, float frac)
{
ColorBgra ret = new ColorBgra();
ret.B = (byte)ClampToByte(Lerp(from.B, to.B, frac));
ret.G = (byte)ClampToByte(Lerp(from.G, to.G, frac));
ret.R = (byte)ClampToByte(Lerp(from.R, to.R, frac));
ret.A = (byte)ClampToByte(Lerp(from.A, to.A, frac));
return ret;
}
public static float Lerp(float from, float to, float frac)
{
return (from + frac * (to - from));
}
public static double Lerp(double from, double to, double frac)
{
return (from + frac * (to - from));
}
/// <summary>
/// Linearly interpolates between two color values.
/// </summary>
/// <param name="from">The color value that represents 0 on the lerp number line.</param>
/// <param name="to">The color value that represents 1 on the lerp number line.</param>
/// <param name="frac">A value in the range [0, 1].</param>
/// <remarks>
/// This method does a simple lerp on each color value and on the alpha channel. It does
/// not properly take into account the alpha channel's effect on color blending.
/// </remarks>
public static ColorBgra Lerp(ColorBgra from, ColorBgra to, double frac)
{
ColorBgra ret = new ColorBgra();
ret.B = (byte)ClampToByte(Lerp(from.B, to.B, frac));
ret.G = (byte)ClampToByte(Lerp(from.G, to.G, frac));
ret.R = (byte)ClampToByte(Lerp(from.R, to.R, frac));
ret.A = (byte)ClampToByte(Lerp(from.A, to.A, frac));
return ret;
}
public static byte ClampToByte(double x)
{
if (x > 255)
{
return 255;
}
else if (x < 0)
{
return 0;
}
else
{
return (byte)x;
}
}
/// <summary>
/// Blends four colors together based on the given weight values.
/// </summary>
/// <returns>The blended color.</returns>
/// <remarks>
/// The weights should be 16-bit fixed point numbers that add up to 65536 ("1.0").
/// 4W16IP means "4 colors, weights, 16-bit integer precision"
/// </remarks>
public static ColorBgra BlendColors4W16IP(ColorBgra c1, uint w1, ColorBgra c2, uint w2, ColorBgra c3, uint w3, ColorBgra c4, uint w4)
{
#if DEBUG
if ((w1 + w2 + w3 + w4) != 65536)
{
throw new ArgumentOutOfRangeException("w1 + w2 + w3 + w4 must equal 65536!");
}
#endif
const uint ww = 32768;
uint af = (c1.A * w1) + (c2.A * w2) + (c3.A * w3) + (c4.A * w4);
uint a = (af + ww) >> 16;
uint b;
uint g;
uint r;
if (a == 0)
{
b = 0;
g = 0;
r = 0;
}
else
{
b = (uint)((((long)c1.A * c1.B * w1) + ((long)c2.A * c2.B * w2) + ((long)c3.A * c3.B * w3) + ((long)c4.A * c4.B * w4)) / af);
g = (uint)((((long)c1.A * c1.G * w1) + ((long)c2.A * c2.G * w2) + ((long)c3.A * c3.G * w3) + ((long)c4.A * c4.G * w4)) / af);
r = (uint)((((long)c1.A * c1.R * w1) + ((long)c2.A * c2.R * w2) + ((long)c3.A * c3.R * w3) + ((long)c4.A * c4.R * w4)) / af);
}
return ColorBgra.FromBgra((byte)b, (byte)g, (byte)r, (byte)a);
}
/// <summary>
/// Blends the colors based on the given weight values.
/// </summary>
/// <param name="c">The array of color values.</param>
/// <param name="w">The array of weight values.</param>
/// <returns>
/// The weights should be fixed point numbers.
/// The total summation of the weight values will be treated as "1.0".
/// Each color will be blended in proportionally to its weight value respective to
/// the total summation of the weight values.
/// </returns>
/// <remarks>
/// "WAIP" stands for "weights, arbitrary integer precision"</remarks>
public static ColorBgra BlendColorsWAIP(ColorBgra[] c, uint[] w)
{
if (c.Length != w.Length)
{
throw new ArgumentException("c.Length != w.Length");
}
if (c.Length == 0)
{
return ColorBgra.FromUInt32(0);
}
long wsum = 0;
long asum = 0;
for (int i = 0; i < w.Length; ++i)
{
wsum += w[i];
asum += c[i].A * w[i];
}
uint a = (uint)((asum + (wsum >> 1)) / wsum);
long b;
long g;
long r;
if (a == 0)
{
b = 0;
g = 0;
r = 0;
}
else
{
b = 0;
g = 0;
r = 0;
for (int i = 0; i < c.Length; ++i)
{
b += (long)c[i].A * c[i].B * w[i];
g += (long)c[i].A * c[i].G * w[i];
r += (long)c[i].A * c[i].R * w[i];
}
b /= asum;
g /= asum;
r /= asum;
}
return ColorBgra.FromUInt32((uint)b + ((uint)g << 8) + ((uint)r << 16) + ((uint)a << 24));
}
/// <summary>
/// Blends the colors based on the given weight values.
/// </summary>
/// <param name="c">The array of color values.</param>
/// <param name="w">The array of weight values.</param>
/// <returns>
/// Each color will be blended in proportionally to its weight value respective to
/// the total summation of the weight values.
/// </returns>
/// <remarks>
/// "WAIP" stands for "weights, floating-point"</remarks>
public static ColorBgra BlendColorsWFP(ColorBgra[] c, double[] w)
{
if (c.Length != w.Length)
{
throw new ArgumentException("c.Length != w.Length");
}
if (c.Length == 0)
{
return ColorBgra.FromUInt32(0);
}
double wsum = 0;
double asum = 0;
for (int i = 0; i < w.Length; ++i)
{
wsum += w[i];
asum += (double)c[i].A * w[i];
}
double a = asum / wsum;
double aMultWsum = a * wsum;
double b;
double g;
double r;
if (asum == 0)
{
b = 0;
g = 0;
r = 0;
}
else
{
b = 0;
g = 0;
r = 0;
for (int i = 0; i < c.Length; ++i)
{
b += (double)c[i].A * c[i].B * w[i];
g += (double)c[i].A * c[i].G * w[i];
r += (double)c[i].A * c[i].R * w[i];
}
b /= aMultWsum;
g /= aMultWsum;
r /= aMultWsum;
}
return ColorBgra.FromBgra((byte)b, (byte)g, (byte)r, (byte)a);
}
/// <summary>
/// Smoothly blends the given colors together, assuming equal weighting for each one.
/// </summary>
/// <param name="colors"></param>
/// <param name="colorCount"></param>
/// <returns></returns>
public unsafe static ColorBgra Blend(ColorBgra* colors, int count)
{
if (count < 0)
{
throw new ArgumentOutOfRangeException("count must be 0 or greater");
}
if (count == 0)
{
return ColorBgra.Transparent;
}
ulong aSum = 0;
for (int i = 0; i < count; ++i)
{
aSum += (ulong)colors[i].A;
}
byte b = 0;
byte g = 0;
byte r = 0;
byte a = (byte)(aSum / (ulong)count);
if (aSum != 0)
{
ulong bSum = 0;
ulong gSum = 0;
ulong rSum = 0;
for (int i = 0; i < count; ++i)
{
bSum += (ulong)(colors[i].A * colors[i].B);
gSum += (ulong)(colors[i].A * colors[i].G);
rSum += (ulong)(colors[i].A * colors[i].R);
}
b = (byte)(bSum / aSum);
g = (byte)(gSum / aSum);
r = (byte)(rSum / aSum);
}
return ColorBgra.FromBgra(b, g, r, a);
}
public override string ToString()
{
return "B: " + B + ", G: " + G + ", R: " + R + ", A: " + A;
}
/// <summary>
/// Casts a ColorBgra to a UInt32.
/// </summary>
public static explicit operator UInt32(ColorBgra color)
{
return color.Bgra;
}
/// <summary>
/// Casts a UInt32 to a ColorBgra.
/// </summary>
public static explicit operator ColorBgra(UInt32 uint32)
{
return ColorBgra.FromUInt32(uint32);
}
//// Colors: copied from System.Drawing.Color's list (don't worry I didn't type it in
//// manually, I used a code generator w/ reflection ...)
public static ColorBgra Transparent { get { return ColorBgra.FromBgra (255, 255, 255, 0); } }
public static ColorBgra Zero { get { return (ColorBgra)0; } }
public static ColorBgra Black { get { return ColorBgra.FromBgra (0, 0, 0, 255); } }
public static ColorBgra Blue { get { return ColorBgra.FromBgra (255, 0, 0, 255); } }
public static ColorBgra Cyan { get { return ColorBgra.FromBgra (255, 255, 0, 255); } }
public static ColorBgra Green { get { return ColorBgra.FromBgra (0, 128, 0, 255); } }
public static ColorBgra Magenta { get { return ColorBgra.FromBgra (255, 0, 255, 255); } }
public static ColorBgra Red { get { return ColorBgra.FromBgra (0, 0, 255, 255); } }
public static ColorBgra White { get { return ColorBgra.FromBgra (255, 255, 255, 255); } }
public static ColorBgra Yellow { get { return ColorBgra.FromBgra (0, 255, 255, 255); } }
}
}
|