using Test, Colors

@testset "Colormaps" begin
    @testset "distinguishable_colors" begin
        cols = distinguishable_colors(10)
        @test isconcretetype(eltype(cols))

        mindiff = Inf
        for i = 1:10, j = i+1:10
            mindiff = min(mindiff, colordiff(cols[i], cols[j]))
        end
        @test mindiff > 8

        seed = distinguishable_colors(1)
        @test colordiff(distinguishable_colors(1, seed)[1], seed[1]) == 0.0
        @test colordiff(distinguishable_colors(1, seed; dropseed=true)[1], seed[1]) > 50

        cols_i = distinguishable_colors(4, LCHab(60, 50, 40), transform=identity,
                                        lchoices=[40, 60], cchoices=[50], hchoices=[40, 140])
        cols_p = distinguishable_colors(4, LCHab(60, 50, 40), transform=protanopic,
                                        lchoices=[40, 60], cchoices=[50], hchoices=[40, 140])
        @test cols_i[2] ≈ LCHab(40, 50, 140) atol=1f-3 # green
        @test cols_p[2] ≈ LCHab(40, 50,  40) atol=1f-3 # red
    end

    @test length(colormap("RdBu", 100)) == 100

    @test isconcretetype(eltype(colormap("Grays")))

    @test_throws ArgumentError colormap("Grays", N=10) # optional arguments, not keyword

    # not return values to check here, just checking that keywords can be used
    # Sequential
    default_blues = colormap("Blues", 10)
    @test colormap("Blues", 10; mid=0.9) == default_blues # `mid` is for diverging colormaps only (issue #300)
    @test colormap("Blues", 10; logscale=true) != default_blues
    @test colormap("Blues", 10; h=0.5) != default_blues
    @test colormap("Blues", 10; w=0.5) != default_blues
    @test colormap("Blues", 10; d=0.5) != default_blues
    @test colormap("Blues", 10; c=0.5) != default_blues
    @test colormap("Blues", 10; s=0.5) != default_blues
    @test colormap("Blues", 10; b=0.5) != default_blues
    @test colormap("Blues", 10; wcolor=colorant"white") != default_blues
    @test colormap("Blues", 10; dcolor=colorant"black") != default_blues
    @test_throws ArgumentError colormap("Blues", 10; h1=0.5)

    # Diverging
    default_rdbu = colormap("RdBu", 10)
    @test colormap("RdBu", 10; mid=0.9) != default_rdbu
    @test colormap("RdBu", 10; logscale=true) != default_rdbu
    @test colormap("RdBu", 10; h1=0.5) != default_rdbu
    @test colormap("RdBu", 10; h2=0.5) != default_rdbu
    @test colormap("RdBu", 10; w=0.5) != default_rdbu
    @test colormap("RdBu", 10; d1=0.5) != default_rdbu
    @test colormap("RdBu", 10; d2=0.5) != default_rdbu
    @test colormap("RdBu", 10; c=0.5) != default_rdbu
    @test colormap("RdBu", 10; s=0.5) != default_rdbu
    @test colormap("RdBu", 10; b=0.5) != default_rdbu
    @test colormap("RdBu", 10; wcolor=colorant"white") != default_rdbu
    @test colormap("RdBu", 10; dcolor1=colorant"black") != default_rdbu
    # `dcolor2` is disabled by the default `d2`(=0)
    @test colormap("RdBu", 10; dcolor2=colorant"black", d2=1) != default_rdbu
    @test_throws ArgumentError colormap("RdBu", 10; h=0.5)

    # The outputs of `colormap()` were slightly affected by the bug fix of
    # `MSC(h)` (issue #349).
    # The following were generated by `colormap()` in Colors.jl v0.9.6.
    blues_old   = ["F4FDFF", "B3E3F4", "65B9E7", "2978BE", "0B2857"]
    greens_old  = ["FAFFF7", "B6EEA0", "75C769", "308B40", "00391A"]
    grays_old   = ["FFFFFF", "DCDCDC", "A9A9A9", "626262", "000000"]
    oranges_old = ["FFFBF6", "FFD6B4", "FF9D5F", "E2500D", "732108"]
    purples_old = ["FBFBFB", "DBD7F6", "AFA7E6", "7666BF", "3C0468"]
    reds_old    = ["FFF1EE", "FFC4B9", "FF8576", "E72823", "6D0B0C"]
    rdbu_old    = ["610102", "FF8D7B", "F9F8F9", "76B4E8", "092C58"]

    to_rgb(s) = parse(RGB, "#"*s)
    max_colordiff(a1, a2) = reduce(max, colordiff.(a1, a2))
    @test max_colordiff(colormap("Blues", 5), to_rgb.(blues_old)) < 1
    @test max_colordiff(colormap("Greens", 5), to_rgb.(greens_old)) < 1
    @test max_colordiff(colormap("Grays", 5), to_rgb.(grays_old)) < 1
    @test max_colordiff(colormap("Oranges", 5), to_rgb.(oranges_old)) < 1
    @test max_colordiff(colormap("Purples", 5), to_rgb.(purples_old)) < 1
    @test max_colordiff(colormap("Reds", 5), to_rgb.(reds_old)) < 1
    @test max_colordiff(colormap("RdBu", 5), to_rgb.(rdbu_old)) < 1

    # TODO: add more tests
end
