How to define a color scheme option?

121 views Asked by At

Can anyone help me with best approach to the following? I have a SWT GUI java app developed using Eclipse. Not that all of that is particularly relevant.

I have a class to hold preferences that to simplify only has colors at moment. The option is a single integer held in a file that it successfully reads. The color_option is one of eight color schemes of 6 RGB type colors. So among others I tried:

class Preferences {
        int color_option = 0;
        String[] color_option_name = new String[8];
        RGB[] rgb01=new RGB[8];
        RGB[] rgb02=new RGB[8];
        RGB[] rgb03=new RGB[8];
        RGB[] rgb04=new RGB[8];
        RGB[] rgb05=new RGB[8];
        RGB[] rgb06=new RGB[8];
        Color[] color01=new Color[8];
        Color[] color02=new Color[8];
        Color[] color03=new Color[8];
        Color[] color04=new Color[8];
        Color[] color05=new Color[8];
        Color[] color06=new Color[8];

I have become aware that these colors are like literals and not variables so the above leads to a very bland display. And I tried:

if(color_option == 0) {
                color_option_name[color_option] = "Earth";
                rgb01[color_option]=new RGB(218, 165, 32);
                color01[color_option]=new Color(Display.getCurrent(),rgb01[color_option]);
                rgb02[color_option]=new RGB(188, 143, 143);

I want to use the colors for things like:

g0_opens_or_creates_project.setBackground(

and

if (script_deletion_flag[x]!=null && script_deletion_flag[x] ) {

itemS.setForeground(thisPreferences.color04[thisPreferences.color_option]);
           }
           else
           {
itemS.setForeground(thisPreferences.color05[thisPreferences.color_option]);
           }

Probably obvious but if anyone can shine a light on how to go about this without defining every color and lots of if statements this beginner would be very grateful. Tx in anticipation.

1

There are 1 answers

0
Nigele2 On

Well either no one uses color, it's difficult, or so simple no one could be bothered (could be lol) but for other beginners here is the best I have come up with and now implemented.

The problem revolves around RGB and Color not being variables and Color needing to be final.

So in my preferences file I have:

Earth Colors|218|165|32|188|143|143|178|34|34|107|142|35|143|188|143|189|183|107| Pastel|127|255|212|255|182|193|238|233|233|32|178|170|186|85|211|210|180|140| Navy|255|255|255|0|0|128|100|149|237|190|190|190|230|230|250|32|178|170| Fire|233|150|122|250|128|114|255|160|122|255|165|0|255|140|0|255|127|80| Gothic|128|0|128|0|0|0|255|255|255|192|192|192|221|160|221|255|228|196| Bright Colors|255|20|147|0|255|0|255|255|0|0|255|255|255|0|0|0|128|128| Bloom|238|221|130|205|92|92|255|140|0|255|69|0|255|0|0|232|150|122| ICE|255|250|250|230|230|250|192|192|192|176|196|222|95|158|160|100|149|237|

So 8 color schemes of 6 colors each specified by an r, g and b integer (0-255).

And having read me preferences file coded like:

    // This should be chosen color scheme
    String[] color_rgb = lineoptions.split("\\|");


    // Gothic
         color_int[0] = Integer.valueOf(color_rgb[1]);
         color_int[1] = Integer.valueOf(color_rgb[2]);
         color_int[2] = Integer.valueOf(color_rgb[3]);
         color_int[3] = Integer.valueOf(color_rgb[4]);
         color_int[4] = Integer.valueOf(color_rgb[5]);
         color_int[5] = Integer.valueOf(color_rgb[6]);
    .....etc

Then:

RGB rgb101=new RGB(color_int[0], color_int[1], color_int[2]);
    RGB rgb102=new RGB(color_int[3], color_int[4], color_int[5]);
    RGB rgb103=new RGB(color_int[6], color_int[7], color_int[8]);
    RGB rgb104=new RGB(color_int[9], color_int[10], color_int[11]);
    RGB rgb105=new RGB(color_int[12], color_int[13], color_int[14]);
    RGB rgb106=new RGB(color_int[15], color_int[16], color_int[17]);
    final Color color01 = new Color(Display.getCurrent(), rgb101);
    final Color color02 = new Color(Display.getCurrent(), rgb102);
    final Color color03 = new Color(Display.getCurrent(), rgb103);
    final Color color04 = new Color(Display.getCurrent(), rgb104);
    final Color color05 = new Color(Display.getCurrent(), rgb105);
    final Color color06 = new Color(Display.getCurrent(), rgb106);

Of course one has to restart the application before the new color selection takes effect.

Hope might help someone and possibly there are better answers? Cheers Nigel