local M = {}

M.red = "#ff0000"
M.green = "#00ff00"
M.blue = "#0000ff"
M.white = "#ffffff"
M.black = "#000000"
M.gray = "#808080"
M.yellow = "#ffff00"
M.cyan = "#00ffff"
M.magenta = "#ff00ff"
M.orange = "#ffa500"
M.purple = "#800080"
M.brown = "#a52a2a"
M.pink = "#ffc0cb"

-- SVG colors
M.aliceblue = "#f0f8ff"
M.antiquewhite = "#faebd7"
M.aqua = "#00ffff"
M.aquamarine = "#7fffd4"
M.azure = "#f0ffff"
M.beige = "#f5f5dc"
M.bisque = "#ffe4c4"
M.blanchedalmond = "#ffebcd"
M.blueviolet = "#8a2be2"
M.burlywood = "#deb887"
M.cadetblue = "#5f9ea0"
M.chartreuse = "#7fff00"
M.chocolate = "#d2691e"
M.coral = "#ff7f50"
M.cornflowerblue = "#6495ed"
M.cornsilk = "#fff8dc"
M.crimson = "#dc143c"
M.darkblue = "#00008b"
M.darkcyan = "#008b8b"
M.darkgoldenrod = "#b8860b"
M.darkgray = "#a9a9a9"
M.darkgreen = "#006400"
M.darkkhaki = "#bdb76b"
M.darkmagenta = "#8b008b"
M.darkolivegreen = "#556b2f"
M.darkorange = "#ff8c00"
M.darkorchid = "#9932cc"
M.darkred = "#8b0000"
M.darksalmon = "#e9967a"
M.darkseagreen = "#8fbc8f"
M.darkslateblue = "#483d8b"
M.darkslategray = "#2f4f4f"
M.darkturquoise = "#00ced1"
M.darkviolet = "#9400d3"
M.deeppink = "#ff1493"
M.deepskyblue = "#00bfff"
M.dimgray = "#696969"
M.dodgerblue = "#1e90ff"
M.firebrick = "#b22222"
M.floralwhite = "#fffaf0"
M.forestgreen = "#228b22"
M.fuchsia = "#ff00ff"
M.gainsboro = "#dcdcdc"
M.ghostwhite = "#f8f8ff"
M.gold = "#ffd700"
M.goldenrod = "#daa520"
M.greenyellow = "#adff2f"
M.honeydew = "#f0fff0"
M.hotpink = "#ff69b4"
M.indianred = "#cd5c5c"
M.indigo = "#4b0082"
M.ivory = "#fffff0"
M.khaki = "#f0e68c"
M.lavender = "#e6e6fa"
M.lavenderblush = "#fff0f5"
M.lawngreen = "#7cfc00"
M.lemonchiffon = "#fffacd"
M.lightblue = "#add8e6"
M.lightcoral = "#f08080"
M.lightcyan = "#e0ffff"
M.lightgoldenrodyellow = "#fafad2"
M.lightgray = "#d3d3d3"
M.lightgreen = "#90ee90"
M.lightpink = "#ffb6c1"
M.lightsalmon = "#ffa07a"
M.lightseagreen = "#20b2aa"
M.lightskyblue = "#87cefa"
M.lightslategray = "#778899"
M.lightsteelblue = "#b0c4de"
M.lightyellow = "#ffffe0"
M.lime = "#00ff00"
M.limegreen = "#32cd32"
M.linen = "#faf0e6"
M.maroon = "#800000"
M.mediumaquamarine = "#66cdaa"
M.mediumblue = "#0000cd"
M.mediumorchid = "#ba55d3"
M.mediumpurple = "#9370db"
M.mediumseagreen = "#3cb371"
M.mediumslateblue = "#7b68ee"
M.mediumspringgreen = "#00fa9a"
M.mediumturquoise = "#48d1cc"
M.mediumvioletred = "#c71585"
M.midnightblue = "#191970"
M.mintcream = "#f5fffa"
M.mistyrose = "#ffe4e1"
M.moccasin = "#ffe4b5"
M.navajowhite = "#ffdead"
M.navy = "#000080"
M.oldlace = "#fdf5e6"
M.olive = "#808000"
M.olivedrab = "#6b8e23"
M.orangered = "#ff4500"
M.orchid = "#da70d6"
M.palegoldenrod = "#eee8aa"
M.palegreen = "#98fb98"
M.paleturquoise = "#afeeee"
M.palevioletred = "#db7093"
M.papayawhip = "#ffefd5"
M.peachpuff = "#ffdab9"
M.peru = "#cd853f"
M.plum = "#dda0dd"
M.powderblue = "#b0e0e6"
M.rebeccapurple = "#663399"
M.rosybrown = "#bc8f8f"
M.royalblue = "#4169e1"
M.saddlebrown = "#8b4513"
M.salmon = "#fa8072"
M.sandybrown = "#f4a460"
M.seagreen = "#2e8b57"
M.seashell = "#fff5ee"
M.sienna = "#a0522d"
M.silver = "#c0c0c0"
M.skyblue = "#87ceeb"
M.slateblue = "#6a5acd"
M.slategray = "#708090"
M.snow = "#fffafa"
M.springgreen = "#00ff7f"
M.steelblue = "#4682b4"
M.tan = "#d2b48c"
M.teal = "#008080"
M.thistle = "#d8bfd8"
M.tomato = "#ff6347"
M.turquoise = "#40e0d0"
M.violet = "#ee82ee"
M.wheat = "#f5deb3"
M.whitesmoke = "#f5f5f5"
M.yellowgreen = "#9acd32"

-- POV-Ray colors
M.pov_aquamarine = "#70db93"
M.pov_brown = "#a52a2a"
M.pov_coral = "#ff7f50"
M.pov_forestgreen = "#238e23"
M.pov_gold = "#cd7f32"
M.pov_maroon = "#b03060"
M.pov_orange = "#ffa500"
M.pov_pink = "#ffc0cb"
M.pov_salmon = "#fa8072"
M.pov_sienna = "#a0522d"
M.pov_tan = "#d2b48c"
M.pov_violet = "#ee82ee"
M.pov_gray05 = "#0d0d0d"
M.pov_gray10 = "#1a1a1a"
M.pov_gray25 = "#404040"
M.pov_gray50 = "#808080"
M.pov_gray75 = "#bfcfcf"
M.pov_dimgray = "#545454"

-- POV-Ray colors.inc
for k, v in pairs({
    Red = "#FF0000",
    Green = "#00FF00",
    Blue = "#0000FF",
    Yellow = "#FFFF00",
    Cyan = "#00FFFF",
    Magenta = "#FF00FF",
    Clear = "#00000000",
    White = "#FFFFFF",
    Black = "#000000",
    Gray05 = "#0D0D0D",
    Gray10 = "#1A1A1A",
    Gray15 = "#262626",
    Gray20 = "#333333",
    Gray25 = "#404040",
    Gray30 = "#4D4D4D",
    Gray35 = "#595959",
    Gray40 = "#666666",
    Gray45 = "#737373",
    Gray50 = "#808080",
    Gray55 = "#8C8C8C",
    Gray60 = "#999999",
    Gray65 = "#A6A6A6",
    Gray70 = "#B3B3B3",
    Gray75 = "#BFBFBF",
    Gray80 = "#CCCCCC",
    Gray85 = "#D9D9D9",
    Gray90 = "#E6E6E6",
    Gray95 = "#F2F2F2",
    DimGray = "#545454",
    DimGrey = "#545454",
    Gray = "#C0C0C0",
    Grey = "#C0C0C0",
    LightGray = "#A8A8A8",
    LightGrey = "#A8A8A8",
    VLightGray = "#CCCCCC",
    VLightGrey = "#CCCCCC",
    Aquamarine = "#70DB93",
    BlueViolet = "#9F5F9F",
    Brown = "#A52A2A",
    CadetBlue = "#5F9F9F",
    Coral = "#FF7F00",
    CornflowerBlue = "#42426F",
    DarkGreen = "#2F4F2F",
    DarkOliveGreen = "#4F4F2F",
    DarkOrchid = "#9932CC",
    DarkSlateBlue = "#1F238E",
    DarkSlateGray = "#2F4F4F",
    DarkSlateGrey = "#2F4F4F",
    DarkTurquoise = "#7093DB",
    Firebrick = "#8E2323",
    ForestGreen = "#238E23",
    Gold = "#CC7F32",
    Goldenrod = "#DBDB70",
    GreenYellow = "#93DB70",
    IndianRed = "#4F2F2F",
    Khaki = "#9F9F5F",
    LightBlue = "#BFD8D8",
    LightSteelBlue = "#8F8FBC",
    LimeGreen = "#32CC32",
    Maroon = "#8E236B",
    MediumAquamarine = "#32CC99",
    MediumBlue = "#3232CC",
    MediumForestGreen = "#6B8E23",
    MediumGoldenrod = "#EAEABD",
    MediumOrchid = "#9370DB",
    MediumSeaGreen = "#426F42",
    MediumSlateBlue = "#7F00FF",
    MediumSpringGreen = "#7FFF00",
    MediumTurquoise = "#70DBDB",
    MediumVioletRed = "#DB7093",
    MidnightBlue = "#2F2F4F",
    Navy = "#23238E",
    NavyBlue = "#23238E",
    Orange = "#FF8000",
    OrangeRed = "#FF4000",
    Orchid = "#DB70DB",
    PaleGreen = "#8FBC8F",
    Pink = "#BC8F8F",
    Plum = "#EAEABD",
    Salmon = "#6F4242",
    SeaGreen = "#238E6B",
    Sienna = "#8E6B23",
    SkyBlue = "#3299CC",
    SlateBlue = "#007FFF",
    SpringGreen = "#00FF7F",
    SteelBlue = "#236B8E",
    Tan = "#DB9370",
    Thistle = "#D8BFD8",
    Turquoise = "#ADEABD",
    Violet = "#4F2F4F",
    VioletRed = "#CC3299",
    Wheat = "#D8D8BF",
    YellowGreen = "#99CC32",
    SummerSky = "#38B0DE",
    RichBlue = "#5959AB",
    Brass = "#B5A642",
    Copper = "#B87333",
    Bronze = "#8C7824",
    Bronze2 = "#A67D3D",
    Silver = "#E5E8FA",
    BrightGold = "#D9D91A",
    OldGold = "#CFA33B",
    Feldspar = "#D19175",
    Quartz = "#D9D9F2",
    Mica = "#000000",
    NeonPink = "#FF6EC7",
    DarkPurple = "#871F77",
    NeonBlue = "#4D4DFF",
    CoolCopper = "#D9871A",
    MandarinOrange = "#E37833",
    LightWood = "#E8C2A6",
    MediumWood = "#A68063",
    DarkWood = "#855E42",
    SpicyPink = "#FF1C80",
    SemiSweetChoc = "#6B4226",
    BakersChoc = "#5C3317",
    Flesh = "#F5CCB0",
    NewTan = "#EBC79E",
    NewMidnightBlue = "#00009C",
    VeryDarkBrown = "#592924",
    DarkBrown = "#5C4033",
    DarkTan = "#96694F",
    GreenCopper = "#527D75",
    DkGreenCopper = "#4A756E",
    DustyRose = "#856363",
    HuntersGreen = "#215E4F",
    Scarlet = "#8C1717",
    Med_Purple = "#BA29F5",
    Light_Purple = "#DE94FA",
    Very_Light_Purple = "#EFFFCF",
}) do M[k] = v end

function M.num2hex(num)
    local hexstr = '0123456789abcdef'
    local s = ''
    while num > 0 do
        local mod = math.fmod(num, 16)
        s = string.sub(hexstr, mod+1, mod+1) .. s
        num = math.floor(num / 16)
    end
    if s == '' then s = '00' end
    return s
end

function M.rgb2col(r,g,b)
  return string.format("#%s%s%s", M.num2hex(r), M.num2hex(g), M.num2hex(b))
end

function M.rgbt2col(r,g,b,t)
  return string.format("#%s%s%s%s", M.num2hex(r), M.num2hex(g), M.num2hex(b), M.num2hex(t))
end

function M.col2rgb(col)
  local c = QColor(col)
  return c.r, c.g, c.b
end

-- from http://stackoverflow.com/questions/43044
M.random_pastel = function()
  return M.rgb2col(math.random(127)+127,math.random(127)+127,math.random(127)+127)
end

-- print(rgb2col(255,255,255))
-- print(rgbt2col(255,255,255,0))

function M.random_bpp()
   local scheme = {
      "#3a8581",
      "#71b04b",
      "#00a651",
      "#29a969",
      "#6e2c90",
      "#4f6ab1",
      "#86733d",
      "#ef4927",
      "#895781",
      "#ec2290",
      "#d76048",
      "#72c59a",
      "#4ec2c6",
      "#8ea0c8",
      "#eec343",
      "#d0b05f",
      "#f9b598",
      "#aa7694",
      "#abdddf",
      "#80ffff",
      "#b3b08f",
   }
   
   return scheme[ math.random( #scheme ) ]
end

function M.random_chrome()
   local scheme = {
      "#698153",
      "#177646",
      "#13864c",
      "#159053",
      "#169f5c",
      "#1da362",
      "#39ac74",
      "#b44438",
      "#d54b3e",
      "#dc5044",
      "#c77067",
      "#e27066",
      "#4b8bf4",
      "#e5a738",
      "#fbc03d",
      "#fecd42",
      "#c8b763",
      "#fbd366",
      "#79a7f3",
      "#7ebd9d",
      "#aecae8",
      "#dec1aa",
      "#d9dfe4",
   }

   return scheme[ math.random( #scheme ) ]
end

function M.random_google()
   local scheme = {
      "#4285F4",
      "#DB4437",
      "#F4B400",
      "#0F9D58",
   }

   return scheme[ math.random( #scheme ) ]
end

return M
