SporkApp Source Code

( <-- Back to SporkApp )

Title:"Color Combination Viewer"
Author:Sporkman
Last updated:
10/01/11  12:21 pm

Source code:


int sz_w = 500;
int sz_h = 400;

image img_curr;
image img_last;

resize_image( img_curr, sz_w, sz_h );

rgb col_main_curr, col_trim_curr, col_main_last, col_trim_last;

get_random_colors( col_main_curr, col_trim_curr );

draw_colors( img_curr, col_main_curr, col_trim_curr );

string rand_origin = " (randomly chosen):";
string origin = rand_origin;
string indent = "   ";

for ( ; ; )
{
   print_format("Welcome to the Color Combination Viewer\n\n","b");
   
   print("This tool will help you choose suitable color combinations for your website, publication, or\nother graphical presentation.\n\n");
   
   if ( image_wd(img_last)==0 )
   {
      print("Current" + origin + "\n\n" + indent);
      print_image(img_curr);
      print("\n" + indent + " Main color:  R= " + strf_int(rgb_r(col_main_curr),3,"") + " , " +
                                          "G= " + strf_int(rgb_g(col_main_curr),3,"") + " , " +
                                          "B= " + strf_int(rgb_b(col_main_curr),3,"") + "  -  Hex= #" +
                                          strf_int(rgb_r(col_main_curr),2,"0X") +
                                          strf_int(rgb_g(col_main_curr),2,"0X") +
                                          strf_int(rgb_b(col_main_curr),2,"0X") + "\n");
      print(       indent + " Trim color:  R= " + strf_int(rgb_r(col_trim_curr),3,"") + " , " +
                                          "G= " + strf_int(rgb_g(col_trim_curr),3,"") + " , " +
                                          "B= " + strf_int(rgb_b(col_trim_curr),3,"") + "  -  Hex= #" +
                                          strf_int(rgb_r(col_trim_curr),2,"0X") +
                                          strf_int(rgb_g(col_trim_curr),2,"0X") +
                                          strf_int(rgb_b(col_trim_curr),2,"0X") + "\n\n");
   }
   else
   {
      print("Current" + origin + "                                            Last:\n\n" + indent);
      print_image(img_curr);
      print("       ");
      print_image(img_last);
      print("\n" + indent + " Main color:  R= " + strf_int(rgb_r(col_main_curr),3,"") + " , " +
                                          "G= " + strf_int(rgb_g(col_main_curr),3,"") + " , " +
                                          "B= " + strf_int(rgb_b(col_main_curr),3,"") + "  -  Hex= #" +
                                          strf_int(rgb_r(col_main_curr),2,"0X") +
                                          strf_int(rgb_g(col_main_curr),2,"0X") +
                                          strf_int(rgb_b(col_main_curr),2,"0X"));
      print( "                Main color:  R= " + strf_int(rgb_r(col_main_last),3,"") + " , " +
                                          "G= " + strf_int(rgb_g(col_main_last),3,"") + " , " +
                                          "B= " + strf_int(rgb_b(col_main_last),3,"") + "  -  Hex= #" +
                                          strf_int(rgb_r(col_main_last),2,"0X") +
                                          strf_int(rgb_g(col_main_last),2,"0X") +
                                          strf_int(rgb_b(col_main_last),2,"0X") + "\n");
      print(       indent + " Trim color:  R= " + strf_int(rgb_r(col_trim_curr),3,"") + " , " +
                                          "G= " + strf_int(rgb_g(col_trim_curr),3,"") + " , " +
                                          "B= " + strf_int(rgb_b(col_trim_curr),3,"") + "  -  Hex= #" +
                                          strf_int(rgb_r(col_trim_curr),2,"0X") +
                                          strf_int(rgb_g(col_trim_curr),2,"0X") +
                                          strf_int(rgb_b(col_trim_curr),2,"0X"));
      print( "                Trim color:  R= " + strf_int(rgb_r(col_trim_last),3,"") + " , " +
                                          "G= " + strf_int(rgb_g(col_trim_last),3,"") + " , " +
                                          "B= " + strf_int(rgb_b(col_trim_last),3,"") + "  -  Hex= #" +
                                          strf_int(rgb_r(col_trim_last),2,"0X") +
                                          strf_int(rgb_g(col_trim_last),2,"0X") +
                                          strf_int(rgb_b(col_trim_last),2,"0X") + "\n\n");
   }
   
   print("Next colors:  ");
   
   input btn_rand = print_input_button("Choose random");
   
   print(" or:\n\n                   " + indent);
   
   input btn_val_rgb = print_input_button("Set to RGB value:");
   
   print("                  ");
   
   input btn_val_hex = print_input_button("Set to hex value:");
   
   print("\n\n" + indent + "  Main color:    R=");
   
   input val_m_r = print_input_field(3,3,str(rgb_r(col_main_curr)));
   
   print("  G=");
   
   input val_m_g = print_input_field(3,3,str(rgb_g(col_main_curr)));
   
   print("  B=");
   
   input val_m_b = print_input_field(3,3,str(rgb_b(col_main_curr)));
   
   print("       Hex= #");
   
   input val_m_h = print_input_field(6,6,strf_int(rgb_r(col_main_curr),2,"0X") +
                                       strf_int(rgb_g(col_main_curr),2,"0X") +
                                       strf_int(rgb_b(col_main_curr),2,"0X"));
   
   print("\n" + indent + "  Trim color:    R=");
   
   input val_t_r = print_input_field(3,3,str(rgb_r(col_trim_curr)));
   
   print("  G=");
   
   input val_t_g = print_input_field(3,3,str(rgb_g(col_trim_curr)));
   
   print("  B=");
   
   input val_t_b = print_input_field(3,3,str(rgb_b(col_trim_curr)));
   
   print("       Hex= #");
   
   input val_t_h = print_input_field(6,6,strf_int(rgb_r(col_trim_curr),2,"0X") +
                                         strf_int(rgb_g(col_trim_curr),2,"0X") +
                                         strf_int(rgb_b(col_trim_curr),2,"0X"));
   
   print("\n");
   
   load_user_input();
   
   copy_image(img_last,img_curr);
   
   col_main_last = col_main_curr;
   col_trim_last = col_trim_curr;
   
   if ( get_input_value(btn_rand)=="set" )
   {
      get_random_colors( col_main_curr, col_trim_curr );
   }
   elseif ( get_input_value(btn_val_rgb)=="set" )
   {
      set_rgb_r(col_main_curr,intval(get_input_value(val_m_r)));
      set_rgb_g(col_main_curr,intval(get_input_value(val_m_g)));
      set_rgb_b(col_main_curr,intval(get_input_value(val_m_b)));
      set_rgb_r(col_trim_curr,intval(get_input_value(val_t_r)));
      set_rgb_g(col_trim_curr,intval(get_input_value(val_t_g)));
      set_rgb_b(col_trim_curr,intval(get_input_value(val_t_b)));
   }
   else
   {
      int mv = intval("0x" + get_input_value(val_m_h));
      int tv = intval("0x" + get_input_value(val_t_h));
      
      set_rgb_r(col_main_curr,(mv & 0xff0000)>>16);
      set_rgb_g(col_main_curr,(mv & 0xff00)>>8);
      set_rgb_b(col_main_curr,mv & 0xff);
      set_rgb_r(col_trim_curr,(tv & 0xff0000)>>16);
      set_rgb_g(col_trim_curr,(tv & 0xff00)>>8);
      set_rgb_b(col_trim_curr,tv & 0xff);
   }
   
   draw_colors( img_curr, col_main_curr, col_trim_curr );
}

function void get_random_colors ( rgb & col_main, rgb & col_trim )
{
   int m = random_int();
   int t = random_int();
   
   /* Tiny chance of trim color being the same as the main color */
   while ( (t&0xffffff)==(m&0xffffff) )
   {
      t = random_int();
   }
   
   set_rgb_r(col_main,m & 0xff);
   set_rgb_g(col_main,(m & 0xff00)>>8);
   set_rgb_b(col_main,(m & 0xff0000)>>16);
   set_rgb_r(col_trim,t & 0xff);
   set_rgb_g(col_trim,(t & 0xff00)>>8);
   set_rgb_b(col_trim,(t & 0xff0000)>>16);
}

function void draw_colors ( image & img, rgb col_main, rgb col_trim )
{
   set_image_to_color(img,col_main);
   
   int i, j;
   
   int w = image_wd(img);
   int h = image_ht(img);
   
   int hdr_x0 = 0.16 * w;
   int hdr_x1 = w - hdr_x0;
   int hdr_y0 = 0.75 * h;
   int hdr_y1 = hdr_y0 + 0.12 * h;
   
   int sbar_l_x0 = 0.16 * w;
   int sbar_l_x1 = sbar_l_x0 + 0.017 * w;
   int sbar_l_y0 = 0.25 * h;
   int sbar_l_y1 = hdr_y0 - 20;
   
   int sbar_r_x0 = w - sbar_l_x1;
   int sbar_r_x1 = w - sbar_l_x0;
   int sbar_r_y0 = sbar_l_y0;
   int sbar_r_y1 = sbar_l_y1;
   
   int ftr_x0 = sbar_l_x1 + 20;
   int ftr_x1 = sbar_r_x0 - 20;
   int ftr_y0 = 0.2 * h;
   int ftr_y1 = ftr_y0 + 18;
   
   int ctr_x0 = 0.3 * w;
   int ctr_x1 = w - ctr_x0;
   int ctr_y0 = 0.6 * h;
   int ctr_y1 = ctr_y0 + 0.032 * h;
   
   for ( j=hdr_y0; j<=hdr_y1; ++j )
   {
      for ( i=hdr_x0; i<=hdr_x1; ++i )
      {
         set_image_p(img,i,j,col_trim);
      }
   }
   
   for ( j=sbar_l_y0; j<=sbar_l_y1; ++j )
   {
      for ( i=sbar_l_x0; i<=sbar_l_x1; ++i )
      {
         set_image_p(img,i,j,col_trim);
      }
   }
   
   for ( j=sbar_r_y0; j<=sbar_r_y1; ++j )
   {
      for ( i=sbar_r_x0; i<=sbar_r_x1; ++i )
      {
         set_image_p(img,i,j,col_trim);
      }
   }
   
   for ( j=ftr_y0; j<=ftr_y1; ++j )
   {
      for ( i=ftr_x0; i<=ftr_x1; ++i )
      {
         set_image_p(img,i,j,col_trim);
      }
   }
   
   for ( j=ctr_y0; j<=ctr_y1; ++j )
   {
      for ( i=ctr_x0; i<=ctr_x1; ++i )
      {
         set_image_p(img,i,j,col_trim);
      }
   }
}








sporkforge