From d601c942ec8fcd486fac9fe367098d8ac28e8a42 Mon Sep 17 00:00:00 2001 From: jweigele Date: Mon, 21 Jul 2025 10:59:16 -0700 Subject: [PATCH] replace colors library, plus a few warning cleanups --- package.json | 2 +- resources/images/{metro.png => metrobw.png} | Bin 1105 -> 1105 bytes src/c/gbitmap_color_palette_manipulator.c | 195 ++++++++++++++++++++ src/c/gbitmap_color_palette_manipulator.h | 10 + src/c/main.c | 11 +- 5 files changed, 214 insertions(+), 4 deletions(-) rename resources/images/{metro.png => metrobw.png} (86%) create mode 100644 src/c/gbitmap_color_palette_manipulator.c create mode 100644 src/c/gbitmap_color_palette_manipulator.h diff --git a/package.json b/package.json index 33f63e5..9df7996 100644 --- a/package.json +++ b/package.json @@ -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/metrobw.png similarity index 86% rename from resources/images/metro.png rename to resources/images/metrobw.png index 67003467dacac46e4cb9ee855a81ca2e6f1b1ab4..73cc138ff24be31793ba14a0abf1b1aeec726fbc 100644 GIT binary patch delta 63 zcmcb}agk%g4o0K@|Nk>EFoYhuTnD5$3p^r=85s1GL71^(seKtxkiEpy*OmPxyQrX& P&Vp|hmp8v+OlJZBrot3n delta 63 zcmcb}agk%g4n`ve28REk40nuGjDal90*}aI1_nK45N51cYF`EvWH0gbb!C6aE+S+t OY@VfhYV#|`bS3~17Z73q diff --git a/src/c/gbitmap_color_palette_manipulator.c b/src/c/gbitmap_color_palette_manipulator.c new file mode 100644 index 0000000..bc23363 --- /dev/null +++ b/src/c/gbitmap_color_palette_manipulator.c @@ -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 index 0000000..4829f56 --- /dev/null +++ b/src/c/gbitmap_color_palette_manipulator.h @@ -0,0 +1,10 @@ +#include + +#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 diff --git a/src/c/main.c b/src/c/main.c index 223b1dc..0f84b0e 100644 --- a/src/c/main.c +++ b/src/c/main.c @@ -1,11 +1,10 @@ +#include "gbitmap_color_palette_manipulator.h" #include 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); -- 2.30.2