testcolorchooser.c 3.01 KB
Newer Older
1 2 3 4 5 6 7
#include <gtk/gtk.h>

static void
color_changed (GObject *o, GParamSpec *pspect, gpointer data)
{
  GdkRGBA color;

Matthias Clasen's avatar
Matthias Clasen committed
8
  gtk_color_chooser_get_rgba (GTK_COLOR_CHOOSER (o), &color);
9 10 11 12 13 14 15 16 17 18 19 20
  g_print ("color changed: %g %g %g %g\n",
           color.red, color.green, color.blue, color.alpha);
}

static void
dialog_response (GtkDialog *dialog, gint response)
{
  GdkRGBA color;

  switch (response)
    {
    case GTK_RESPONSE_OK:
Matthias Clasen's avatar
Matthias Clasen committed
21
      gtk_color_chooser_get_rgba (GTK_COLOR_CHOOSER (dialog), &color);
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
      g_print ("color accepted: %g %g %g %g\n",
           color.red, color.green, color.blue, color.alpha);
      break;
    default:
      g_print ("canceled\n");
      break;
    }

  gtk_main_quit ();
}

int
main (int argc, char *argv[])
{
  GtkWidget *dialog;
37
  gint i;
38 39 40 41

  gtk_init (NULL, NULL);

  dialog = gtk_color_chooser_dialog_new ("Select a color", NULL);
42 43

  for (i = 1; i < argc; i++)
Matthias Clasen's avatar
Matthias Clasen committed
44
    {
45 46 47 48 49 50 51 52 53 54
      if (g_strcmp0 (argv[i], "--no-alpha") == 0)
        {
          g_print ("turning alpha off\n");
          gtk_color_chooser_set_use_alpha (GTK_COLOR_CHOOSER (dialog), FALSE);
        }
      else if (g_strcmp0 (argv[i], "--edit") == 0)
        {
          g_print ("starting in edit mode\n");
          g_object_set (dialog, "show-editor", TRUE, NULL);
        }
55 56
      else if (g_strcmp0 (argv[i], "--palette") == 0)
        {
57
          const gchar *c[9] = { "red", "maroon", "yellow", "green", "blue", "magenta", "DarkOliveGreen4", "khaki2", "thistle1" };
58
          GdkRGBA color;
59
          GdkRGBA colors[9*9];
60 61 62 63
          gint i,j;
          gdouble f[5] = { 0.2, 0.35, 0.5, 0.65, 0.8 };

          g_print ("setting custom palette\n");
64
          for (i = 0; i < 9; i++)
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
            {
              gdk_rgba_parse (&color, c[i]);
              for (j = 0; j < 5; j++)
                {
                  colors[i*9 + j].red   = f[j]*color.red;
                  colors[i*9 + j].green = f[j]*color.green;
                  colors[i*9 + j].blue  = f[j]*color.blue;
                  colors[i*9 + j].alpha = 1;
                }
              for (j = 5; j < 9; j++)
                {
                  colors[i*9 + j].red   = f[9-j]*color.red + (1-f[9-j]);
                  colors[i*9 + j].green = f[9-j]*color.green + (1-f[9-j]);
                  colors[i*9 + j].blue  = f[9-j]*color.blue + (1-f[9-j]);
                  colors[i*9 + j].alpha = 1;
                }
            }
          gtk_color_chooser_add_palette (GTK_COLOR_CHOOSER (dialog),
83
                                         GTK_ORIENTATION_VERTICAL,
84
                                         9, 9*9,
85 86
                                         colors);
        }
87 88 89
      else if (g_strcmp0 (argv[i], "--no-palette") == 0)
        {
          gtk_color_chooser_add_palette (GTK_COLOR_CHOOSER (dialog), 
90
                                         GTK_ORIENTATION_VERTICAL, 0, 0, NULL);
91
        }
Matthias Clasen's avatar
Matthias Clasen committed
92
    }
93

94 95 96 97 98 99 100 101 102 103
  g_signal_connect (dialog, "notify::color", G_CALLBACK (color_changed), NULL);
  g_signal_connect (dialog, "response", G_CALLBACK (dialog_response), NULL);

  gtk_widget_show_all (dialog);

  gtk_main ();

  return 0;
}