Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Move colorscheme selector to different file

  • Loading branch information...
commit 13d349089d3687330a78b047253559bdae0ad71f 1 parent 7cd64a7
Björn-Egil Dahlberg authored

Showing 3 changed files with 98 additions and 74 deletions. Show diff stats Hide diff stats

  1. +1 0  Makefile
  2. +3 74 src/egd_chart.erl
  3. +94 0 src/egd_colorscheme.erl
1  Makefile
@@ -7,6 +7,7 @@ LINK = ln -s -f
7 7 EFLAGS = +debug_info
8 8
9 9 MODULES = \
  10 + egd_colorscheme \
10 11 egd_chart \
11 12 eplot_main \
12 13 eview
77 src/egd_chart.erl
@@ -7,11 +7,6 @@
7 7 bar2d/2
8 8 ]).
9 9
10   --export([
11   - hsl2rgb/1,
12   - rgb2hsl/1
13   - ]).
14   -
15 10 -export([smart_ticksize/3]).
16 11
17 12 -record(chart, {
@@ -47,68 +42,6 @@
47 42 -define(float_error, 0.0000000000000001).
48 43
49 44
50   -%% color conversions
51   -%% H, hue has the range of [0, 360]
52   -%% S, saturation has the range of [0,1]
53   -%% L, lightness has the range of [0,1]
54   -
55   -hsl2rgb({H,S,L}) -> hsl2rgb({H,S,L,255});
56   -hsl2rgb({H,S,L,A}) ->
57   - Q = if
58   - L < 0.5 -> L * (1 + S);
59   - true -> L + S - (L * S)
60   - end,
61   - P = 2 * L - Q,
62   - Hk = H/360,
63   - Rt = Hk + 1/3,
64   - Gt = Hk,
65   - Bt = Hk - 1/3,
66   -
67   - Cts = lists:map(fun
68   - (Tc) when Tc < 0.0 -> Tc + 1.0;
69   - (Tc) when Tc > 1.0 -> Tc - 1.0;
70   - (Tc) -> Tc
71   - end, [Rt, Gt, Bt]),
72   - [R,G,B] = lists:map(fun
73   - (Tc) when Tc < 1/6 -> P + ((Q - P) * 6 * Tc);
74   - (Tc) when Tc < 1/2, Tc >= 1/6 -> Q;
75   - (Tc) when Tc < 2/3, Tc >= 1/2 -> P + ((Q - P) * 6 * (2/3 - Tc));
76   - (_ ) -> P
77   - end, Cts),
78   - {trunc(R*255),trunc(G*255),trunc(B*255),A}.
79   -
80   -rgb2hsl({R,G,B}) -> rgb2hsl({R,G,B,255});
81   -rgb2hsl({R,G,B,A}) ->
82   - Rf = R/255,
83   - Gf = G/255,
84   - Bf = B/255,
85   - Max = lists:max([Rf,Gf,Bf]),
86   - Min = lists:min([Rf,Gf,Bf]),
87   - H = if
88   - abs(Max - Min) < ?float_error ->
89   - 0;
90   - abs(Max - Rf) < ?float_error ->
91   - D = 60 * (Gf - Bf)/(Max - Min),
92   - Dt = trunc(D),
93   - Dt rem 360;
94   - abs(Max - Gf) < ?float_error ->
95   - 60 * (Bf - Rf)/(Max - Min) + 120;
96   - abs(Max - Bf) < ?float_error ->
97   - 60 * (Rf - Gf)/(Max - Min) + 240;
98   - true ->
99   - 0
100   - end,
101   - L = (Max + Min)/2,
102   - S = if
103   - abs(Max - Min) < ?float_error ->
104   - 0;
105   - L > 0.5 ->
106   - (Max - Min)/(2 - (Max + Min));
107   - true ->
108   - (Max - Min)/(Max + Min)
109   - end,
110   - {H, S, L, A}.
111   -
112 45 %% graph/1 and graph/2
113 46 %% In:
114 47 %% Data :: [{Graphname :: atom() | string(), [{X,Y}]}]
@@ -232,15 +165,11 @@ draw_xlabel(Chart, Im, Font) ->
232 165 Pt = {Xc - trunc(Width/2), Y},
233 166 egd:text(Im, Pt, Font, Label, egd:color({0,0,0})).
234 167
235   -
236   -color_scheme(I) ->
237   - egd:color(hsl2rgb({I*55 rem 360, 0.8, 0.3, 120})).
238   -
239 168 draw_graphs(Datas, Chart, Im) ->
240 169 draw_graphs(Datas, 0, Chart, Im).
241 170 draw_graphs([],_,_,_) -> ok;
242 171 draw_graphs([{_, Data}|Datas], ColorIndex, Chart, Im) ->
243   - Color = color_scheme(ColorIndex),
  172 + Color = egd_colorscheme:select(default, ColorIndex),
244 173 % convert data to graph data
245 174 % fewer pass of xy2chart
246 175 GraphData = [xy2chart(Pt, Chart) || Pt <- Data],
@@ -277,7 +206,7 @@ draw_graph_names(Datas, Chart, Font, Im) ->
277 206 draw_graph_names(Datas, 0, Chart, Font, Im, 0, Chart#chart.graph_name_yh).
278 207 draw_graph_names([],_,_,_,_,_,_) -> ok;
279 208 draw_graph_names([{Name, _}|Datas], ColorIndex, Chart, Font, Im, Yo, Yh) ->
280   - Color = color_scheme(ColorIndex),
  209 + Color = egd_colorscheme:select(default, ColorIndex),
281 210 draw_graph_name_color(Chart, Im, Font, Name, Color, Yo),
282 211 draw_graph_names(Datas, ColorIndex + 1, Chart, Font, Im, Yo + Yh, Yh).
283 212
@@ -440,7 +369,7 @@ bar2d(Data0, Options) ->
440 369 bar2d_convert_data(Data) -> bar2d_convert_data(Data, 0,{[], []}).
441 370 bar2d_convert_data([], _, {ColorMap, Out}) -> {lists:reverse(ColorMap), lists:sort(Out)};
442 371 bar2d_convert_data([{Set, KVs}|Data], ColorIndex, {ColorMap, Out}) ->
443   - Color = color_scheme(ColorIndex),
  372 + Color = egd_colorscheme:select(default, ColorIndex),
444 373 bar2d_convert_data(Data, ColorIndex + 1, {[{Set,Color}|ColorMap], bar2d_convert_data_kvs(KVs, Set, Color, Out)}).
445 374
446 375 bar2d_convert_data_kvs([], _,_, Out) -> Out;
94 src/egd_colorscheme.erl
... ... @@ -0,0 +1,94 @@
  1 +%% Copyright (C) 2011 Björn-Egil Dahlberg
  2 +%%
  3 +%% File: egd_colorscheme.erl
  4 +%% Author: Björn-Egil Dahlberg
  5 +%% Created: 2011-09-02
  6 +
  7 +-module(egd_colorscheme).
  8 +
  9 +-export([
  10 + test/1,
  11 + hsl2rgb/1,
  12 + rgb2hsl/1,
  13 + select/2
  14 + ]).
  15 +
  16 +test(File) ->
  17 + Im = egd:create(800,600),
  18 + lists:foreach(fun
  19 + (I) ->
  20 + egd:filledRectangle(Im, {(I - 1)*20, 0}, {(I - 1)*20 + 19, 20}, egd_colorscheme:select(default, I))
  21 + end, lists:seq(1,40)),
  22 + B = egd:render(Im),
  23 + egd:save(B, "test.png").
  24 +
  25 +
  26 +select(_Default, I) -> select(I, 31, 0.8, 0.5, 210).
  27 +select(I, Hm, S, L, A) -> egd:color(hsl2rgb({I*Hm rem 360, S, L, A})).
  28 +
  29 +%% color conversions
  30 +%% H, hue has the range of [0, 360]
  31 +%% S, saturation has the range of [0,1]
  32 +%% L, lightness has the range of [0,1]
  33 +
  34 +-define(float_error, 0.0000000000000001).
  35 +
  36 +hsl2rgb({H,S,L}) -> hsl2rgb({H,S,L,255});
  37 +hsl2rgb({H,S,L,A}) ->
  38 + Q = if
  39 + L < 0.5 -> L * (1 + S);
  40 + true -> L + S - (L * S)
  41 + end,
  42 + P = 2 * L - Q,
  43 + Hk = H/360,
  44 + Rt = Hk + 1/3,
  45 + Gt = Hk,
  46 + Bt = Hk - 1/3,
  47 +
  48 + Cts = lists:map(fun
  49 + (Tc) when Tc < 0.0 -> Tc + 1.0;
  50 + (Tc) when Tc > 1.0 -> Tc - 1.0;
  51 + (Tc) -> Tc
  52 + end, [Rt, Gt, Bt]),
  53 + [R,G,B] = lists:map(fun
  54 + (Tc) when Tc < 1/6 -> P + ((Q - P) * 6 * Tc);
  55 + (Tc) when Tc < 1/2, Tc >= 1/6 -> Q;
  56 + (Tc) when Tc < 2/3, Tc >= 1/2 -> P + ((Q - P) * 6 * (2/3 - Tc));
  57 + (_ ) -> P
  58 + end, Cts),
  59 + {trunc(R*255),trunc(G*255),trunc(B*255),A}.
  60 +
  61 +rgb2hsl({R,G,B}) -> rgb2hsl({R,G,B,255});
  62 +rgb2hsl({R,G,B,A}) ->
  63 + Rf = R/255,
  64 + Gf = G/255,
  65 + Bf = B/255,
  66 + Max = lists:max([Rf,Gf,Bf]),
  67 + Min = lists:min([Rf,Gf,Bf]),
  68 + H = if
  69 + abs(Max - Min) < ?float_error ->
  70 + 0;
  71 + abs(Max - Rf) < ?float_error ->
  72 + D = 60 * (Gf - Bf)/(Max - Min),
  73 + Dt = trunc(D),
  74 + Dt rem 360;
  75 + abs(Max - Gf) < ?float_error ->
  76 + 60 * (Bf - Rf)/(Max - Min) + 120;
  77 + abs(Max - Bf) < ?float_error ->
  78 + 60 * (Rf - Gf)/(Max - Min) + 240;
  79 + true ->
  80 + 0
  81 + end,
  82 + L = (Max + Min)/2,
  83 + S = if
  84 + abs(Max - Min) < ?float_error ->
  85 + 0;
  86 + L > 0.5 ->
  87 + (Max - Min)/(2 - (Max + Min));
  88 + true ->
  89 + (Max - Min)/(Max + Min)
  90 + end,
  91 + {H, S, L, A}.
  92 +
  93 +
  94 +

0 comments on commit 13d3490

Please sign in to comment.
Something went wrong with that request. Please try again.