colorbrewer2.html 5.06 KB
Newer Older
Koukofikis's avatar
Koukofikis committed
1
2
3
4
5
6
7
8
9
10
11
12
<html>

<head>
  <meta charset="utf-8">
  <title>Material brewer</title>
  <!-- source Colorpicker slider: http://jqueryui.com/slider/#colorpicker -->
  <link rel="stylesheet" type="text/css" href="x3dom.css">
  <link rel="stylesheet" href="https://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  </link>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
  <script src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  </link>
Mandic's avatar
Mandic committed
13
14
15
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.1/styles/vs.min.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.1/highlight.min.js"></script>
  <script>hljs.highlightAll();</script>
Koukofikis's avatar
Koukofikis committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  <style>
    #red,
    #green,
    #blue {
      float: left;
      clear: left;
      width: 300px;
      margin: 15px;
    }

    #red .ui-slider-range {
      background: #ef2929;
    }

    #red .ui-slider-handle {
      border-color: #ef2929;
    }

    #green .ui-slider-range {
      background: #8ae234;
    }

    #green .ui-slider-handle {
      border-color: #8ae234;
    }

    #blue .ui-slider-range {
      background: #729fcf;
    }

    #blue .ui-slider-handle {
      border-color: #729fcf;
    }
  </style>
</head>

<body class="ui-widget-content" style="border:0;">
  <table width="100%" height="85%" border="0">
    <tr>
      <td width="50%" align="center" valign="top" scope="col">
        <X3D id="shapedata" width="600px" height="600px" style="float:left">
          <Scene>
            <Shape>
              <Appearance>
                <Material id="material" diffuseColor="0.980, 0.502, 0.447"> </Material>
              </Appearance>
              <Sphere radius="2.0"> </Sphere>
            </Shape>
          </Scene>
        </X3D>
      </td>
      <td width="50%" align="left" valign="top" scope="col">
Mandic's avatar
Mandic committed
68
        <p>The following example will show you how to interactively modify the 3D scene.</p>
Koukofikis's avatar
Koukofikis committed
69
70
71
72
73
74
        <div id="red"></div>
        <div id="green"></div>
        <div id="blue"></div>
        <p>
          <br><br><br><br><br><br><br><br><br><br>
          The diffuse color of a material will be interactively changed.
Mandic's avatar
Mandic committed
75
          A JavaScript <a href="http://jqueryui.com/slider/#colorpicker target=" _blank"> RGB color picker </a> is used
Koukofikis's avatar
Koukofikis committed
76
77
78
79
          and modified.
          If the RGB color value is changed, the diffuse material of the sphere shall be changed as well. So we need to
          get the Material node first.
          This is done by the getElementById function in X3DOM. Of course, the Material node needs to have an unique id.
Mandic's avatar
Mandic committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
          <pre>
          <code class="language-html">
&lt;html&gt; 
&lt;head&gt; 
    &lt;title&gt;colorbrewer&lt;/title&gt; 			
    &lt;script type='text/javascript' src='http://www.x3dom.org/download/x3dom.js'&gt; &lt;/script&gt; 
    &lt;link rel='stylesheet' type='text/css' href='http://www.x3dom.org/download/x3dom.css'&gt;&lt;/link&gt; 
&lt;/head&gt; 
&lt;body&gt;
  &lt;X3D id="shapedata" width="600px" height="600px" style="float:left"&gt;
    &lt;Scene&gt;
      &lt;Shape&gt; 
            &lt;Appearance&gt;
                &lt;Material id="material" diffuseColor="0.980, 0.502, 0.447"&gt; &lt;/Material&gt;
            &lt;/Appearance&gt;
        &lt;Sphere radius="2.0"&gt; &lt;/Sphere&gt;
        &lt;/Shape&gt;
    &lt;/Scene&gt;
  &lt;/X3D&gt;
&lt;/body&gt; 
&lt;/html&gt; 
</code>
</pre>    
Koukofikis's avatar
Koukofikis committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
        <br>
        For example, if the diffuse color should be set to red, the Material node is referenced by its id and the
        attribute diffuseColor is set to
        "1.0 0.0 0.0". The attribute value is always a string.
        <pre>
    var mat = document.getElementById("material");
    mat.setAttribute("diffuseColor", "1.0 0.0 0.0");      
    </pre>
        In the example, the RGB value is fetched from the slider. As the slider gives the RGB values between 0 and 255,
        they have to be transformed to
        arithmetic values between 0.0 and 1.0. After that, the RBG value has to be formatted as string.
        <pre>
      var mat = document.getElementById("material");
      rgbstring = " "+ red/255 + " " + green/255 + " " + blue/255;
      mat.setAttribute("diffuseColor", rgbstring);
    </pre>
Mandic's avatar
Mandic committed
119
        </p>
Koukofikis's avatar
Koukofikis committed
120
      </td>
Mandic's avatar
Mandic committed
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
      <script type="text/javascript" src="x3dom.js"></script>
      <script>
        function refreshDiffuseMaterial() {
          var red = $("#red").slider("value"),
            green = $("#green").slider("value"),
            blue = $("#blue").slider("value");
          var mat = document.getElementById("material");
          rgbstring = " " + red / 255 + " " + green / 255 + " " + blue / 255;
          mat.setAttribute("diffuseColor", rgbstring);
        }
        $(function () {
          $("#red, #green, #blue").slider({
            orientation: "horizontal",
            range: "min",
            max: 255,
            value: 127,
            slide: refreshDiffuseMaterial,
            change: refreshDiffuseMaterial
          });
          $("#red").slider("value", 255);
          $("#green").slider("value", 140);
          $("#blue").slider("value", 60);
        });
      </script>
Koukofikis's avatar
Koukofikis committed
145
146
</body>

Mandic's avatar
Mandic committed
147
</html>