// Color.cpp -- RGBA color class // // SEE ALSO // Color.h - definition of class // // Copyright © Daniel Scharstein, 2003. // /////////////////////////////////////////////////////////////////////////// #include "Image.h" #include "Color.h" #include CCol::CCol(int id) { // constructor using color id A = 255; if (id == 0) { R = G = B = 255; } else { id--; R = G = B = 0; for (int j = 7; j >= 0; j--) { R |= ((id >> 0) & 1) << j; G |= ((id >> 1) & 1) << j; B |= ((id >> 2) & 1) << j; id = (id >> 3); } } } CCol::getId() { // translate color into integer id if ((R & G & B) == 255) { return 0; } else { int id = 0; for (int j = 0; j < 8; j++) id = (id << 3) | (((R >> j) & 1) << 0) | (((G >> j) & 1) << 1) | (((B >> j) & 1) << 2); return id+1; } } //names the lowest few ids. void CCol::printName(){ int a=getId(); switch(a){ case 0: printf("White"); break; case 1: printf("Black"); break; case 2: printf("Brick"); break; case 3: printf("Green"); break; case 4: printf("Khaki"); break; case 5: printf(" Blue"); break; case 6: printf("Purpl"); break; case 7: printf(" Teal"); break; case 8: printf(" Gray"); break; case 9: printf("DrkRd"); break; case 10: printf(" Red"); break; case 11: printf("Grass"); break; case 12: printf("Ornge"); break; case 13: printf("Indgo"); break; case 14: printf(" Pink"); break; case 15: printf(" Aqua"); break; default://color not named printf("C#%3d",a); } } //computes euclidean distance in "RGB space" float CCol::similarity(CCol c) { short d0 = c.R - R; short d1 = c.G - G; short d2 = c.B - B; return (float)sqrt((float)(d0*d0 + d1*d1 + d2*d2)); } //divides colors into 27 categories (3 by 3 by 3 cube by RGB values) and assigns the variable to one slot. char* CCol::describe() { if(R<75){ if(G<75){ if(B<75){ return " Black"; }else if(B<180){ return " Navy"; }else{ return " Blue"; } }else if(G<180){ if(B<75){ return "Forest"; }else if(B<180){ return "Turqos"; }else{ return " Azure"; } }else{ if(B<75){ return " Green"; }else if(B<180){ return " Teal"; }else{ return " Cyan"; } } }else if(R<180){ if(G<75){ if(B<75){ return " Brick"; }else if(B<180){ return "Purple"; }else{ return "Violet"; } }else if(G<180){ if(B<75){ return " Khaki"; }else if(B<180){ return " Gray"; }else{ return "L.Blue"; } }else{ if(B<75){ return "Spring"; }else if(B<180){ return "LGreen"; }else{ return "L.Cyan"; } } }else{ if(G<75){ if(B<75){ return " Red"; }else if(B<180){ return " Pink"; }else{ return "Magnta"; } }else if(G<180){ if(B<75){ return "Orange"; }else if(B<180){ return "Salmon"; }else{ return "Lavndr"; } }else{ if(B<75){ return "Yellow"; }else if(B<180){ return "LYello"; }else{ return " White"; } } } } void CCol::overWriteWith(CCol fg) // put fg "over" this color, using fg's alpha { int fa = fg.A; if (fa == 255) *this = fg; else { int ba = 255-fa; R = (fa * fg.R + ba * R) / 255; G = (fa * fg.G + ba * G) / 255; B = (fa * fg.B + ba * B) / 255; } }