This library was created in order to manipulate the color palettes of gbitmaps at runtime. It will be useful if you are trying to adapt your black and white pebble app and app icons for Pebble Time (colors). Instead of including resources for each color of an image/icon, you can just change it's color palette with this library.
The library includes a sample project which loads a picture and spits out its color palette. You can then decide which colors to replace.
Before you start. Ensure that your resources are of type pbi8
or png
in your appinfo.json
file. Black and White images must be of type pbi8
or they will be imported as 1 bit non paletized images. Resources of type pbi
will not work since they do not have a color palette.
"resources": {
"media": [
{
"file": "images/android_bw.png",
"name": "IMAGE_ANDROID",
"type": "pbi8"
},
{
"file": "images/star.png",
"name": "IMAGE_STAR",
"type": "png"
}
]
},
The following library calls are available:
char* get_gbitmapformat_text(GBitmapFormat format);
This function will return the GBitmapFormat text for the given GBitmapFormat.
char* get_gcolor_text(GColor m_color);
This function will return the text of the given GColor (ex: "GColorDukeBlue")
bool gbitmap_color_palette_contains_color(GColor m_color, GBitmap *im);
This function will return true if the provided gbitmap contains the specified color. False otherwise.
void spit_gbitmap_color_palette(GBitmap *im);
This function will spit out the number of colors in a gbitmap and will list which colors it contains. This is one of the most important functions in this library since you'll want to pass your gbitmap to it the first time in order to determine which colors it contains; which you'll use in the next function.
void gbitmap_fill_all_except(GColor color_to_not_change, GColor fill_color, bool fill_gcolorclear, GBitmap *im, BitmapLayer *bml);
This function will replace all colors in the gbitmap's palette except for the color your specify not to change. Tip: Very useful for filling the background color of a icon. I use this before setting a new icon color to clean up any stray colors, leaving only the background color and icon color. Pass NULL to *bml if you do not want to update a BitmapLayer (useful for gbitmaps on your action bar). Allows you to set whether you want GColorClear to be replaced with the fill color.
void replace_gbitmap_color(GColor color_to_replace, GColor replace_with_color, GBitmap *im, BitmapLayer *bml);
This is function allows you to pass in a gbitmap, the color you want to replace and the target color. You also pass your BitmapLayer to this function so that it can automatically be marked dirty. This is an all in one function; it replaces the specified color and automatically updates the BitmapLayer. Pass NULL to *bml if you do not want to update a BitmapLayer (useful for gbitmaps on your action bar).
Including the library in your project
-
- Copy
gbitmap_color_palette_manipulator.c
andgbitmap_color_palette_manipulator.h
into your project.
- Copy
-
- Include the following at the top of your C file
#include "gbitmap_color_palette_manipulator.h"
.
- Include the following at the top of your C file
-
- Use the functions you require.
-
- IMPORTANT: Comment out
#define SHOW_APP_LOGS
when deploying your production app. Otherwise, calls to the library will be slowed down by redundant text display function calls while the app is running on your user's devices.
- IMPORTANT: Comment out
Example uses
Cleaning up Black and White images for better color manipulation. This will set all palette entries to White except for the Black of your image/icon:
-
- Ensure that the Black and White image is of resource type
pbi8
in yourappinfo.json
file.
- Ensure that the Black and White image is of resource type
-
- Create the gbitmap from resource.
-
- Perform the following function call on the gbitmap
gbitmap_fill_all_except(GColorBlack, GColorWhite, true, your_gbitmap, NULL);
- Perform the following function call on the gbitmap
-
- The gbitmap now has a palette that contains only Black (for the icon) and White (as the icon background). You can now manipulate the palette of this gbitmap more efficiently.
Helper Functions
Include these in your project to save some time with changing icon colors. Use set_icon_color
by passing it your newly loaded gbitmap and set first_adaptation true
for the first icon color change, false
for subsequent color changes.
#ifdef PBL_COLOR
//sets black icon with white background to black icon with clear background
static void convert_bw_to_clear_bg(GBitmap *icon){
if(icon == NULL)return;
gbitmap_fill_all_except(GColorBlack, GColorClear, true, icon, NULL);//Fill the background
}
void set_icon_color(GBitmap *icon, GColor m_color, bool first_adaptation){
if(icon == NULL)return;
if(first_adaptation){
convert_bw_to_clear_bg(icon);
}
gbitmap_fill_all_except(GColorClear, m_color, true, icon, NULL);//Fill the background
}
#endif
Credits: I'd like to thank @gregoiresage and @ron064 for their contributions to the library.
Jonathan.