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

« back to all changes in this revision

Viewing changes to lib/ctioga2/metabuilder/types/styles.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
# styles.rb : Different Types to deal with various style arguments.
 
2
# Copyright (C) 2006, 2009 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.
 
13
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program; if not, write to the Free Software
 
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 
17
 
 
18
 
 
19
require 'ctioga2/utils'
 
20
 
 
21
module CTioga2
 
22
 
 
23
  Version::register_svn_info('$Revision: 222 $', '$Date: 2011-01-11 00:52:31 +0100 (Tue, 11 Jan 2011) $')
 
24
 
 
25
  module MetaBuilder
 
26
    module Types
 
27
 
 
28
      # A color for use with Tioga, ie a [red, green, blue] array of
 
29
      # values between 0 and 1.0. It accepts HTML-like colors, or
 
30
      # three comma-separated values between 0 and 1.
 
31
      class TiogaColorType < Type
 
32
        
 
33
        type_name :tioga_color, 'color'
 
34
 
 
35
        HLSRegexp = /(?:hls):/i
 
36
        
 
37
        def string_to_type_internal(str)
 
38
          if str =~ HLSRegexp
 
39
            hls = true
 
40
            str = str.gsub(HLSRegexp,'')
 
41
          else
 
42
            hls = false
 
43
          end
 
44
          if str =~ /^\s*#([0-9a-fA-F]{6})\s*$/
 
45
            value =  $1.scan(/../).map {
 
46
              |i| i.to_i(16)/255.0 
 
47
            }
 
48
          elsif str =~ /^\s*#([0-9a-fA-F]{3})\s*$/
 
49
            value =  $1.scan(/./).map {
 
50
              |i| i.to_i(16)/15.0 
 
51
            }
 
52
          else
 
53
            value = str.split(/\s*,\s*/).map do |s|
 
54
              s.to_f
 
55
            end
 
56
          end
 
57
          if value.size != 3
 
58
            raise IncorrectInput, "You need exactly three values to make up a color"
 
59
          end
 
60
          if hls
 
61
            # Requires Tioga r599
 
62
            value = Tioga::FigureMaker.hls_to_rgb(value)
 
63
          end
 
64
          return value
 
65
        end
 
66
      end
 
67
 
 
68
      # A line style for Tioga. It will be represented as:
 
69
      # 
 
70
      #  a,b,c,d,...:e
 
71
      #  
 
72
      # This creates a line style of:
 
73
      # 
 
74
      #  [[a,b,c,d,...],e]
 
75
      #  
 
76
      # If the :e is omitted 0 is used.
 
77
      class LineStyleType < Type
 
78
        
 
79
        type_name :tioga_line_style, 'line_style'
 
80
        
 
81
        def string_to_type_internal(str)
 
82
          specs = str.split(/\s*,\s*/)
 
83
          if specs.last =~ /:(.*)$/
 
84
            phase = $1.to_f
 
85
            specs.last.gsub!(/:.*$/,'')
 
86
          else
 
87
            phase = 0
 
88
          end
 
89
          return [ specs.map { |s| s.to_f }, phase]
 
90
        end
 
91
      end
 
92
 
 
93
      # A marker Type for Tioga. Input as
 
94
      # 
 
95
      #  a,b(,c)?
 
96
      #  
 
97
      class MarkerType < Type
 
98
        
 
99
        type_name :tioga_marker, 'marker'
 
100
        
 
101
        def string_to_type_internal(str)
 
102
          specs = str.split(/\s*,\s*/)
 
103
          if specs.size == 2
 
104
            return [specs[0].to_i, specs[1].to_i]
 
105
          elsif specs.size == 3
 
106
            return [specs[0].to_i, specs[1].to_i, specs[2].to_f]
 
107
          else
 
108
            raise IncorrectInput, "You need two or three values to make a marker"
 
109
          end
 
110
        end
 
111
      end
 
112
 
 
113
      # The type of edges/axis
 
114
      class AxisType < Type
 
115
 
 
116
        include Tioga::FigureConstants
 
117
 
 
118
        ValidTypes = {
 
119
          /hidden|off/i => AXIS_HIDDEN,
 
120
          /line/i => AXIS_LINE_ONLY, 
 
121
          /ticks/i => AXIS_WITH_TICKS_ONLY,
 
122
          /major/i => AXIS_WITH_MAJOR_TICKS_ONLY, 
 
123
          /major-num/i => AXIS_WITH_MAJOR_TICKS_AND_NUMERIC_LABELS,
 
124
          /full/i => AXIS_WITH_TICKS_AND_NUMERIC_LABELS
 
125
        }
 
126
        
 
127
        type_name :tioga_axis_type, 'axis_type'
 
128
        
 
129
        def string_to_type_internal(str)
 
130
          for k,v in ValidTypes
 
131
            if str =~ /^\s*#{k}\s*/
 
132
                return v
 
133
            end
 
134
          end
 
135
          raise IncorrectInput, "Not an axis type: #{str}"
 
136
        end
 
137
      end
 
138
 
 
139
      # LaTeX font
 
140
      class LaTeXFontBaseType < Type
 
141
        type_name :latex_font, 'latex font'
 
142
        
 
143
        def string_to_type_internal(str)
 
144
          return Graphics::Styles::LaTeXFont.from_text(str)
 
145
        end
 
146
      end
 
147
 
 
148
      # Colormap
 
149
      class LaTeXFontBaseType < Type
 
150
        type_name :colormap, 'color map'
 
151
        
 
152
        def string_to_type_internal(str)
 
153
          return Graphics::Styles::ColorMap.from_text(str)
 
154
        end
 
155
      end
 
156
 
 
157
    end
 
158
  end
 
159
end