From 7f8f4b8b2514057a2975fd6c5623f6e43a835ef6 Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Tue, 21 Feb 2023 08:38:44 +0530 Subject: [PATCH 1/3] Add functions which make saving cgrad as CSV a little easier Had some use for these when making colormaps out of `PerceptualColourMaps.jl` which has quite a lot of dependencies, so I needed to serialize them somehow. --- src/color_utils.jl | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/color_utils.jl b/src/color_utils.jl index aca4640..e63ffb4 100644 --- a/src/color_utils.jl +++ b/src/color_utils.jl @@ -35,6 +35,28 @@ function generate_colorscheme( ColorScheme(colors) end +# -------------------------------------------------------------- +# Methods to save colorschemes to CSV +function cgrad_to_csv(file::AbstractString, cgrad::ColorGradient) + values = cgrad.values + colors = cgrad.colors.colors + + reds = Colors.red.(colors) + greens = Colors.green.(colors) + blues = Colors.blue.(colors) + alphas = Colors.alpha.(colors) + + csv_matrix = hcat(values, reds, greens, blues, alphas) + csv_matrix_with_headers = vcat(["Value" "Red" "Green" "Blue" "Alpha"], csv_matrix) + + DelimitedFiles.writedlm(file, csv_matrix_with_headers, ',') +end + +function csv_to_cgrad(file::AbstractString) + data_cells, header_cells = DelimitedFiles.readdlm(file, ',', Float64, '\n'; header = true) + return cgrad(RGBA{Float64}.(data_cells[:, 2], data_cells[:, 3], data_cells[:, 4], data_cells[:, 5]), data_cells[:, 1]) +end + # ---------------------------------------------------------------------------------- function getpctrange(n::Integer) From 3e1fd3167bf368c7a6a238bc7fe38c51f42dda96 Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Tue, 21 Feb 2023 08:46:57 +0530 Subject: [PATCH 2/3] use DelimitedFiles in PlotUtils.jl --- src/PlotUtils.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/PlotUtils.jl b/src/PlotUtils.jl index 1ab7a44..2ed4d07 100644 --- a/src/PlotUtils.jl +++ b/src/PlotUtils.jl @@ -3,6 +3,7 @@ module PlotUtils using SnoopPrecompile using ColorSchemes using Reexport +using DelimitedFiles using Printf using Dates From c1c97cc59c45f4a81606d2f3f46c73fbe33212bf Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Tue, 21 Feb 2023 08:47:42 +0530 Subject: [PATCH 3/3] Add DelimitedFiles to Project.toml --- Project.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Project.toml b/Project.toml index 0698ea8..e12703b 100644 --- a/Project.toml +++ b/Project.toml @@ -6,6 +6,7 @@ version = "1.3.4" ColorSchemes = "35d6a980-a343-548e-a6ea-1d62b119f2f4" Colors = "5ae59095-9a9b-59fe-a467-6f913c188581" Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" +DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab" Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" Reexport = "189a3867-3050-52da-a836-e630ba90ab69"