aboutsummaryrefslogtreecommitdiffstats
path: root/lib/gs/examples/color_demo2.erl
diff options
context:
space:
mode:
authorErlang/OTP <[email protected]>2009-11-20 14:54:40 +0000
committerErlang/OTP <[email protected]>2009-11-20 14:54:40 +0000
commit84adefa331c4159d432d22840663c38f155cd4c1 (patch)
treebff9a9c66adda4df2106dfd0e5c053ab182a12bd /lib/gs/examples/color_demo2.erl
downloadotp-84adefa331c4159d432d22840663c38f155cd4c1.tar.gz
otp-84adefa331c4159d432d22840663c38f155cd4c1.tar.bz2
otp-84adefa331c4159d432d22840663c38f155cd4c1.zip
The R13B03 release.OTP_R13B03
Diffstat (limited to 'lib/gs/examples/color_demo2.erl')
-rw-r--r--lib/gs/examples/color_demo2.erl108
1 files changed, 108 insertions, 0 deletions
diff --git a/lib/gs/examples/color_demo2.erl b/lib/gs/examples/color_demo2.erl
new file mode 100644
index 0000000000..817cc9ed7d
--- /dev/null
+++ b/lib/gs/examples/color_demo2.erl
@@ -0,0 +1,108 @@
+%%
+%% %CopyrightBegin%
+%%
+%% Copyright Ericsson AB 1996-2009. All Rights Reserved.
+%%
+%% The contents of this file are subject to the Erlang Public License,
+%% Version 1.1, (the "License"); you may not use this file except in
+%% compliance with the License. You should have received a copy of the
+%% Erlang Public License along with this software. If not, it can be
+%% retrieved online at http://www.erlang.org/.
+%%
+%% Software distributed under the License is distributed on an "AS IS"
+%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
+%% the License for the specific language governing rights and limitations
+%% under the License.
+%%
+%% %CopyrightEnd%
+%%
+
+%%
+%% ------------------------------------------------------------
+%% Another simple demo for choosing
+%% colors in a window.
+%% ------------------------------------------------------------
+
+-module(color_demo2).
+
+-export([start/0,init/0]).
+
+start() ->
+ spawn(color_demo2,init,[]).
+
+
+init() ->
+ S=gs:start(),
+ Win = gs:create(window,S,[{width,380},{height,430},{motion,true},
+ {buttonpress,true}]),
+ gs:create(button,b1,Win,[{x,200},{label,{text,""}}]),
+ gs:create(button,Win,[{data,quit},{label,{text,"Quit"}},{bg,yellow},
+ {width,40}]),
+ Side = 200,
+ Tri = equi_tri(100, 400, Side),
+ %% draw_lines(Win, Tri),
+ gs:config(Win,[{title,"Color Demo 2"},{map,true}]),
+ server(Win, Side, Tri, {0,0,0}).
+
+server(Win, Side, [Point1, Point2, Point3], OldCol) ->
+ receive
+ {gs,Win,motion,_,[X,Y|_]} ->
+ R = col({X,Y}, Point1, Side),
+ G = col({X,Y}, Point2, Side),
+ B = col({X,Y}, Point3, Side),
+ Txt = lists:flatten(io_lib:format("~w ~w ~w",[R,G,B])),
+ gs:config(b1,[{label,{text,Txt}}]),
+ Col = {R, G, B},
+ gs:config(Win, [{bg,Col}]),
+ server(Win, Side, [Point1, Point2, Point3], Col);
+ {gs,Win,buttonpress,_,[_X,_Y|_]} ->
+ io:format("{color, ~w}\n", [OldCol]),
+ server(Win, Side, [Point1, Point2, Point3], OldCol);
+ {gs,_,click,quit,_} ->
+ exit(die);
+ {gs,Win,destroy,_,_} ->
+ exit(die);
+ _Any ->
+ server(Win, Side, [Point1, Point2, Point3], OldCol)
+ end.
+
+
+col(Point1, Point2, Side) ->
+ D = dist(Point1, Point2),
+ %% All FFFFFF is white
+ %% when Col = 255 when D = Side
+ %% when Col = 0 when D = 0
+ %% Assume Col = A * D + B
+ B = 0,
+ A = (255 - B)/Side,
+ Col = trunc(A*D + B),
+ map(Col).
+
+map(X) ->
+ X band 255.
+
+equi_tri(X1, Y1, L) ->
+ X2 = trunc(X1 + L/2),
+ Y2 = trunc(Y1 - L * math:sqrt(3)),
+ [{X1,Y1},{X2,Y2},{X1+L,Y1}].
+
+%draw_line(Win, {X1, Y1}, {X2, Y2}) ->
+% gs:create(line,Win,[{coords,[{X1,Y1},{X2,Y2}]},{width,2}]).
+
+%draw_lines(Win, L) ->
+% draw1(Win, L),
+% draw_line(Win, hd(L), lists:last(L)).
+
+%draw1(Win, [X,Y|T]) ->
+% draw_line(Win, X, Y),
+% draw1(Win, [Y|T]);
+%draw1(Win, _) ->
+% [].
+
+dist({X1,Y1},{X2,Y2}) ->
+ XX = X1 - X2,
+ YY = Y1 - Y2,
+ math:sqrt(XX*XX+YY*YY).
+
+%% ------------------------------------------------------------
+%% end of color_demo2.erl