Ocena użytkownikóww: ***** / 5
Nadesłany przez Tomasz Lubiński, 19 sierpnia 2010 10:00
Kod przedstawiony poniżej przedstawia główną część rozwiązania problemu.
Pobierz pełne rozwiązanie.Jeżeli nie odpowiada Ci sposób formatowania kodu przez autora skorzystaj z pretty printer'a i dostosuj go automatycznie do siebie.
Color Accent - C++/Color_accent.cpp:
//---------------------------------------------------------------------------
//Akcent koloru
//www.algorytm.org
//(c) 2010 by Tomasz Lubinski
#include <vcl.h>
#include "math.h"
#pragma hdrstop
#include "Color_accent.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
struct rgb
{
Byte b;
Byte g;
Byte r;
};
#define MIN(a,b) ((a)<(b)?(a):(b))
#define MAX(a,b) ((a)>(b)?(a):(b))
void RGB2HSV(float &hue, float &sat, float &val, float red, float grn, float blu){
float x, f, i;
x = MIN(MIN(red, grn), blu);
val = MAX(MAX(red, grn), blu);
if (x == val){
hue = 0;
sat = 0;
}
else {
f = (red == x) ? grn-blu : ((grn == x) ? blu-red : red-grn);
i = (red == x) ? 3 : ((grn == x) ? 5 : 1);
hue = fmod((i-f/(val-x))*60, 360);
sat = ((val-x)/val);
}
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SrcMouseDown(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
TColor color;
rgb *Psrc, *Pdst;
float hsrc, ssrc, vsrc, hdst, sdst, vdst, hfrom, hto;
int range;
Dst->Canvas->Brush->Color = clWhite;
Dst->Canvas->Rectangle(0, 0, Src->Picture->Bitmap->Width, Src->Picture->Bitmap->Height);
Dst->Picture->Bitmap->PixelFormat = pf24bit;
color = Src->Canvas->Pixels[X][Y];
RGB2HSV(hsrc, ssrc, vsrc, (color & 0xFF) / 255.0, ((color & 0xFF00) >> 8) / 255.0, ((color & 0xFF0000) >> 16) / 255.0);
range = StrToInt(Range->Text);
hfrom = fmod((hsrc - (range / 2)) + 360, 360);
hto = fmod(hsrc + (range / 2), 360);
for (int i=0; i<Src->Picture->Bitmap->Height; i++)
{
Psrc = (rgb *)Src->Picture->Bitmap->ScanLine[i];
Pdst = (rgb *)Dst->Picture->Bitmap->ScanLine[i];
for (int j=0; j<Src->Picture->Bitmap->Width; j++)
{
RGB2HSV(hdst, sdst, vdst, Psrc->r/255.0, Psrc->g/255.0, Psrc->b/255.0);
if (( hfrom <= hto &&
hfrom <= hdst &&
hto >= hdst) ||
( hfrom > hto &&
( hfrom <= hdst ||
hto >= hdst )))
{
*Pdst = *Psrc;
}
else
{
Pdst->r = (int)(0.299 * Psrc->r + 0.587 * Psrc->g + 0.114 * Psrc->b);
Pdst->g = (int)(0.299 * Psrc->r + 0.587 * Psrc->g + 0.114 * Psrc->b);
Pdst->b = (int)(0.299 * Psrc->r + 0.587 * Psrc->g + 0.114 * Psrc->b);
}
Psrc++;
Pdst++;
}
}
}
//---------------------------------------------------------------------------