Start development of color-palette class

This commit is contained in:
Mario Hüttel 2019-05-20 20:07:45 +02:00
parent 5cfd93c18d
commit 1f7f3118fa
2 changed files with 193 additions and 0 deletions

View File

@ -0,0 +1,70 @@
/*
* GDSII-Converter
* Copyright (C) 2019 Mario Hüttel <mario.huettel@gmx.net>
*
* This file is part of GDSII-Converter.
*
* GDSII-Converter is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* GDSII-Converter is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GDSII-Converter. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file color-palette.h
* @brief Class representing a color palette
* @author Mario Hüttel <mario.huettel@gmx.net>
*/
#ifndef _COLOR_PALETTE_H_
#define _COLOR_PALETTE_H_
#include <glib.h>
#include <gtk/gtk.h>
G_BEGIN_DECLS
G_DECLARE_FINAL_TYPE(ColorPalette, color_palette, GDS_RENDER, COLOR_PALETTE, GObject);
#define TYPE_GDS_RENDER_COLOR_PALETTE (color_palette_get_type())
/**
* @brief Create a new object with from a resource containing the html hex color scheme
* @param resource_name Name of the resource
* @return New object
*/
ColorPalette *color_palette_new_from_resource(char *resource_name);
/**
* @brief Get the n-th color in the palette identified by the index.
*
* This function fills the nth color into the supplied \p color.
* \p color is returned.
*
* If \p color is NULL, a new GdkRGBA is created and returned.
* This element must be freed afterwards.
*
* @param palette Color palette
* @param color GdkRGBA struct to fill data in. May be NULL.
* @param index Index of color. Starts at 0
* @return GdkRGBA color. If \p color is NULL, the returned color must be freed afterwards
*/
GdkRGBA *color_palette_get_color(ColorPalette *palette, GdkRGBA *color, unsigned int index);
/**
* @brief Return amount of stored colors in \p palette
* @param palette Color palette
* @return Count of colors
*/
unsigned int color_palette_get_color_count(ColorPalette *palette);
G_END_DECLS
#endif /* _COLOR_PALETTE_H_ */

123
layer/color-palette.c Normal file
View File

@ -0,0 +1,123 @@
/*
* GDSII-Converter
* Copyright (C) 2019 Mario Hüttel <mario.huettel@gmx.net>
*
* This file is part of GDSII-Converter.
*
* GDSII-Converter is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* GDSII-Converter is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GDSII-Converter. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file color-palette.c
* @brief Class representing a color palette
* @author Mario Hüttel <mario.huettel@gmx.net>
*/
#include <gds-render/layer/color-palette.h>
struct _ColorPalette {
/* Inheritance */
GObject parent;
/* Custom fields */
/** @brief The internal array to store the colors */
GdkRGBA *color_array;
/** @brief The length of the _ColorPalette::color_array array */
unsigned int color_array_length;
gpointer dummy[4];
};
G_DEFINE_TYPE(ColorPalette, color_palette, G_TYPE_OBJECT)
/**
* @brief color_palette_fill_with_resource
* @param palette
* @param resource_name
* @return 0 if successful
*/
static int color_palette_fill_with_resource(ColorPalette *palette, char *resource_name)
{
GBytes *data;
char *char_array;
if (!palette || !resource_name)
return -1;
data = g_resources_lookup_data(resource_name, 0, NULL);
if (!data)
return -2;
g_bytes_unref(data);
return 0;
}
ColorPalette *color_palette_new_from_resource(char *resource_name)
{
ColorPalette *palette;
palette = GDS_RENDER_COLOR_PALETTE(g_object_new(TYPE_GDS_RENDER_COLOR_PALETTE, NULL));
if (palette)
(void)color_palette_fill_with_resource(palette, resource_name);
return palette;
}
GdkRGBA *color_palette_get_color(ColorPalette *palette, GdkRGBA *color, unsigned int index)
{
GdkRGBA *c = NULL;
if (!palette)
goto ret_c;
if (index >= palette->color_array_length)
goto ret_c;
if (color)
c = color;
else
c = (GdkRGBA *)malloc(sizeof(GdkRGBA));
/* Copy color */
c->red = palette->color_array[index].red;
c->green = palette->color_array[index].green;
c->blue = palette->color_array[index].blue;
c->alpha = palette->color_array[index].alpha;
ret_c:
return c;
}
unsigned int color_palette_get_color_count(ColorPalette *palette)
{
unsigned int return_val = 0;
if (palette)
return_val = palette->color_array_length;
return return_val;
}
static void color_palette_class_init(ColorPaletteClass *klass)
{
/* Nothing to do for now */
return;
}
static void color_palette_init(ColorPalette *self)
{
self->color_array = NULL;
self->color_array_length = 0;
}