chiark / gitweb /
Refactoring from James H: the highlight and lowlight colour setup
authorSimon Tatham <anakin@pobox.com>
Wed, 6 Jul 2005 18:27:40 +0000 (18:27 +0000)
committerSimon Tatham <anakin@pobox.com>
Wed, 6 Jul 2005 18:27:40 +0000 (18:27 +0000)
common to Fifteen, Sixteen, Twiddle and Pegs is now a utility
function in misc.c.

[originally from svn r6076]

fifteen.c
misc.c
pegs.c
puzzles.h
sixteen.c
twiddle.c

index 3a387bbdc2f5fce11a2b368cad4b90bf10670c35..3ce96119efe6f43774b6b54d479b6a71165d8636 100644 (file)
--- a/fifteen.c
+++ b/fifteen.c
@@ -584,28 +584,11 @@ static float *game_colours(frontend *fe, game_state *state, int *ncolours)
 {
     float *ret = snewn(3 * NCOLOURS, float);
     int i;
-    float max;
 
-    frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
+    game_mkhighlight(fe, ret, COL_BACKGROUND, COL_HIGHLIGHT, COL_LOWLIGHT);
 
-    /*
-     * Drop the background colour so that the highlight is
-     * noticeably brighter than it while still being under 1.
-     */
-    max = ret[COL_BACKGROUND*3];
-    for (i = 1; i < 3; i++)
-        if (ret[COL_BACKGROUND*3+i] > max)
-            max = ret[COL_BACKGROUND*3+i];
-    if (max * 1.2F > 1.0F) {
-        for (i = 0; i < 3; i++)
-            ret[COL_BACKGROUND*3+i] /= (max * 1.2F);
-    }
-
-    for (i = 0; i < 3; i++) {
-        ret[COL_HIGHLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 1.2F;
-        ret[COL_LOWLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.8F;
+    for (i = 0; i < 3; i++)
         ret[COL_TEXT * 3 + i] = 0.0;
-    }
 
     *ncolours = NCOLOURS;
     return ret;
diff --git a/misc.c b/misc.c
index 2f8d08d48be07d0e181021b10f683e28c44297bd..025083dc81204555bf9a11fc6f01d707f05d2e1b 100644 (file)
--- a/misc.c
+++ b/misc.c
@@ -169,4 +169,31 @@ unsigned char *hex2bin(const char *in, int outlen)
     return ret;
 }
 
+void game_mkhighlight(frontend *fe, float *ret,
+                      int background, int highlight, int lowlight)
+{
+    float max;
+    int i;
+
+    frontend_default_colour(fe, &ret[background * 3]);
+
+    /*
+     * Drop the background colour so that the highlight is
+     * noticeably brighter than it while still being under 1.
+     */
+    max = ret[background*3];
+    for (i = 1; i < 3; i++)
+        if (ret[background*3+i] > max)
+            max = ret[background*3+i];
+    if (max * 1.2F > 1.0F) {
+        for (i = 0; i < 3; i++)
+            ret[background*3+i] /= (max * 1.2F);
+    }
+
+    for (i = 0; i < 3; i++) {
+        ret[highlight * 3 + i] = ret[background * 3 + i] * 1.2F;
+        ret[lowlight * 3 + i] = ret[background * 3 + i] * 0.8F;
+    }
+}
+
 /* vim: set shiftwidth=4 tabstop=8: */
diff --git a/pegs.c b/pegs.c
index 07091875d6c63f6661aeb29688509785fed8eebe..2a03902d4de6bacedce0e24bdc2185ea519e470c 100644 (file)
--- a/pegs.c
+++ b/pegs.c
@@ -845,28 +845,8 @@ static void game_set_size(game_drawstate *ds, game_params *params,
 static float *game_colours(frontend *fe, game_state *state, int *ncolours)
 {
     float *ret = snewn(3 * NCOLOURS, float);
-    int i;
-    float max;
-
-    frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
 
-    /*
-     * Drop the background colour so that the highlight is
-     * noticeably brighter than it while still being under 1.
-     */
-    max = ret[COL_BACKGROUND*3];
-    for (i = 1; i < 3; i++)
-        if (ret[COL_BACKGROUND*3+i] > max)
-            max = ret[COL_BACKGROUND*3+i];
-    if (max * 1.2F > 1.0F) {
-        for (i = 0; i < 3; i++)
-            ret[COL_BACKGROUND*3+i] /= (max * 1.2F);
-    }
-
-    for (i = 0; i < 3; i++) {
-        ret[COL_HIGHLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 1.2F;
-        ret[COL_LOWLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.8F;
-    }
+    game_mkhighlight(fe, ret, COL_BACKGROUND, COL_HIGHLIGHT, COL_LOWLIGHT);
 
     ret[COL_PEG * 3 + 0] = 0.0F;
     ret[COL_PEG * 3 + 1] = 0.0F;
index 57b5c3399bfe19259c8498086526128283c74098..e35beceb3ff8c3b898b9b1a889f2a59976fef24e 100644 (file)
--- a/puzzles.h
+++ b/puzzles.h
@@ -218,6 +218,10 @@ void obfuscate_bitmap(unsigned char *bmp, int bits, int decode);
 char *bin2hex(const unsigned char *in, int inlen);
 unsigned char *hex2bin(const char *in, int outlen);
 
+/* Sets (and possibly dims) background from frontend default colour,
+ * and auto-generates highlight and lowlight colours too. */
+void game_mkhighlight(frontend *fe, float *ret,
+                      int background, int highlight, int lowlight);
 
 /*
  * version.c
index 5eb0715f39936af3820b9cb8c5946e7d419db921..21c10c2856a80e4635d00ec5910fb60df8236b0e 100644 (file)
--- a/sixteen.c
+++ b/sixteen.c
@@ -706,28 +706,11 @@ static float *game_colours(frontend *fe, game_state *state, int *ncolours)
 {
     float *ret = snewn(3 * NCOLOURS, float);
     int i;
-    float max;
 
-    frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
+    game_mkhighlight(fe, ret, COL_BACKGROUND, COL_HIGHLIGHT, COL_LOWLIGHT);
 
-    /*
-     * Drop the background colour so that the highlight is
-     * noticeably brighter than it while still being under 1.
-     */
-    max = ret[COL_BACKGROUND*3];
-    for (i = 1; i < 3; i++)
-        if (ret[COL_BACKGROUND*3+i] > max)
-            max = ret[COL_BACKGROUND*3+i];
-    if (max * 1.2F > 1.0F) {
-        for (i = 0; i < 3; i++)
-            ret[COL_BACKGROUND*3+i] /= (max * 1.2F);
-    }
-
-    for (i = 0; i < 3; i++) {
-        ret[COL_HIGHLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 1.2F;
-        ret[COL_LOWLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.8F;
+    for (i = 0; i < 3; i++)
         ret[COL_TEXT * 3 + i] = 0.0;
-    }
 
     *ncolours = NCOLOURS;
     return ret;
index ac6f385dfd47f98e40527de91a865e5e604bc1fa..3e9e74e6f2450e398ad5733c6a40b8a6ad122ed6 100644 (file)
--- a/twiddle.c
+++ b/twiddle.c
@@ -765,27 +765,11 @@ static float *game_colours(frontend *fe, game_state *state, int *ncolours)
 {
     float *ret = snewn(3 * NCOLOURS, float);
     int i;
-    float max;
 
-    frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
-
-    /*
-     * Drop the background colour so that the highlight is
-     * noticeably brighter than it while still being under 1.
-     */
-    max = ret[COL_BACKGROUND*3];
-    for (i = 1; i < 3; i++)
-        if (ret[COL_BACKGROUND*3+i] > max)
-            max = ret[COL_BACKGROUND*3+i];
-    if (max * 1.2F > 1.0F) {
-        for (i = 0; i < 3; i++)
-            ret[COL_BACKGROUND*3+i] /= (max * 1.2F);
-    }
+    game_mkhighlight(fe, ret, COL_BACKGROUND, COL_HIGHLIGHT, COL_LOWLIGHT);
 
     for (i = 0; i < 3; i++) {
-        ret[COL_HIGHLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 1.2F;
         ret[COL_HIGHLIGHT_GENTLE * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 1.1F;
-        ret[COL_LOWLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.8F;
         ret[COL_LOWLIGHT_GENTLE * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.9F;
         ret[COL_TEXT * 3 + i] = 0.0;
     }