replace colors library, plus a few warning cleanups
authorjweigele <jweigele@local>
Mon, 21 Jul 2025 17:59:16 +0000 (10:59 -0700)
committerjweigele <jweigele@local>
Mon, 21 Jul 2025 18:00:11 +0000 (11:00 -0700)
package.json
resources/images/metro.png [deleted file]
resources/images/metrobw.png [new file with mode: 0644]
src/c/gbitmap_color_palette_manipulator.c [new file with mode: 0644]
src/c/gbitmap_color_palette_manipulator.h [new file with mode: 0644]
src/c/main.c

index 33f63e55836dec301554a4890fb7d09faec31dae..9df79968d93fa5febbd82383f792a64421a6df65 100644 (file)
@@ -27,7 +27,7 @@
           {
               "type": "bitmap",
               "name": "IMAGE_METRO",
-              "file": "images/metro.png",
+              "file": "images/metrobw.png",
               "memoryFormat": "Smallest",
               "spaceOptimization": "memory"
           },
diff --git a/resources/images/metro.png b/resources/images/metro.png
deleted file mode 100644 (file)
index 6700346..0000000
Binary files a/resources/images/metro.png and /dev/null differ
diff --git a/resources/images/metrobw.png b/resources/images/metrobw.png
new file mode 100644 (file)
index 0000000..73cc138
Binary files /dev/null and b/resources/images/metrobw.png differ
diff --git a/src/c/gbitmap_color_palette_manipulator.c b/src/c/gbitmap_color_palette_manipulator.c
new file mode 100644 (file)
index 0000000..bc23363
--- /dev/null
@@ -0,0 +1,195 @@
+
+#include "gbitmap_color_palette_manipulator.h"
+
+#ifdef PBL_COLOR
+
+// #define SHOW_APP_LOGS
+
+char* get_gbitmapformat_text(GBitmapFormat format){
+       switch (format) {
+               case GBitmapFormat1Bit: return "GBitmapFormat1Bit";
+               case GBitmapFormat8Bit: return "GBitmapFormat8Bit";
+               case GBitmapFormat1BitPalette: return "GBitmapFormat1BitPalette";
+               case GBitmapFormat2BitPalette: return "GBitmapFormat2BitPalette";
+               case GBitmapFormat4BitPalette: return "GBitmapFormat4BitPalette";
+
+               default: return "UNKNOWN FORMAT";
+       }
+
+}
+
+int get_num_palette_colors(GBitmap *b){
+
+       GBitmapFormat format = gbitmap_get_format(b);
+
+       switch (format) {
+               case GBitmapFormat1Bit: return 0;
+               case GBitmapFormat8Bit: return 0;
+               case GBitmapFormat1BitPalette: return 2;
+               case GBitmapFormat2BitPalette: return 4;
+               case GBitmapFormat4BitPalette: return 16;
+
+               default: return 0;
+       }
+
+}
+
+void replace_gbitmap_color(GColor color_to_replace, GColor replace_with_color, GBitmap *im, BitmapLayer *bml){
+
+       //First determine what the number of colors in the palette
+       int num_palette_items = get_num_palette_colors(im);
+
+       #ifdef SHOW_APP_LOGS
+       APP_LOG(APP_LOG_LEVEL_DEBUG, "Palette has %d items", num_palette_items);
+       #endif
+
+       //Get the gbitmap's current palette
+       GColor *current_palette = gbitmap_get_palette(im);
+
+       //Iterate through the palette finding the color we want to replace and replacing 
+       //it with the new color
+       #ifdef SHOW_APP_LOGS
+       APP_LOG(APP_LOG_LEVEL_DEBUG, "--Replace Color Start--");
+       #endif 
+
+       for(int i = 0; i < num_palette_items; i++){
+
+               #ifdef SHOW_APP_LOGS
+               APP_LOG(APP_LOG_LEVEL_DEBUG, "Palette[%d] = %s (alpha:%d)", i, get_gcolor_text(current_palette[i]),(current_palette[i].argb >>6));
+               #endif
+
+               if ((color_to_replace.argb & 0x3F)==(current_palette[i].argb & 0x3F)){
+
+                       current_palette[i].argb = (current_palette[i].argb & 0xC0)| (replace_with_color.argb & 0x3F);
+                       #ifdef SHOW_APP_LOGS
+                       APP_LOG(APP_LOG_LEVEL_DEBUG, "-------[%d] replaced with %s (alpha:%d)", i, get_gcolor_text(current_palette[i]),(current_palette[i].argb >>6));
+                       #endif
+                       
+               }
+
+       }
+
+       #ifdef SHOW_APP_LOGS
+       APP_LOG(APP_LOG_LEVEL_DEBUG, "--Replace Color End--");
+       #endif
+
+       //Mark the bitmaplayer dirty
+       if(bml != NULL){
+               layer_mark_dirty(bitmap_layer_get_layer(bml));
+       }
+
+}
+
+void gbitmap_fill_all_except(GColor color_to_not_change, GColor fill_color, bool fill_gcolorclear, GBitmap *im, BitmapLayer *bml){
+
+       //First determine what the number of colors in the palette
+       int num_palette_items = get_num_palette_colors(im);
+
+       #ifdef SHOW_APP_LOGS
+       APP_LOG(APP_LOG_LEVEL_DEBUG, "Palette has %d items", num_palette_items);
+       #endif
+
+       //Get the gbitmap's current palette
+       GColor *current_palette = gbitmap_get_palette(im);
+
+       //Iterate through the palette replacing all colors except the color_to_not_change
+       #ifdef SHOW_APP_LOGS
+       APP_LOG(APP_LOG_LEVEL_DEBUG, "--Color Fill Start--");
+       #endif
+
+       for(int i = 0; i < num_palette_items; i++){
+
+               #ifdef SHOW_APP_LOGS
+               APP_LOG(APP_LOG_LEVEL_DEBUG, "Palette[%d] = %s", i, get_gcolor_text(current_palette[i]));
+               #endif
+
+               if(!gcolor_equal(color_to_not_change, current_palette[i])){//all colors except color_to_not_change
+                       if((gcolor_equal(current_palette[i], GColorClear) && fill_gcolorclear) || !gcolor_equal(current_palette[i], GColorClear)){
+                               current_palette[i] = fill_color;
+                               #ifdef SHOW_APP_LOGS
+                               APP_LOG(APP_LOG_LEVEL_DEBUG, "-------[%d] filled with %s", i, get_gcolor_text(current_palette[i]) );
+                               #endif
+                       }
+               }
+
+       }
+       #ifdef SHOW_APP_LOGS
+       APP_LOG(APP_LOG_LEVEL_DEBUG, "--Color Fill End--");
+       #endif
+
+       //Mark the bitmap layer dirty
+       if(bml != NULL){
+               layer_mark_dirty(bitmap_layer_get_layer(bml));
+       }
+
+}
+
+bool gbitmap_color_palette_contains_color(GColor m_color, GBitmap *im){
+
+       int num_palette_items = get_num_palette_colors(im);
+       GColor *current_palette = gbitmap_get_palette(im);
+       for(int i = 0; i < num_palette_items; i++){
+
+               if ((m_color.argb & 0x3F)==(current_palette[i].argb & 0x3F)){
+                       #ifdef SHOW_APP_LOGS
+                       APP_LOG(APP_LOG_LEVEL_DEBUG, "GBitmap contains: %s", get_gcolor_text(current_palette[i]));
+                       #endif
+
+                       return true;
+               }
+
+       }
+
+       #ifdef SHOW_APP_LOGS
+       APP_LOG(APP_LOG_LEVEL_DEBUG, "GBitmap does not contain: %s", get_gcolor_text(m_color));
+       #endif
+
+       return false;
+
+}
+
+void spit_gbitmap_color_palette(GBitmap *im){
+
+       //First determine what the number of colors in the palette
+       int num_palette_items = get_num_palette_colors(im);
+
+       APP_LOG(APP_LOG_LEVEL_DEBUG, "Palette has %d items", num_palette_items);
+
+       GColor *current_palette = gbitmap_get_palette(im);
+
+       APP_LOG(APP_LOG_LEVEL_DEBUG, "--Spit Palette Start--");
+       for(int i = 0; i < num_palette_items; i++){
+
+               APP_LOG(APP_LOG_LEVEL_DEBUG, "Palette[%d] = %s (alpha:%d)", i, get_gcolor_text(current_palette[i]),(current_palette[i].argb >>6) );
+
+       }
+       APP_LOG(APP_LOG_LEVEL_DEBUG, "--Spit Palette End--");
+
+}
+
+const char * GColorsNames[]= {
+ "GColorBlack", "GColorOxfordBlue", "GColorDukeBlue", "GColorBlue", 
+ "GColorDarkGreen", "GColorMidnightGreen", "GColorCobaltBlue", "GColorBlueMoon", 
+ "GColorIslamicGreen", "GColorJaegerGreen", "GColorTiffanyBlue", "GColorVividCerulean", 
+ "GColorGreen", "GColorMalachite", "GColorMediumSpringGreen", "GColorCyan", 
+ "GColorBulgarianRose", "GColorImperialPurple", "GColorIndigo", "GColorElectricUltramarine", 
+ "GColorArmyGreen", "GColorDarkGray", "GColorLiberty", "GColorVeryLightBlue", 
+ "GColorKellyGreen", "GColorMayGreen", "GColorCadetBlue", "GColorPictonBlue",
+ "GColorBrightGreen", "GColorScreaminGreen", "GColorMediumAquamarine", "GColorElectricBlue",
+ "GColorDarkCandyAppleRed", "GColorJazzberryJam", "GColorPurple", "GColorVividViolet",
+ "GColorWindsorTan", "GColorRoseVale", "GColorPurpureus", "GColorLavenderIndigo",
+ "GColorLimerick", "GColorBrass", "GColorLightGray", "GColorBabyBlueEyes",
+ "GColorSpringBud", "GColorInchworm", "GColorMintGreen", "GColorCeleste",
+ "GColorRed", "GColorFolly", "GColorFashionMagenta", "GColorMagenta",
+ "GColorOrange", "GColorSunsetOrange", "GColorBrilliantRose", "GColorShockingPink",
+ "GColorChromeYellow", "GColorRajah", "GColorMelon", "GColorRichBrilliantLavender",
+ "GColorYellow", "GColorIcterine", "GColorPastelYellow", "GColorWhite"
+};
+
+const char* get_gcolor_text(GColor m_color){
+       if(gcolor_equal(m_color, GColorClear))
+               return "GColorClear";
+       return GColorsNames[m_color.argb & 0x3F];
+
+}
+#endif 
diff --git a/src/c/gbitmap_color_palette_manipulator.h b/src/c/gbitmap_color_palette_manipulator.h
new file mode 100644 (file)
index 0000000..4829f56
--- /dev/null
@@ -0,0 +1,10 @@
+#include <pebble.h>
+
+#ifdef PBL_COLOR
+char* get_gbitmapformat_text(GBitmapFormat format);
+const char* get_gcolor_text(GColor m_color);
+void replace_gbitmap_color(GColor color_to_replace, GColor replace_with_color, GBitmap *im, BitmapLayer *bml);
+void spit_gbitmap_color_palette(GBitmap *im);
+bool gbitmap_color_palette_contains_color(GColor m_color, GBitmap *im);
+void gbitmap_fill_all_except(GColor color_to_not_change, GColor fill_color, bool fill_gcolorclear, GBitmap *im, BitmapLayer *bml);
+#endif
\ No newline at end of file
index 223b1dc696d3c8e487f46bf316131869126de14b..0f84b0e1bc815ef2fe3e4a7e126c96e4bbde37f5 100644 (file)
@@ -1,11 +1,10 @@
+#include "gbitmap_color_palette_manipulator.h"
 #include <pebble.h>
 
 static Window *s_main_window;
 static Layer *s_hands_layer;
 static TextLayer *s_time_layer;
 
-static GPath *s_minute_arrow, *s_hour_arrow;
-
 // background stuff
 static BitmapLayer *s_background_layer;
 static GBitmap *s_background_bitmap;
@@ -44,7 +43,6 @@ static void draw_ticks(Layer *layer, GContext *ctx){
 
 
 static void hands_update_proc(Layer *layer, GContext *ctx) {
-    GRect bounds = layer_get_bounds(layer);
   
     const int16_t second_hand_length = 64; 
     const int16_t minute_hand_length = 64;
@@ -103,6 +101,13 @@ static void main_window_load(Window *window) {
 
     // Create GBitmap
     s_background_bitmap = gbitmap_create_with_resource(RESOURCE_ID_IMAGE_METRO);
+    // color replace, only if color pebble
+#ifdef PBL_COLOR    
+    // foreground, black -> user-defined color
+    replace_gbitmap_color(GColorBlack, GColorChromeYellow, s_background_bitmap, NULL);    
+    // background, white -> user-defined color
+    replace_gbitmap_color(GColorWhite, GColorDarkCandyAppleRed, s_background_bitmap, NULL);
+#endif
 
     // Create BitmapLayer to display the GBitmap
     s_background_layer = bitmap_layer_create(bounds);