~ubuntu-branches/ubuntu/oneiric/ctioga2/oneiric

« back to all changes in this revision

Viewing changes to lib/ctioga2/graphics/elements/gradient-region.rb

  • Committer: Bazaar Package Importer
  • Author(s): Vincent Fourmond
  • Date: 2011-01-24 21:36:06 UTC
  • Revision ID: james.westby@ubuntu.com-20110124213606-9ettx0ugl83z0bzp
Tags: upstream-0.1
ImportĀ upstreamĀ versionĀ 0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# region.rb: draw curves-delimited fills
 
2
# copyright (c) 2010 by Vincent Fourmond
 
3
  
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
  
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details (in the COPYING file).
 
13
 
 
14
 
 
15
require 'ctioga2/utils'
 
16
require 'ctioga2/log'
 
17
 
 
18
module CTioga2
 
19
 
 
20
  Version::register_svn_info('$Revision: 159 $', '$Date: 2010-07-26 17:29:47 +0200 (Mon, 26 Jul 2010) $')
 
21
 
 
22
  module Graphics
 
23
 
 
24
    module Elements
 
25
 
 
26
      # A GradientRegion is an object that makes color gradient for
 
27
      # the curves. Especially useful for a great number of curves,
 
28
      # and when one doesn't want to compute...
 
29
      #
 
30
      # 
 
31
      #
 
32
      # Like Region It is a fake container in the sense that all the
 
33
      # elements are actually forwarded to the parent.
 
34
      class GradientRegion < Container
 
35
 
 
36
        undef :elements
 
37
        undef :subframe
 
38
 
 
39
        # The curves which delimit the region
 
40
        attr_accessor :curves
 
41
 
 
42
        # The start and end colors
 
43
        attr_accessor :start_color, :end_color
 
44
 
 
45
        # Creates a new empty region
 
46
        def initialize(parent = nil, root = nil)
 
47
          @parent = parent
 
48
          
 
49
          # The curves whose color we should change
 
50
          @curves = []
 
51
 
 
52
          @root_object = root
 
53
 
 
54
          @legend_area = nil
 
55
 
 
56
          @start_color = Tioga::ColorConstants::Red
 
57
          @end_color = Tioga::ColorConstants::Green
 
58
 
 
59
        end
 
60
 
 
61
        # Adds an element. Actually forwards it to the parent.
 
62
        def add_element(element)
 
63
          parent.add_element(element)
 
64
          if element.respond_to?(:curve_style)
 
65
            @curves << element
 
66
          end
 
67
        end
 
68
 
 
69
        # Sets the various things from hash.
 
70
        def set_from_hash(hash)
 
71
        end
 
72
 
 
73
        # Redirects to the parent's style
 
74
        def style(*a)
 
75
          return parent.style(*a)
 
76
        end
 
77
 
 
78
        protected 
 
79
 
 
80
        # Simply sets the color of the curves.
 
81
        def real_do(t)
 
82
          nb = @curves.size
 
83
          i = 0
 
84
          for c in @curves
 
85
            c.curve_style.line.color = 
 
86
              Utils::mix_objects(@end_color,@start_color, i/(nb - 1.0))
 
87
            i += 1
 
88
          end
 
89
        end
 
90
 
 
91
      end
 
92
    end
 
93
  end
 
94
end