//+------------------------------------------------------------------+
//|                                                     PdfColor.mqh |
//|                                Copyright 2026, Sandro Begashvili |
//|                    https://www.mql5.com/en/users/sandrobegashvil |
//|                           Telegram: https://t.me/MrMQL5Developer |
//|                                                                  |
//| Building a PDF Creation Library in MQL5                          |
//| Part 4: Lines, Boxes, and Color                                  |
//|                                                                  |
//| PDF has no color type. It has operators that take one, three, or |
//| four numbers in the range 0..1. This file stores colors as       |
//| bytes, which is how everyone thinks about them, and converts     |
//| them on output.                                                  |
//+------------------------------------------------------------------+
#ifndef PDF5_COLOR_MQH
#define PDF5_COLOR_MQH

#include "PdfDefines.mqh"

//+------------------------------------------------------------------+
//| SPdfColor                                                        |
//|                                                                  |
//| An RGB color with one byte per channel.                          |
//+------------------------------------------------------------------+
struct SPdfColor
  {
   uchar             r;   // red, 0..255
   uchar             g;   // green, 0..255
   uchar             b;   // blue, 0..255
  };

//+------------------------------------------------------------------+
//| PdfRgb                                                           |
//|                                                                  |
//| Builds a color from its red, green, and blue components.         |
//+------------------------------------------------------------------+
SPdfColor PdfRgb(const uchar red, const uchar green, const uchar blue)
  {
   SPdfColor c;
   c.r = red;
   c.g = green;
   c.b = blue;
   return(c);
  }

//+------------------------------------------------------------------+
//| PdfHex                                                           |
//|                                                                  |
//| Builds a color from a 0xRRGGBB literal.                          |
//|                                                                  |
//| This is the form used throughout the library, because a hex      |
//| triplet is what a designer hands over and what a CSS file holds. |
//| Note that the MQL5 `color` type is 0xBBGGRR, which is why        |
//| PdfFromMql exists separately.                                    |
//+------------------------------------------------------------------+
SPdfColor PdfHex(const uint rrggbb)
  {
   SPdfColor c;
   c.r = (uchar)((rrggbb >> 16) & 0xFF);
   c.g = (uchar)((rrggbb >> 8) & 0xFF);
   c.b = (uchar)(rrggbb & 0xFF);
   return(c);
  }

//+------------------------------------------------------------------+
//| PdfFromMql                                                       |
//|                                                                  |
//| Converts an MQL5 color, which stores blue in the high byte.      |
//+------------------------------------------------------------------+
SPdfColor PdfFromMql(const color value)
  {
   SPdfColor c;
   c.r = (uchar)(value & 0xFF);
   c.g = (uchar)((value >> 8) & 0xFF);
   c.b = (uchar)((value >> 16) & 0xFF);
   return(c);
  }

//+------------------------------------------------------------------+
//| PdfGray                                                          |
//|                                                                  |
//| Returns a gray of the given lightness, 0 = black, 255 = white.   |
//+------------------------------------------------------------------+
SPdfColor PdfGray(const uchar level)
  {
   return(PdfRgb(level, level, level));
  }

//+------------------------------------------------------------------+
//| PdfMix                                                           |
//|                                                                  |
//| Blends two colors. t = 0 returns a, t = 1 returns b, and values  |
//| outside 0..1 are clamped.                                        |
//+------------------------------------------------------------------+
SPdfColor PdfMix(const SPdfColor &a, const SPdfColor &b, const double t)
  {
   double k = t;
   if(k < 0.0)
      k = 0.0;
   if(k > 1.0)
      k = 1.0;

   SPdfColor c;
   c.r = (uchar)MathRound(a.r + (b.r - a.r) * k);
   c.g = (uchar)MathRound(a.g + (b.g - a.g) * k);
   c.b = (uchar)MathRound(a.b + (b.b - a.b) * k);
   return(c);
  }

//+------------------------------------------------------------------+
//| PdfLuma                                                          |
//|                                                                  |
//| Returns the perceived brightness of a color, 0..255.             |
//|                                                                  |
//| The weights come from the luma formula: the eye is far more      |
//| sensitive to green than to blue. It is used to decide whether a  |
//| label drawn on a colored background should be black or white.    |
//+------------------------------------------------------------------+
uchar PdfLuma(const SPdfColor &c)
  {
   return((uchar)MathRound(0.299 * c.r + 0.587 * c.g + 0.114 * c.b));
  }

//+------------------------------------------------------------------+
//| PdfContrastInk                                                   |
//|                                                                  |
//| Returns black or white, whichever is more readable on the given  |
//| background.                                                      |
//+------------------------------------------------------------------+
SPdfColor PdfContrastInk(const SPdfColor &background)
  {
   if(PdfLuma(background) > 150)
      return(PdfRgb(0, 0, 0));
   return(PdfRgb(255, 255, 255));
  }

//--- A small default palette, so that demos and reports look consistent
//--- without every caller inventing its own colors
#define PDF_WHITE      PdfHex(0xFFFFFF)
#define PDF_BLACK      PdfHex(0x000000)
#define PDF_INK        PdfHex(0x1B1F24)
#define PDF_INK_SOFT   PdfHex(0x57606A)
#define PDF_BORDER     PdfHex(0xC8CDD4)
#define PDF_PANEL      PdfHex(0xF6F8FA)
#define PDF_BLUE       PdfHex(0x1F6FEB)
#define PDF_GREEN      PdfHex(0x1A7F37)
#define PDF_RED        PdfHex(0xCF222E)
#define PDF_AMBER      PdfHex(0xBF8700)
#define PDF_PURPLE     PdfHex(0x8250DF)
#define PDF_GREY       PdfHex(0x8C959F)

#endif // PDF5_COLOR_MQH
//+------------------------------------------------------------------+
