Question: Not sure how to explain this, but here it goes.

`$val` could contain a string followed by none, one or multiple dots:

```
test
test.
test..
test... 
etc

```

Using regex I'm looking for any invalid characters in `$val` and then I want to replace them.

```
(preg_replace('-['.$inv.']-',"",$val))

```

`$inv` is an array of invalid entries, this also includes a dot.

How do I strip all `$inv` characters from `$val` EXCEPT any dots on the end?

if `$val = te%st...` the correct result should be test…

if `$val = te.est.` the correct result should be test. 

Hope that makes sense. Thanks.

Answer: 
I would write it this way

```
int[] values = new int[]{0, 32, 64, 96, 128, 160, 192, 224, 255};
SortedMap<Long, Color> colours = new TreeMap<>(Collections.reverseOrder());
for (int r : values) {
    for (int g : values) {
        for (int b : values) {
            long brightness = (r + g + b) / 3;
            long colourScore = (brightness << 24) + (r << 8) + (g << 16) + b;
            colours.put(colourScore, new Color(r,g,b));
        }
    }
}
for (Color color : colours.values()) {
    System.out.println(color);
}

```

This favours red and green over blue as blue tinting is associated with night. ;) It prints

```
java.awt.Color[r=255,g=255,b=255]
java.awt.Color[r=255,g=255,b=224]
java.awt.Color[r=224,g=255,b=255]
java.awt.Color[r=255,g=224,b=255]
java.awt.Color[r=255,g=255,b=192]
java.awt.Color[r=224,g=255,b=224]
java.awt.Color[r=192,g=255,b=255]
... many deleted ...
java.awt.Color[r=32,g=0,b=32]
java.awt.Color[r=0,g=0,b=64]
java.awt.Color[r=0,g=32,b=0]
java.awt.Color[r=32,g=0,b=0]
java.awt.Color[r=0,g=0,b=32]
java.awt.Color[r=0,g=0,b=0]

```
You'll need:

1. a `Color` class that represents a full RGB triplet;
2. implement `Comparable` on `Color`, whereby deciding exactly what constitutes your desired color order;
3. in your nested loop you'll build a `TreeSet` of all colors. When you iterate over it, you'll get the colors in your desired order.

Since colors are inherently three-dimensional entities, it is not quite obvious how to linearize their order, so you'll have to make some compromises.