From 90a7ce912040039287ec25e10c8cd3c21eb034d1 Mon Sep 17 00:00:00 2001 From: Alexander Iljin Date: Sat, 4 Feb 2017 04:25:29 +0300 Subject: [PATCH] ui.gadgets.charts.utils: move some common code to the new vocab --- extra/ui/gadgets/charts/axes/axes.factor | 6 +----- extra/ui/gadgets/charts/lines/lines-tests.factor | 4 ++-- extra/ui/gadgets/charts/lines/lines.factor | 6 ++---- extra/ui/gadgets/charts/utils/authors.txt | 1 + extra/ui/gadgets/charts/utils/utils.factor | 10 ++++++++++ 5 files changed, 16 insertions(+), 11 deletions(-) create mode 100644 extra/ui/gadgets/charts/utils/authors.txt create mode 100644 extra/ui/gadgets/charts/utils/utils.factor diff --git a/extra/ui/gadgets/charts/axes/axes.factor b/extra/ui/gadgets/charts/axes/axes.factor index 24c9d5f24d..b88333f02a 100644 --- a/extra/ui/gadgets/charts/axes/axes.factor +++ b/extra/ui/gadgets/charts/axes/axes.factor @@ -2,8 +2,7 @@ ! See http://factorcode.org/license.txt for BSD license. USING: accessors arrays colors.constants kernel locals math math.order opengl sequences ui.gadgets ui.gadgets.charts -ui.gadgets.charts.lines ui.gadgets.charts.lines.private -ui.render ; +ui.gadgets.charts.lines ui.gadgets.charts.utils ui.render ; IN: ui.gadgets.charts.axes TUPLE: axis < gadget vertical? color ; @@ -15,9 +14,6 @@ ALIAS: y second : axis-pos ( min,max -- value ) 0 swap first2 clamp ; -: default-color ( default obj -- ) - color>> dup [ swap ] unless gl-color drop ; - :: x-0y-chunk ( x y -- chunk ) x 0 2array x y 2array 2array ; :: 0x-y-chunk ( x y -- chunk ) 0 y 2array x y 2array 2array ; : flip-y ( axis-y xmax ymax -- xmax axis-y' ) rot - ; diff --git a/extra/ui/gadgets/charts/lines/lines-tests.factor b/extra/ui/gadgets/charts/lines/lines-tests.factor index 95c89abb83..9b50cb7952 100644 --- a/extra/ui/gadgets/charts/lines/lines-tests.factor +++ b/extra/ui/gadgets/charts/lines/lines-tests.factor @@ -1,7 +1,7 @@ ! Copyright (C) 2017 Alexander Ilin. ! See http://factorcode.org/license.txt for BSD license. -USING: kernel math sequences tools.test -ui.gadgets.charts.lines ui.gadgets.charts.lines.private ; +USING: kernel math sequences tools.test ui.gadgets.charts.lines +ui.gadgets.charts.lines.private ui.gadgets.charts.utils ; IN: ui.gadgets.charts.lines.tests { -2/3 } [ { 1 3 } { -2 5 } calc-line-slope ] unit-test diff --git a/extra/ui/gadgets/charts/lines/lines.factor b/extra/ui/gadgets/charts/lines/lines.factor index 756ba8f438..21420fda76 100644 --- a/extra/ui/gadgets/charts/lines/lines.factor +++ b/extra/ui/gadgets/charts/lines/lines.factor @@ -4,7 +4,8 @@ USING: accessors arrays assocs binary-search combinators combinators.short-circuit fry kernel locals make math math.order math.statistics math.vectors namespaces opengl opengl.gl sequences specialized-arrays.instances.alien.c-types.float -splitting.monotonic ui.gadgets ui.gadgets.charts ui.render ; +splitting.monotonic ui.gadgets ui.gadgets.charts +ui.gadgets.charts.utils ui.render ; IN: ui.gadgets.charts.lines ! Data must be a sequence of { x y } coordinates sorted by @@ -208,9 +209,6 @@ ALIAS: y second : flip-y-axis ( chunks ymin,ymax -- chunks ) first2 + '[ [ _ swap - ] assoc-map ] map ; -! value' = (value - min) / (max - min) * width -: scale ( width value max min -- value' ) neg [ + ] curry bi@ / * ; - ! Return quotation that can be used in map operation. : scale-mapper ( width min,max -- quot: ( value -- value' ) ) first2 swap '[ _ swap _ _ scale ] ; inline diff --git a/extra/ui/gadgets/charts/utils/authors.txt b/extra/ui/gadgets/charts/utils/authors.txt new file mode 100644 index 0000000000..8e1955f8e1 --- /dev/null +++ b/extra/ui/gadgets/charts/utils/authors.txt @@ -0,0 +1 @@ +Alexander Ilin diff --git a/extra/ui/gadgets/charts/utils/utils.factor b/extra/ui/gadgets/charts/utils/utils.factor new file mode 100644 index 0000000000..9971a82c14 --- /dev/null +++ b/extra/ui/gadgets/charts/utils/utils.factor @@ -0,0 +1,10 @@ +! Copyright (C) 2017 Alexander Ilin. +! See http://factorcode.org/license.txt for BSD license. +USING: accessors kernel math opengl ; +IN: ui.gadgets.charts.utils + +: default-color ( default obj -- ) + color>> dup [ swap ] unless gl-color drop ; + +! value' = (value - min) / (max - min) * width +: scale ( width value max min -- value' ) neg [ + ] curry bi@ / * ;