/* LIBPIKA - The PIKA Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 *
 * This library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <https://www.gnu.org/licenses/>.
 */

#if !defined (__PIKA_COLOR_H_INSIDE__) && !defined (PIKA_COLOR_COMPILATION)
#error "Only <libpikacolor/pikacolor.h> can be included directly."
#endif

#ifndef __PIKA_RGB_H__
#define __PIKA_RGB_H__

G_BEGIN_DECLS

/* For information look into the C source or the html documentation */


/*
 * PIKA_TYPE_RGB
 */

#define PIKA_TYPE_RGB               (pika_rgb_get_type ())
#define PIKA_VALUE_HOLDS_RGB(value) (G_TYPE_CHECK_VALUE_TYPE ((value), PIKA_TYPE_RGB))

GType   pika_rgb_get_type           (void) G_GNUC_CONST;

void    pika_value_get_rgb          (const GValue  *value,
                                     PikaRGB       *rgb);
void    pika_value_set_rgb          (GValue        *value,
                                     const PikaRGB *rgb);


/*
 * PIKA_TYPE_PARAM_RGB
 */

#define PIKA_TYPE_PARAM_RGB           (pika_param_rgb_get_type ())
#define PIKA_IS_PARAM_SPEC_RGB(pspec) (G_TYPE_CHECK_INSTANCE_TYPE ((pspec), PIKA_TYPE_PARAM_RGB))

typedef struct _PikaParamSpecRGB PikaParamSpecRGB;

GType        pika_param_rgb_get_type         (void) G_GNUC_CONST;

GParamSpec * pika_param_spec_rgb             (const gchar    *name,
                                              const gchar    *nick,
                                              const gchar    *blurb,
                                              gboolean        has_alpha,
                                              const PikaRGB  *default_value,
                                              GParamFlags     flags);

void         pika_param_spec_rgb_get_default (GParamSpec     *pspec,
                                              PikaRGB        *default_value);
gboolean     pika_param_spec_rgb_has_alpha   (GParamSpec     *pspec);


/*  RGB and RGBA color types and operations taken from LibGCK  */

/**
 * PikaRGBCompositeMode:
 * @PIKA_RGB_COMPOSITE_NONE: don't do compositing
 * @PIKA_RGB_COMPOSITE_NORMAL: composite on top
 * @PIKA_RGB_COMPOSITE_BEHIND: composite behind
 **/
typedef enum
{
  PIKA_RGB_COMPOSITE_NONE = 0,
  PIKA_RGB_COMPOSITE_NORMAL,
  PIKA_RGB_COMPOSITE_BEHIND
} PikaRGBCompositeMode;


void      pika_rgb_set             (PikaRGB       *rgb,
                                    gdouble        red,
                                    gdouble        green,
                                    gdouble        blue);
void      pika_rgb_set_alpha       (PikaRGB       *rgb,
                                    gdouble        alpha);

void      pika_rgb_set_pixel       (PikaRGB       *rgb,
                                    const Babl    *format,
                                    gconstpointer  pixel);
void      pika_rgb_get_pixel       (const PikaRGB *rgb,
                                    const Babl    *format,
                                    gpointer       pixel);

void      pika_rgb_set_uchar       (PikaRGB       *rgb,
                                    guchar         red,
                                    guchar         green,
                                    guchar         blue);
void      pika_rgb_get_uchar       (const PikaRGB *rgb,
                                    guchar        *red,
                                    guchar        *green,
                                    guchar        *blue);

gboolean  pika_rgb_parse_name      (PikaRGB       *rgb,
                                    const gchar   *name,
                                    gint           len);
gboolean  pika_rgb_parse_hex       (PikaRGB       *rgb,
                                    const gchar   *hex,
                                    gint           len);
gboolean  pika_rgb_parse_css       (PikaRGB       *rgb,
                                    const gchar   *css,
                                    gint           len);

void      pika_rgb_add             (PikaRGB       *rgb1,
                                    const PikaRGB *rgb2);
void      pika_rgb_subtract        (PikaRGB       *rgb1,
                                    const PikaRGB *rgb2);
void      pika_rgb_multiply        (PikaRGB       *rgb1,
                                    gdouble        factor);
gdouble   pika_rgb_distance        (const PikaRGB *rgb1,
                                    const PikaRGB *rgb2);

gdouble   pika_rgb_max             (const PikaRGB *rgb);
gdouble   pika_rgb_min             (const PikaRGB *rgb);
void      pika_rgb_clamp           (PikaRGB       *rgb);

void      pika_rgb_gamma           (PikaRGB       *rgb,
                                    gdouble        gamma);

gdouble   pika_rgb_luminance       (const PikaRGB *rgb);
guchar    pika_rgb_luminance_uchar (const PikaRGB *rgb);

void      pika_rgb_composite       (PikaRGB              *color1,
                                    const PikaRGB        *color2,
                                    PikaRGBCompositeMode  mode);

/*  access to the list of color names  */
void      pika_rgb_list_names      (const gchar ***names,
                                    PikaRGB      **colors,
                                    gint          *n_colors);


void      pika_rgba_set            (PikaRGB       *rgba,
                                    gdouble        red,
                                    gdouble        green,
                                    gdouble        blue,
                                    gdouble        alpha);

void      pika_rgba_set_pixel      (PikaRGB       *rgba,
                                    const Babl    *format,
                                    gconstpointer  pixel);
void      pika_rgba_get_pixel      (const PikaRGB *rgba,
                                    const Babl    *format,
                                    gpointer       pixel);

void      pika_rgba_set_uchar      (PikaRGB       *rgba,
                                    guchar         red,
                                    guchar         green,
                                    guchar         blue,
                                    guchar         alpha);
void      pika_rgba_get_uchar      (const PikaRGB *rgba,
                                    guchar        *red,
                                    guchar        *green,
                                    guchar        *blue,
                                    guchar        *alpha);

gboolean  pika_rgba_parse_css      (PikaRGB       *rgba,
                                    const gchar   *css,
                                    gint           len);

void      pika_rgba_add            (PikaRGB       *rgba1,
                                    const PikaRGB *rgba2);
void      pika_rgba_subtract       (PikaRGB       *rgba1,
                                    const PikaRGB *rgba2);
void      pika_rgba_multiply       (PikaRGB       *rgba,
                                    gdouble        factor);

gdouble   pika_rgba_distance       (const PikaRGB *rgba1,
                                    const PikaRGB *rgba2);



/*  Map D50-adapted sRGB to luminance  */

/*
 * The weights to compute true CIE luminance from linear red, green
 * and blue as defined by the sRGB color space specs in an ICC profile
 * color managed application. The weights below have been chromatically
 * adapted from D65 (as specified by the sRGB color space specs)
 * to D50 (as specified by D50 illuminant values in the ICC V4 specs).
 */

#define PIKA_RGB_LUMINANCE_RED    (0.22248840)
#define PIKA_RGB_LUMINANCE_GREEN  (0.71690369)
#define PIKA_RGB_LUMINANCE_BLUE   (0.06060791)

#define PIKA_RGB_LUMINANCE(r,g,b) ((r) * PIKA_RGB_LUMINANCE_RED   + \
                                   (g) * PIKA_RGB_LUMINANCE_GREEN + \
                                   (b) * PIKA_RGB_LUMINANCE_BLUE)


G_END_DECLS

#endif  /* __PIKA_RGB_H__ */
