Example #1
0
void HSVControl::CalcRamps()
{
    // all the ramps are 256 pixels in width and 1 pixel in height
    // and 32-bit mode

    // the order of colors in a bitmap is BGRA
    uint32 *bits = (uint32*)ramp1->Bits();
    union {
        uint8 bytes[4];
        uint32 word;
    } color,black,white;
    for (int32 i=0; i<256; i++) {
        float red_value = 0.0;
        float green_value = 0.0;
        float blue_value = 0.0;
        float temp_value = i*(max_value_at_1()-min_value_at_1())/255.0+min_value_at_1();
        hsv_to_rgb(temp_value,s_value,v_value,&red_value,&green_value,&blue_value);
        color.bytes[0] = (uint8)blue_value;
        color.bytes[1] = (uint8)green_value;
        color.bytes[2] = (uint8)red_value;
        color.bytes[3] = (uint8)255;

        *bits++ = color.word;
    }
    bits = (uint32*)ramp2->Bits();
    for (int32 i=0; i<256; i++) {
        float red_value = 0.0;
        float green_value = 0.0;
        float blue_value = 0.0;
        float temp_value = i*(max_value_at_2()-min_value_at_2())/255.0+min_value_at_2();
        hsv_to_rgb(h_value,temp_value,v_value,&red_value,&green_value,&blue_value);
        color.bytes[0] = (uint8)blue_value;
        color.bytes[1] = (uint8)green_value;
        color.bytes[2] = (uint8)red_value;
        color.bytes[3] = (uint8)255;

        *bits++ = color.word;
    }

    bits = (uint32*)ramp3->Bits();
    for (int32 i=0; i<256; i++) {
        float red_value = 0.0;
        float green_value = 0.0;
        float blue_value = 0.0;
        float temp_value = i*(max_value_at_3()-min_value_at_3())/255.0+min_value_at_3();
        hsv_to_rgb(h_value,s_value,temp_value,&red_value,&green_value,&blue_value);
        color.bytes[0] = (uint8)blue_value;
        color.bytes[1] = (uint8)green_value;
        color.bytes[2] = (uint8)red_value;
        color.bytes[3] = (uint8)255;

        *bits++ = color.word;
    }
    bits = (uint32*)ramp4->Bits();
    black.word = 0x00000000;
    black.bytes[3] = 0xFF;
    white.word = 0xFFFFFFFF;
    color.word = value.word;
    for (int32 i=0; i<256; i++) {
        if ((i%2) == 0) {
            color.word = mix_2_pixels_fixed(value.word,black.word,32768/255*i);
        }
        else
            color.word = mix_2_pixels_fixed(value.word,white.word,32768/255*i);
        *bits++ = color.word;
    }
}
Example #2
0
void HSVControl::MouseDown(BPoint point)
{
    BRect plate_rect = BRect(Bounds().Width()-PLATE_WIDTH+2,2,Bounds().Width()-2,Bounds().Height()-2);
    if (plate_rect.Contains(point)) {
        // If the point is on the color-plate we drag that color.
        // Start a drag'n'drop session.
        BBitmap *dragged_map = new BBitmap(BRect(0,0,15,15),B_RGB32,TRUE);
        BView *dragger_view = new BView(BRect(0,0,15,15),"dragger_view",B_FOLLOW_NONE,B_WILL_DRAW);
        rgb_color c = ValueAsColor();
        dragged_map->AddChild(dragger_view);
        dragged_map->Lock();
        dragger_view->SetHighColor(c);
        dragger_view->FillRect(dragger_view->Bounds());
        dragger_view->Sync();
        dragged_map->Unlock();
        BMessage dragger_message(B_PASTE);
        dragger_message.AddData("RGBColor",B_RGB_COLOR_TYPE,&c,sizeof(rgb_color));
        DragMessage(&dragger_message,dragged_map,BPoint(7,7));
    }
    else {
        uint32 buttons;
        Window()->CurrentMessage()->FindInt32("buttons",(int32*)&buttons);

        if (Message() != NULL) {
            // if invocation-message has "buttons" we should replace it
            if (Message()->HasInt32("buttons"))
                Message()->ReplaceInt32("buttons",buttons);
        }

        float previous_value = -500;
        float orig_h = h_value;
        float orig_s = s_value;
        float orig_v = v_value;
        int32 orig_x = (int32)point.x;
        if (modifiers() & B_LEFT_SHIFT_KEY) {
            float prev_h = -500;
            float prev_s = -500;
            float prev_v = -500;
            while (buttons) {
                // Change all the values linearily
                h_value = ((point.x-4-ramp_left_edge)/RAMP_WIDTH)*(max_value_at_1()-min_value_at_1());
                h_value = min_c(h_value,max_value_at_1());
                h_value = max_c(h_value,min_value_at_1());

                s_value = min_value_at_2() + ((point.x-4-ramp_left_edge)/RAMP_WIDTH)*(max_value_at_2()-min_value_at_2());
                s_value = min_c(s_value,max_value_at_2());
                s_value = max_c(s_value,min_value_at_2());

                v_value = min_value_at_3() + ((point.x-4-ramp_left_edge)/RAMP_WIDTH)*(max_value_at_3()-min_value_at_3());
                v_value = min_c(v_value,max_value_at_3());
                v_value = max_c(v_value,min_value_at_3());

//				float red_value = max_c(0,min_c(255,(1*y_value + 0.956*i_value + 0.621*q_value)));
//				float green_value = max_c(0,min_c(255,(1*y_value - 0.272*i_value - 0.647*q_value)));
//				float blue_value = max_c(0,min_c(255,(1*y_value - 1.105*i_value + 1.702*q_value)));
                float red_value = 0.0;
                float green_value = 0.0;
                float blue_value = 0.0;

                hsv_to_rgb(h_value,s_value,v_value,&red_value,&green_value,&blue_value);

                value.bytes[0] = (uint8)blue_value;
                value.bytes[1] = (uint8)green_value;
                value.bytes[2] = (uint8)red_value;

                if ((h_value != prev_h) || (s_value != prev_s) || (v_value != prev_v)) {
                    prev_h = h_value;
                    prev_s = s_value;
                    prev_v = v_value;
                    CalcRamps();
                    Draw(Bounds());
                }
                GetMouse(&point,&buttons);
            }
        }
        else if (modifiers() & B_LEFT_CONTROL_KEY) {
            float prev_h = -500;
            float prev_s = -500;
            float prev_v = -500;
            while (buttons) {
                // Change all the values proportionally
                h_value = orig_h - (orig_x-point.x);
                h_value = min_c(h_value,max_value_at_1());
                h_value = max_c(h_value,min_value_at_1());

                s_value = orig_s - (orig_x-point.x);
                s_value = min_c(s_value,max_value_at_2());
                s_value = max_c(s_value,min_value_at_2());

                v_value = orig_v - (orig_x-point.x);
                v_value = min_c(v_value,max_value_at_3());
                v_value = max_c(v_value,min_value_at_3());

                float red_value = 0.0;
                float green_value = 0.0;
                float blue_value = 0.0;

                hsv_to_rgb(h_value,s_value,v_value,&red_value,&green_value,&blue_value);

                value.bytes[0] = (uint8)blue_value;
                value.bytes[1] = (uint8)green_value;
                value.bytes[2] = (uint8)red_value;

                if ((h_value != prev_h) || (s_value != prev_s) || (v_value != prev_v)) {
                    prev_h = h_value;
                    prev_s = s_value;
                    prev_v = v_value;
                    CalcRamps();
                    Draw(Bounds());
                }
                GetMouse(&point,&buttons);
            }
        }



        else if (((int32)(point.y / COLOR_HEIGHT)) == 0) {
            // Here we change the Hue-value
            while (buttons) {
                // make sure the value does not exceed limits
                h_value = ((point.x-4-ramp_left_edge)/RAMP_WIDTH)*(max_value_at_1()-min_value_at_1());
                h_value = min_c(h_value,max_value_at_1());
                h_value = max_c(h_value,min_value_at_1());

                float red_value = 0.0;
                float green_value = 0.0;
                float blue_value = 0.0;
                hsv_to_rgb(h_value,s_value,v_value,&red_value,&green_value,&blue_value);

                value.bytes[0] = (uint8)blue_value;
                value.bytes[1] = (uint8)green_value;
                value.bytes[2] = (uint8)red_value;


                if (h_value != previous_value) {
                    previous_value = h_value;
                    CalcRamps();
                    Draw(Bounds());
                }
                GetMouse(&point,&buttons);
            }
        }

        else if	(((int32)(point.y / COLOR_HEIGHT)) == 1) {
            // Here we change the Saturation-value
            while (buttons) {
                // make sure the value does not exceed limits
                s_value = min_value_at_2() + ((point.x-4-ramp_left_edge)/RAMP_WIDTH)*(max_value_at_2()-min_value_at_2());
                s_value = min_c(s_value,max_value_at_2());
                s_value = max_c(s_value,min_value_at_2());

                float red_value = 0.0;
                float green_value = 0.0;
                float blue_value = 0.0;

                hsv_to_rgb(h_value,s_value,v_value,&red_value,&green_value,&blue_value);

                value.bytes[0] = (uint8)blue_value;
                value.bytes[1] = (uint8)green_value;
                value.bytes[2] = (uint8)red_value;

                if (s_value != previous_value) {
                    previous_value = s_value;
                    CalcRamps();
                    Draw(Bounds());
                }
                GetMouse(&point,&buttons);
            }
        }

        else if	(((int32)(point.y / COLOR_HEIGHT)) == 2) {
            // Here we change the Value-value
            while (buttons) {
                // make sure the value does not exceed limits
                v_value = min_value_at_3() + ((point.x-4-ramp_left_edge)/RAMP_WIDTH)*(max_value_at_3()-min_value_at_3());
                v_value = min_c(v_value,max_value_at_3());
                v_value = max_c(v_value,min_value_at_3());

                float red_value = 0.0;
                float green_value = 0.0;
                float blue_value = 0.0;

                hsv_to_rgb(h_value,s_value,v_value,&red_value,&green_value,&blue_value);

                value.bytes[0] = (uint8)blue_value;
                value.bytes[1] = (uint8)green_value;
                value.bytes[2] = (uint8)red_value;

                if (v_value != previous_value) {
                    previous_value = v_value;
                    CalcRamps();
                    Draw(Bounds());
                }
                GetMouse(&point,&buttons);
            }
        }
        else {
            while (buttons) {
                // Change the alpha
                int32 alpha_value = (int32)(((point.x-4-ramp_left_edge)/RAMP_WIDTH)*255);
                alpha_value = min_c(alpha_value,255);
                alpha_value = max_c(alpha_value,0);

                value.bytes[3] = alpha_value;

                if (alpha_value != previous_value) {
                    previous_value = alpha_value;
                    CalcRamps();
                    Draw(Bounds());
                }
                GetMouse(&point,&buttons);
            }
        }
        if (Message() != NULL) {
            // if invocation-message has "color" we should replace it
            if (Message()->HasInt32("color"))
                Message()->ReplaceInt32("color",value.word);
        }
        Invoke();
    }
}
void VisualColorControl::Draw(BRect)
{
	// ramp height is COLOR_HEIGHT - 12 pixels that are required for controls
	int32 ramp_height = COLOR_HEIGHT - 12;

	// here draw the ramps
	BRect ramp_rect = BRect(ramp_left_edge+4,6,ramp_left_edge+RAMP_WIDTH+4,6+ramp_height-1);

	DrawBitmap(ramp1,ramp1->Bounds(),ramp_rect);
	ramp_rect.OffsetBy(0,COLOR_HEIGHT);
	DrawBitmap(ramp2,ramp2->Bounds(),ramp_rect);
	ramp_rect.OffsetBy(0,COLOR_HEIGHT);
	DrawBitmap(ramp3,ramp3->Bounds(),ramp_rect);
	ramp_rect.OffsetBy(0,COLOR_HEIGHT);
	DrawBitmap(ramp4,ramp4->Bounds(),ramp_rect);

	// draw the color-plate
	BRect a_rect = BRect(Bounds().Width()-PLATE_WIDTH+2,2,Bounds().Width()-2,Bounds().Height()-2);
	SetHighColor(0,0,0);
	StrokeRect(a_rect);


	rgb_color low = ValueAsColor();
	rgb_color high = low;

	float coeff = high.alpha / 255.0;
	low.red = (uint8)(coeff*low.red);
	low.green = (uint8)(coeff*low.green);
	low.blue = (uint8)(coeff*low.blue);
	low.alpha = 255;

	high.red = (uint8)(coeff*high.red + (1-coeff)*255);
	high.green = (uint8)(coeff*high.green + (1-coeff)*255);
	high.blue = (uint8)(coeff*high.blue + (1-coeff)*255);
	high.alpha = 255;

	SetHighColor(high);
	SetLowColor(low);
	a_rect.InsetBy(1,1);
	FillRect(a_rect,HS_2X2_BLOCKS);

	// clear the previous arrows and draw new arrows
	SetHighColor(ViewColor());
	BRect bg_rect = BRect(ramp_left_edge + 0,0,ramp_left_edge + RAMP_WIDTH+8,5);
	if (previous_value_at_1 != value_at_1())
		FillRect(bg_rect);

	bg_rect.OffsetBy(0,COLOR_HEIGHT);

	if (previous_value_at_2 != value_at_2())
		FillRect(bg_rect);

	bg_rect.OffsetBy(0,COLOR_HEIGHT);

	if (previous_value_at_3 != value_at_3())
		FillRect(bg_rect);

	bg_rect.OffsetBy(0,COLOR_HEIGHT);

	if (previous_value_at_4 != value_at_4())
		FillRect(bg_rect);

	DrawPicture(down_arrow,BPoint(ramp_left_edge + (float)((value_at_1()-min_value_at_1()))/(max_value_at_1()-min_value_at_1())*RAMP_WIDTH+4,5));
	DrawPicture(down_arrow,BPoint(ramp_left_edge + (float)((value_at_2()-min_value_at_2()))/(max_value_at_2()-min_value_at_2())*RAMP_WIDTH+4,COLOR_HEIGHT+5));
	DrawPicture(down_arrow,BPoint(ramp_left_edge + (float)((value_at_3()-min_value_at_3()))/(max_value_at_3()-min_value_at_3())*RAMP_WIDTH+4,2*COLOR_HEIGHT+5));
	DrawPicture(down_arrow,BPoint(ramp_left_edge + (float)((value_at_4()-min_value_at_4()))/(max_value_at_4()-min_value_at_4())*RAMP_WIDTH+4,3*COLOR_HEIGHT+5));
	Sync();

	previous_value_at_1 = value_at_1();
	previous_value_at_2 = value_at_2();
	previous_value_at_3 = value_at_3();
	previous_value_at_4 = value_at_4();


//	DrawPicture(down_arrow,BPoint(ramp_left_edge + (float)(value_at_2())/255.0*RAMP_WIDTH+4,COLOR_HEIGHT+5));
//	DrawPicture(down_arrow,BPoint(ramp_left_edge + (float)(value_at_3())/255.0*RAMP_WIDTH+4,2*COLOR_HEIGHT+5));
}