Here is the source code for the three color classes. TAjColor a class for dealing with
32bit colors on the windows platform, TAjQuad a class for dealing with 32bit
colors in bitmaps and last a TAjTriple for dealing with 24bit colors.
I have
written these classes, so I can avoid the old fashioned C code:
Byte Red = aColour & 0x00FF0000 >> 16; which I
find very hard to read or the Byte way where you read each
color part in a scanline from a bitmap by adding to the index e.g. Byte
Red = Line[i]; Byte Green = Line[i+1]; equally hard to read. I also wanted to avoid the quite slow windows type of code: Byte
Red = GetRValue( aColour );. So what to do.
What I came up
with is a struct containing 4 bytes, Red, Green, Blue and
Alpha, this seem
simple enough, but I would also like to be able to deal with the whole pixel and
not just each color part. So the next step was to use a struct containing a
union which again contained the different types like you can see here to the
right.
Putting everything into nameless structs lets me address each member
with just one dot like this: TAjColor Cl; Cl.Red or
Cl.Rgb. And because this is a union, the four structs all share the
same space, putting something in one, automatically fills the other as well.
This is actually a huge advantage, because now we can assign an int to the
.IntCl and right away read each individual color by reading
.Red, .Green and .Blue. This I think gives
some very readable code when using the color classes.
There are actually 3 different color types presented here, as you will see in the source code below TAjColor a 32 bit color used on windows systems, TAjQuad also a 32 bit color, but this one is used in 32 bit bitmaps and the color order are reversed. Last there is the TAjTriple which is used when working with 24 bit bitmaps.
I have added a few of the most used constructors to all three types as well as a number of functions, I encourage you to add functions to the classes, when ever you have the need, because it is always easier to read well named functions instead of cryptic code lines, as an example lets take this line of code: Byte b = ( 320 * Red + 576 * Green + 128 * Blue ) >> 10; You have to know a bit about colors before you know that this is the formula for getting the gray average of a color instead I think it is very much easier to read Byte b = Color.AsGray() .
As mentioned in the header, the code i written in C++Builder, most of the code are common to all C++ compilers, but the TColor type is a VCL specific type that will have to be removed or maybe replaced, if you want to compile the code with another compiler. If you need to remove it you just remove the whole struct containing the TColor and of course the functions that uses the TColor.
You can use this source code as you see fit and at your own risk, as long as you don't publish it anywhere and as long as you don't claim to have written it. Links to this code page will be greatly appreciated.
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 |
#ifndef AjColorH #define AjColorH //--------------------------------------------------------------------------- #include <classes.hpp> // a C++ Builder VCL header includes e.g stdlib.h //--------------------- Helper functions ---------------------------- void AjByteToHex( wchar_t *res, Byte b ); // res minimum 2 in length void AjByteToDec( wchar_t *res, Byte b ); // res minimum 3 in length //--------------------------------------------------------------------------- struct TAjQuad; struct TAjTriple; //--------------------------------------------------------------------------- //---------- TAjColor --------- //--------------------------------------------------------------------------- #pragma pack(push, 1) //--------------------------------------------------------------------------- struct TAjColor // a 32 bit Color class designed for work with 32bit { // colors on the windows system union{ struct{ Byte Red; Byte Green; Byte Blue; Byte Alpha; }; struct{ TColor Cl; }; struct{ COLORREF Rgb; }; struct{ int IntCl; }; }; //================ Constructors =================== TAjColor(): Rgb(0x00FFFFFF){} //----------------------------------------------------- TAjColor( COLORREF Cf ): Rgb(Cf){} //----------------------------------------------------- TAjColor( TColor c ) : Cl( (TColor)ColorToRGB( c ) ){} // ColorToRGB will translate e.g. clBtnFace into the . // apropriate color ( VCL ) . //----------------------------------------------------- TAjColor( Byte r, Byte g, Byte b, Byte a ) : Blue(b), Green(g), Red(r), Alpha(a){} //----------------------------------------------------- TAjColor( Byte r, Byte g, Byte b ) : Blue(b), Green(g), Red(r), Alpha(0){} //----------------------------------------------------- TAjColor( const TAjColor &t ) : Alpha(t.Alpha),Blue(t.Blue), Green(t.Green), Red(t.Red){} //---------------------------------------------------- TAjColor( const TAjQuad &t ); // implementet after TAjTriple //---------------------------------------------------- TAjColor( const TAjTriple &t ); // implementet after TAjTriple //============ End Constructors =================== TAjColor AlphaMix( const TAjColor &Bkg ) { int DestOpaq = (Byte)( ~Alpha ); TAjColor Res; Res.Red = ( Bkg.Red * DestOpaq + Red * Alpha ) >> 8; Res.Green = ( Bkg.Green * DestOpaq + Green * Alpha ) >> 8; Res.Blue = ( Bkg.Blue * DestOpaq + Blue * Alpha ) >> 8; return Res; }//---------------------------------------------------- TAjColor Lighten( Byte Light ) { int DestOpaq = (Byte)(~Light); Red = ( Red * DestOpaq + 0xFF * Light ) >> 8; Green = ( Green * DestOpaq + 0xFF * Light ) >> 8; Blue = ( Blue * DestOpaq + 0xFF * Light ) >> 8; return *this; }//---------------------------------------------------- TAjColor Darken( Byte Dark ) { int DestOpaq = (Byte)( ~Dark ); Red = ( Red * DestOpaq / 0xFF ); Green = ( Green * DestOpaq / 0xFF ); Blue = ( Blue * DestOpaq / 0xFF ); return *this; }//---------------------------------------------------- bool operator ==( const TAjColor& t ) const { return Rgb == t.Rgb; }//---------------------------------------------------- bool operator !=( const TAjColor& t ) const { return ! ( Rgb == t.Rgb ); }//---------------------------------------------------- operator TColor(){ return (TColor)( Cl&0x00FFFFFF ); } //---------------------------------------------------- operator COLORREF(){ return ( Cl&0x00FFFFFF ); } //---------------------------------------------------- void TurnGray() { Blue = Green = Red = (320*Red + 576*Green + 128*Blue) >> 10; }//---------------------------------------------------- void TurnWeb()// get the closest websafe version of the color { int iRed = ( Red + 26 ) / 51; iRed *= 51; int iGreen = ( Green + 26 ) / 51; iGreen *= 51; int iBlue = ( Blue + 26 ) / 51; iBlue *= 51; Red = iRed; Green = iGreen; Blue = iBlue; }//---------------------------------------------------- void Mix50(const TAjColor &t) { Blue = ( (int)Blue + t.Blue ) / 2; Green = ( (int)Green + t.Green) / 2; Red = ( (int)Red + t.Red ) / 2; }//---------------------------------------------------- TAjColor SwapRedAndBlue() { Byte tmp = Red; Red = Blue; Blue = tmp; return *this; }//---------------------------------------------------- bool IsWhite() { return ( Red == 0xFF && Green == 0xFF && Blue == 0xFF ); } //---------------------------------------------------- bool IsBlack(){ return ( Red == 0 && Green == 0 && Blue == 0 ); } //---------------------------------------------------- TAjColor Str2Color( const wchar_t* BgrWHex ); //---------------------------------------------------- Byte AsGray() { return ( 320 * Red + 576 * Green + 128 * Blue ) >> 10; } //---------------------------------------------------- TAjColor InvertTxtColor() { return ( AsGray() < 111 ) ? 0x00FFFFFF : 0; } //---------------------------------------------------- void AsBgrHex(wchar_t *hex)// minimum 6 in length { AjByteToHex( hex , Blue ); AjByteToHex( &hex[2], Green ); AjByteToHex( &hex[4], Red ); } //---------------------------------------------------- void AsRgbHex(wchar_t *hex)// minimum 6 in length { AjByteToHex( hex , Red ); AjByteToHex( &hex[2], Green ); AjByteToHex( &hex[4], Blue ); } //---------------------------------------------------- }; #pragma pack(pop) //--------------------------------------------------------------------------- // TAjQuad . //--------------------------------------------------------------------------- #pragma pack(push, 1) //--------------------------------------------------------------------------- struct TAjQuad // a 32 bit Color class designed for work with 32bit { // bitmap colors on windows union{ struct{ Byte Blue; Byte Green; Byte Red; Byte Alpha; }; struct{ RGBQUAD WinQuad; }; struct{ int IntCl; }; }; //================ Constructors =================== TAjQuad() : IntCl(0xFFFFFFFF){} //----------------------------------------------------- TAjQuad( const TAjQuad &t ) : IntCl(t.IntCl){} //----------------------------------------------------- TAjQuad( const TAjColor &t ): Red(t.Red), Green(t.Green), Blue(t.Blue), Alpha(t.Alpha){} //----------------------------------------------------- TAjQuad( int i ) : IntCl(i){} //----------------------------------------------------- TAjQuad( Byte R, Byte G, Byte B) : Blue(B), Green(G), Red(R), Alpha(255){} //----------------------------------------------------- TAjQuad( Byte Gray ) : Blue(Gray), Green(Gray), Red(Gray), Alpha(0){} //----------------------------------------------------- TAjQuad( Byte R, Byte G, Byte B, Byte A ) : Blue(B), Green(G), Red(R), Alpha(A){} //----------------------------------------------------- TAjQuad( const RGBQUAD &t ) : WinQuad(t){} //----------------------------------------------------- TAjQuad( TColor t )// the VCL's TColor { TAjColor C( t ); Blue = C.Blue; Green = C.Green; Red = C.Red; Alpha=C.Alpha; }//----------------------------------------------------- TAjQuad( const TAjTriple &t ); // implementet after TAjTriple //----------------------------------------------------- TAjQuad( COLORREF t ) { TAjColor C(t); Blue = C.Blue; Green = C.Green; Red = C.Red; Alpha=C.Alpha; } //============ End Constructors =================== void TurnGray() { Blue = Green = Red = GrayByte(); }//---------------------------------------------------- Byte GrayByte() { return ( ( 320 * Red + 576 * Green + 128 * Blue ) >> 10 ); }//---------------------------------------------------- TAjQuad Gray() { return TAjQuad( GrayByte() ); }//---------------------------------------------------- Byte CreateTextAlphaWithColor( TAjQuad &t ) { // Write white text on black and use color pixels to // to create the alpha chanel and set all color pixels // to the coler you want for the text. Alpha = ( (73*Red + 150*Green + 32*Blue) / 0xFF ); if( Alpha < 245 ) Alpha = (int)Alpha * 75 / 100;// remove some boldness Red = t.Red; Green = t.Green; Blue = t.Blue; return Alpha; }//---------------------------------------------------- void PreMultiply() { Red = ( (int)Red * Alpha ) >> 8; Green = ( (int)Green * Alpha ) >> 8; Blue = ( (int)Blue * Alpha ) >> 8; }//---------------------------------------------------- void PreMultiplyFrom( const TAjQuad &t ) { Red = ( (int)( t.Red ) * t.Alpha ) >> 8; Green = ( (int)( t.Green ) * t.Alpha ) >> 8; Blue = ( (int)( t.Blue ) * t.Alpha ) >> 8; }//---------------------------------------------------- void AssignPreMultiplyed( Byte r, Byte g, Byte b, Byte a ) { Red = ( r * a ) >> 8; Green = ( g * a ) >> 8; Blue = ( b * a ) >> 8; }//---------------------------------------------------- void AssignGrayByteFullAlpha( Byte Val ) { Alpha = 255; Blue = Green = Red = Val; }//---------------------------------------------------- void AssignGrayByteNullAlpha( Byte Val ) { Alpha = 0; Blue = Green = Red = Val; }//---------------------------------------------------- void AssignGray( const TAjQuad &t ) { Blue = Green = Red = (320*t.Red + 576*t.Green + 128*t.Blue) >> 10; }//---------------------------------------------------- void AssignColorsOnly( const TAjQuad &t ) { Red = t.Red; Green = t.Green; Blue = t.Blue; }//---------------------------------------------------- void AssignInts( int R, int G, int B ) { Blue = B; Green = G; Red = R; Alpha=255; }//---------------------------------------------------- void AssignBytes( Byte R, Byte G, Byte B ) { Blue = B; Green = G; Red = R; Alpha=255; }//---------------------------------------------------- void AssignInvert( const TAjQuad& t ) { Blue = ~(t.Blue); Green = ~(t.Green); Red = ~(t.Red); }//---------------------------------------------------- void AssignFastInvert( const TAjQuad& t ) { IntCl = ~(t.IntCl); }//---------------------------------------------------- void Invert() { Blue = ~Blue; Green = ~Green; Red = ~Red; }//---------------------------------------------------- void FastInvert() { IntCl = ~IntCl; }//---------------------------------------------------- COLORREF Rgb(){ return TAjColor( Red, Green, Blue ).Rgb; } //---------------------------------------------------- TColor Color(){ return (TColor)Rgb(); } //---------------------------------------------------- operator int(){ return (IntCl); } //---------------------------------------------------- bool operator ==( const TAjColor& t ) const { return Blue == t.Blue && Green == t.Green && Red == t.Red; }//---------------------------------------------------- bool operator ==( const TAjQuad& t ) const { return Blue == t.Blue && Green == t.Green && Red == t.Red; }//---------------------------------------------------- bool operator !=( const TAjQuad& t ) const { return !(Blue == t.Blue && Green == t.Green && Red == t.Red); }//---------------------------------------------------- TAjQuad AlphaMix( const TAjQuad &Bkg ) { int DestOpaq = (Byte)(~Alpha); TAjColor Res; Res.Red = ( Bkg.Red *DestOpaq + Red *Alpha ) >> 8; Res.Green = ( Bkg.Green *DestOpaq + Green *Alpha ) >> 8; Res.Blue = ( Bkg.Blue *DestOpaq + Blue *Alpha ) >> 8; return Res; }//---------------------------------------------------- void Mix50( const TAjQuad &t ) { Blue = ( (int)Blue + t.Blue ) / 2; Green = ( (int)Green + t.Green) / 2; Red = ( (int)Red + t.Red ) / 2; }//---------------------------------------------------- TAjQuad Lighten(Byte Val) { int DestOpaq = (Byte)(~Val); Red = ( Red * DestOpaq + 0xFF * Val ) >> 8; Green = ( Green * DestOpaq + 0xFF * Val ) >> 8; Blue = ( Blue * DestOpaq + 0xFF * Val ) >> 8; return *this; }//---------------------------------------------------- TAjQuad Darken(Byte Val) { int DestOpaq = (Byte)(~Val); Red = ( Red *DestOpaq / 0xFF ); Green = ( Green *DestOpaq / 0xFF ); Blue = ( Blue *DestOpaq / 0xFF ); return *this; }//---------------------------------------------------- TAjQuad DarkenOrLighten( int16_t Val ) { if( Val < 0 ) Darken( Val * -1 ); else Lighten(Val); return *this; }//---------------------------------------------------- bool IsWhite() { return ( Red == 0xFF && Green == 0xFF && Blue == 0xFF ); } //---------------------------------------------------- bool IsBlack() { return ( Red == 0 && Green == 0 && Blue == 0 ); } //---------------------------------------------------- void Debug() { // 01234567890123456789012 wchar_t *res = L"A=000-R=000-G=000-B=000"; AjByteToDec( &res[2], Alpha ); AjByteToDec( &res[8], Red ); AjByteToDec( &res[14], Green ); AjByteToDec( &res[20], Blue ); OutputDebugStringW( res ); } //---------------------------------------------------- }; //--------------------------------------------------------------------------- #pragma pack(pop) //--------------------------------------------------------------------------- //------- TAjTriple ---------- //--------------------------------------------------------------------------- #pragma pack(push, 1) //--------------------------------------------------------------------------- struct TAjTriple // a 24bit color class designed for working { // with 24bit bitmap colors Byte Blue; Byte Green; Byte Red; //================ Constructors =================== TAjTriple() : Blue(0), Green(0), Red(0){} //----------------------------------------------------- TAjTriple( const TAjTriple &t ) :Blue(t.Blue), Green(t.Green), Red(t.Red){} //---------------------------------------------------- TAjTriple( Byte R, Byte G, Byte B ) : Blue(B), Green(G), Red(R){} //----------------------------------------------------- TAjTriple::TAjTriple( const TAjColor &t ) : Blue( t.Blue ) , Green( t.Green ) , Red( t.Red ) {} //---------------------------------------------------- TAjTriple::TAjTriple( const TAjQuad &t ) : Blue( t.Blue ) , Green( t.Green ) , Red( t.Red ) {} //============ End Constructors =================== bool operator ==( const TAjTriple& t ) const { return Blue == t.Blue && Green == t.Green && Red == t.Red; }//---------------------------------------------------- bool operator !=( const TAjTriple& t ) const { return ! ( Blue == t.Blue && Green == t.Green && Red == t.Red ); }//---------------------------------------------------- operator int() const { return TAjQuad( Red, Green, Blue, 0xFF ).IntCl; }//---------------------------------------------------- void Assign( const TAjTriple &t ) { Blue = t.Blue; Green = t.Green; Red = t.Red; }//---------------------------------------------------- void AssignGrayByte( Byte b ) { Blue = Green = Red = b; }//---------------------------------------------------- void AssignQuad( const TAjQuad &t ) { Blue = t.Blue; Green = t.Green; Red = t.Red; }//---------------------------------------------------- void TurnGray() { Blue = Green = Red = (320*Red + 576*Green + 128*Blue) >> 10; }//---------------------------------------------------- Byte Gray() { return (320*Red + 576*Green + 128*Blue) >> 10;//1024; }//---------------------------------------------------- TAjTriple GrayScanCl() { Byte Cl = Gray(); return TAjTriple( Cl, Cl, Cl ); }//---------------------------------------------------- void SetColors( Byte R, Byte G, Byte B ) { Blue = B; Green = G; Red = R; }//---------------------------------------------------- void SetIntColors( int R, int G, int B ) { Blue = B; Green = G; Red = R; }//---------------------------------------------------- void SetColors( const float &R, const float &G, const float &B ) { Blue = B; Green = G; Red = R; }//---------------------------------------------------- bool IsWhite() { return ( Red == 0xFF && Green == 0xFF && Blue == 0xFF ); } //---------------------------------------------------- bool IsBlack() { return ( Red == 0 && Green == 0 && Blue == 0 ); } //---------------------------------------------------- }; #pragma pack(pop) //--------------------------------------------------------------------------- // Implementation . //--------------------------------------------------------------------------- TAjColor MixColors( TAjColor A, TAjColor B ) { return ((A.Rgb>>1) & 0x7F7F7F) + ((B.Rgb>>1) & 0x7F7F7F); } //--------------------------------------------------------------------------- TAjQuad MixQColors( TAjQuad A, TAjQuad B ) { return ((A.IntCl>>1) & 0x7F7F7F) + ((B.IntCl>>1) & 0x7F7F7F); } //--------------------------------------------------------------------------- TAjColor::TAjColor( const TAjQuad &t ) : Alpha( t.Alpha ) , Blue( t.Blue ) , Green( t.Green ) , Red( t.Red ) {} //--------------------------------------------------------------------------- TAjColor::TAjColor( const TAjTriple &t ) : Alpha( 0 ) , Blue( t.Blue ) , Green( t.Green ) , Red( t.Red ) {} //--------------------------------------------------------------------------- TAjColor TAjColor::Str2Color( const wchar_t* BgrWHex ) { // To get all three colors you must provide BBGGRR eg FFFFFF if( BgrWHex == 0 || *BgrWHex == L'\0' ){ IntCl = 0; return *this;} const wchar_t* Num = BgrWHex; int iNum = 0; while( *Num != '\0' ) { wchar_t n = *Num - 48; if( n < 10 ) { iNum = iNum << 4; //same as * 16 iNum += n; }else if( n > 16 && n < 23 ) { iNum = iNum << 4; iNum += n-7; }else if( n > 48 && n < 55 ) { iNum = iNum << 4; iNum += n-39; } ++Num; } return TAjColor( iNum ); } //--------------------------------------------------------------------------- TAjQuad::TAjQuad( const TAjTriple &t ) : Alpha( 0xFF ) , Blue( t.Blue ) , Green( t.Green ) , Red( t.Red ) {} //--------------------------------------------------------------------------- void AjByteToHex( wchar_t *res, Byte b ) { wchar_t *hex = L"0123456789ABCDEF"; res[1] = hex[b % 16]; b /= 16; res[0] = hex[b]; } //--------------------------------------------------------------------------- void AjByteToDec( wchar_t *res, Byte b ) { res[2] = ( b % 10 ) + L'0'; b /= 10; res[1] = ( b % 10 ) + L'0'; b /= 10; res[0] = b + L'0'; } //--------------------------------------------------------------------------- #endif |
Last Updated: Jun-28-2022 © Copyright 2003-2022 Asger-P