/* PIKA - Photo and Image Kooker Application
 * a rebranding of The GNU Image Manipulation Program (created with heckimp)
 * A derived work which may be trivial. However, any changes may be (C)2023 by Aldercone Studio
 *
 * Original copyright, applying to most contents (license remains unchanged): 
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#ifndef __PIKA_GRADIENT_H__
#define __PIKA_GRADIENT_H__


#include "pikadata.h"


#define PIKA_GRADIENT_DEFAULT_SAMPLE_SIZE 40


struct _PikaGradientSegment
{
  gdouble                  left, middle, right;

  PikaGradientColor        left_color_type;
  PikaRGB                  left_color;
  PikaGradientColor        right_color_type;
  PikaRGB                  right_color;

  PikaGradientSegmentType  type;          /*  Segment's blending function  */
  PikaGradientSegmentColor color;         /*  Segment's coloring type      */

  PikaGradientSegment     *prev;
  PikaGradientSegment     *next;
};


#define PIKA_TYPE_GRADIENT            (pika_gradient_get_type ())
#define PIKA_GRADIENT(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), PIKA_TYPE_GRADIENT, PikaGradient))
#define PIKA_GRADIENT_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), PIKA_TYPE_GRADIENT, PikaGradientClass))
#define PIKA_IS_GRADIENT(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PIKA_TYPE_GRADIENT))
#define PIKA_IS_GRADIENT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), PIKA_TYPE_GRADIENT))
#define PIKA_GRADIENT_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), PIKA_TYPE_GRADIENT, PikaGradientClass))


typedef struct _PikaGradientClass PikaGradientClass;

struct _PikaGradient
{
  PikaData             parent_instance;

  PikaGradientSegment *segments;
};

struct _PikaGradientClass
{
  PikaDataClass  parent_class;
};


GType                 pika_gradient_get_type       (void) G_GNUC_CONST;

PikaData            * pika_gradient_new            (PikaContext   *context,
                                                    const gchar   *name);
PikaData            * pika_gradient_get_standard   (PikaContext   *context);

PikaGradientSegment * pika_gradient_get_color_at   (PikaGradient        *gradient,
                                                    PikaContext         *context,
                                                    PikaGradientSegment *seg,
                                                    gdouble              pos,
                                                    gboolean             reverse,
                                                    PikaGradientBlendColorSpace blend_color_space,
                                                    PikaRGB             *color);
PikaGradientSegment * pika_gradient_get_segment_at (PikaGradient  *grad,
                                                    gdouble        pos);
void                  pika_gradient_split_at       (PikaGradient         *gradient,
                                                    PikaContext          *context,
                                                    PikaGradientSegment  *seg,
                                                    gdouble               pos,
                                                    PikaGradientBlendColorSpace blend_color_space,
                                                    PikaGradientSegment **newl,
                                                    PikaGradientSegment **newr);

gboolean          pika_gradient_has_fg_bg_segments (PikaGradient  *gradient);
PikaGradient    * pika_gradient_flatten            (PikaGradient  *gradient,
                                                    PikaContext   *context);


/*  gradient segment functions  */

PikaGradientSegment * pika_gradient_segment_new       (void);
PikaGradientSegment * pika_gradient_segment_get_last  (PikaGradientSegment *seg);
PikaGradientSegment * pika_gradient_segment_get_first (PikaGradientSegment *seg);
PikaGradientSegment * pika_gradient_segment_get_nth   (PikaGradientSegment *seg,
                                                       gint                 index);

void                  pika_gradient_segment_free      (PikaGradientSegment *seg);
void                  pika_gradient_segments_free     (PikaGradientSegment *seg);

void    pika_gradient_segment_split_midpoint  (PikaGradient         *gradient,
                                               PikaContext          *context,
                                               PikaGradientSegment  *lseg,
                                               PikaGradientBlendColorSpace blend_color_space,
                                               PikaGradientSegment **newl,
                                               PikaGradientSegment **newr);
void    pika_gradient_segment_split_uniform   (PikaGradient         *gradient,
                                               PikaContext          *context,
                                               PikaGradientSegment  *lseg,
                                               gint                  parts,
                                               PikaGradientBlendColorSpace blend_color_space,
                                               PikaGradientSegment **newl,
                                               PikaGradientSegment **newr);

/* Colors Setting/Getting Routines */
void    pika_gradient_segment_get_left_color  (PikaGradient         *gradient,
                                               PikaGradientSegment  *seg,
                                               PikaRGB              *color);

void    pika_gradient_segment_set_left_color  (PikaGradient         *gradient,
                                               PikaGradientSegment  *seg,
                                               const PikaRGB        *color);


void    pika_gradient_segment_get_right_color (PikaGradient         *gradient,
                                               PikaGradientSegment  *seg,
                                               PikaRGB              *color);

void    pika_gradient_segment_set_right_color (PikaGradient         *gradient,
                                               PikaGradientSegment  *seg,
                                               const PikaRGB        *color);


PikaGradientColor
pika_gradient_segment_get_left_color_type     (PikaGradient         *gradient,
                                               PikaGradientSegment  *seg);

void
pika_gradient_segment_set_left_color_type     (PikaGradient         *gradient,
                                               PikaGradientSegment  *seg,
                                               PikaGradientColor     color_type);


PikaGradientColor
pika_gradient_segment_get_right_color_type    (PikaGradient         *gradient,
                                               PikaGradientSegment  *seg);

void
pika_gradient_segment_set_right_color_type    (PikaGradient         *gradient,
                                               PikaGradientSegment  *seg,
                                               PikaGradientColor     color_type);


void
pika_gradient_segment_get_left_flat_color     (PikaGradient         *gradient,
                                               PikaContext          *context,
                                               PikaGradientSegment  *seg,
                                               PikaRGB              *color);


void
pika_gradient_segment_get_right_flat_color    (PikaGradient         *gradient,
                                               PikaContext          *context,
                                               PikaGradientSegment  *seg,
                                               PikaRGB              *color);


/* Position Setting/Getting Routines */
/* (Setters return the position after it was set) */
gdouble pika_gradient_segment_get_left_pos    (PikaGradient         *gradient,
                                               PikaGradientSegment  *seg);
gdouble pika_gradient_segment_set_left_pos    (PikaGradient         *gradient,
                                               PikaGradientSegment  *seg,
                                               gdouble               pos);

gdouble pika_gradient_segment_get_right_pos   (PikaGradient         *gradient,
                                               PikaGradientSegment  *seg);
gdouble pika_gradient_segment_set_right_pos   (PikaGradient         *gradient,
                                               PikaGradientSegment  *seg,
                                               gdouble               pos);

gdouble pika_gradient_segment_get_middle_pos  (PikaGradient         *gradient,
                                               PikaGradientSegment  *seg);
gdouble pika_gradient_segment_set_middle_pos  (PikaGradient         *gradient,
                                               PikaGradientSegment  *seg,
                                               gdouble               pos);

/* Getting/Setting the Blending Function/Coloring Type */
PikaGradientSegmentType
pika_gradient_segment_get_blending_function   (PikaGradient         *gradient,
                                               PikaGradientSegment  *seg);
PikaGradientSegmentColor
pika_gradient_segment_get_coloring_type       (PikaGradient         *gradient,
                                               PikaGradientSegment  *seg);

/*
 * If the second segment is NULL, these functions will process
 * until the end of the string.
 * */
gint    pika_gradient_segment_range_get_n_segments
                                              (PikaGradient         *gradient,
                                               PikaGradientSegment  *range_l,
                                               PikaGradientSegment  *range_r);

void    pika_gradient_segment_range_compress  (PikaGradient         *gradient,
                                               PikaGradientSegment  *range_l,
                                               PikaGradientSegment  *range_r,
                                               gdouble               new_l,
                                               gdouble               new_r);
void    pika_gradient_segment_range_blend     (PikaGradient         *gradient,
                                               PikaGradientSegment  *lseg,
                                               PikaGradientSegment  *rseg,
                                               const PikaRGB        *rgb1,
                                               const PikaRGB        *rgb2,
                                               gboolean              blend_colors,
                                               gboolean              blend_opacity);

void    pika_gradient_segment_range_set_blending_function
                                              (PikaGradient         *gradient,
                                               PikaGradientSegment  *start_seg,
                                               PikaGradientSegment  *end_seg,
                                               PikaGradientSegmentType new_type);

void    pika_gradient_segment_range_set_coloring_type
                                              (PikaGradient         *gradient,
                                               PikaGradientSegment  *start_seg,
                                               PikaGradientSegment  *end_seg,
                                               PikaGradientSegmentColor new_color);

void    pika_gradient_segment_range_flip      (PikaGradient         *gradient,
                                               PikaGradientSegment  *start_seg,
                                               PikaGradientSegment  *end_seg,
                                               PikaGradientSegment **final_start_seg,
                                               PikaGradientSegment **final_end_seg);

void    pika_gradient_segment_range_replicate (PikaGradient         *gradient,
                                               PikaGradientSegment  *start_seg,
                                               PikaGradientSegment  *end_seg,
                                               gint                  replicate_times,
                                               PikaGradientSegment **final_start_seg,
                                               PikaGradientSegment **final_end_seg);

void    pika_gradient_segment_range_split_midpoint
                                              (PikaGradient         *gradient,
                                               PikaContext          *context,
                                               PikaGradientSegment  *start_seg,
                                               PikaGradientSegment  *end_seg,
                                               PikaGradientBlendColorSpace blend_color_space,
                                               PikaGradientSegment **final_start_seg,
                                               PikaGradientSegment **final_end_seg);

void    pika_gradient_segment_range_split_uniform
                                              (PikaGradient         *gradient,
                                               PikaContext          *context,
                                               PikaGradientSegment  *start_seg,
                                               PikaGradientSegment  *end_seg,
                                               gint                  parts,
                                               PikaGradientBlendColorSpace blend_color_space,
                                               PikaGradientSegment **final_start_seg,
                                               PikaGradientSegment **final_end_seg);

void    pika_gradient_segment_range_delete    (PikaGradient         *gradient,
                                               PikaGradientSegment  *start_seg,
                                               PikaGradientSegment  *end_seg,
                                               PikaGradientSegment **final_start_seg,
                                               PikaGradientSegment **final_end_seg);

void    pika_gradient_segment_range_merge     (PikaGradient         *gradient,
                                               PikaGradientSegment  *start_seg,
                                               PikaGradientSegment  *end_seg,
                                               PikaGradientSegment **final_start_seg,
                                               PikaGradientSegment **final_end_seg);

void    pika_gradient_segment_range_recenter_handles
                                              (PikaGradient         *gradient,
                                               PikaGradientSegment  *start_seg,
                                               PikaGradientSegment  *end_seg);
void    pika_gradient_segment_range_redistribute_handles
                                              (PikaGradient         *gradient,
                                               PikaGradientSegment  *start_seg,
                                               PikaGradientSegment  *end_seg);

gdouble pika_gradient_segment_range_move      (PikaGradient         *gradient,
                                               PikaGradientSegment  *range_l,
                                               PikaGradientSegment  *range_r,
                                               gdouble               delta,
                                               gboolean              control_compress);


#endif /* __PIKA_GRADIENT_H__ */
