rgb_color ColorSwatch::Inverted (void) const { rgb_color baseColor, value = ValueAsColor(); baseColor.red = 255 - value.red; baseColor.green = 255 - value.green; baseColor.blue = 255 - value.blue; baseColor.alpha = value.alpha; return baseColor; }
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 ColorSwatch::Draw (BRect) { PushState(); SetDrawingMode (B_OP_COPY); rgb_color high (HighColor()); BRect colorPad (Bounds()); SetHighColor (ValueAsColor()); FillRect (colorPad); rgb_color light (ShiftColor (ValueAsColor(), 0.4)); rgb_color dark (ShiftColor (ValueAsColor(), 1.2)); BeginLineArray (10); AddLine ( colorPad.LeftTop(), colorPad.RightTop() - BPoint (1, 0), light); AddLine ( colorPad.LeftTop(), colorPad.LeftBottom() - BPoint (0, 1), light); AddLine ( colorPad.RightTop() + BPoint (0, 1), colorPad.RightBottom(), dark); AddLine ( colorPad.RightBottom(), colorPad.LeftBottom() + BPoint (1, 0), dark); light = ShiftColor (ViewColor(), 0.1); dark = ShiftColor (ViewColor(), 1.4); colorPad.InsetBy (-1, -1); BPoint hless (-1, 0); BPoint hmore (1, 0); BPoint vless (0, -1); BPoint vmore (0, 1); if (IsFocus() && Window()->IsActive()) { light = general_info.mark_color; dark = general_info.mark_color; hless = hmore = vless = vmore = BPoint (0, 0); } else { // A little blue residue clean up AddLine ( colorPad.RightTop(), colorPad.RightTop(), ViewColor()); AddLine ( colorPad.LeftBottom(), colorPad.LeftBottom(), ViewColor()); } AddLine ( colorPad.LeftTop(), colorPad.RightTop() + hless, dark); AddLine ( colorPad.LeftTop(), colorPad.LeftBottom() + vless, dark); AddLine ( colorPad.RightTop() + vmore, colorPad.RightBottom(), light); AddLine ( colorPad.RightBottom(), colorPad.LeftBottom() + hmore, light); EndLineArray(); PopState(); }
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)); }