1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.diagram.internal;
14 import java.util.HashMap;
17 import org.eclipse.jface.resource.ImageDescriptor;
18 import org.eclipse.ui.plugin.AbstractUIPlugin;
19 import org.osgi.framework.Bundle;
20 import org.osgi.framework.BundleContext;
21 import org.simantics.datatypes.literal.RGB;
22 import org.simantics.datatypes.literal.Vec2d;
23 import org.simantics.scl.runtime.function.Function3;
24 import org.simantics.scl.runtime.function.FunctionImpl3;
25 import org.simantics.utils.FileUtils;
28 * @author Tuukka Lehtonen
30 public class Activator extends AbstractUIPlugin {
33 public static final String PLUGIN_ID = "org.simantics.diagram";
35 // The shared instance
36 private static Activator plugin;
38 public static ImageDescriptor LINK_ICON;
39 public static ImageDescriptor LINK_BREAK_ICON;
41 private static Map<String, String> iconTexts = new HashMap<String, String>();
42 private static Map<String, String> iconDocumentCache = new HashMap<String, String>();
44 public static Function3<String, RGB.Integer, Vec2d, String> ICON_PROVIDER = new FunctionImpl3<String, RGB.Integer, Vec2d, String>() {
46 private String hex2(int value) {
47 String result = Integer.toHexString(value);
48 if(result.length() == 1) result = "0" + result;
52 private int saturate(int in, double factor) {
53 return (int)((double)in + factor*(double)(255-in));
56 private int darken(int in, double factor) {
57 return (int)((double)in*factor);
61 public String apply(String iconName, RGB.Integer rgb, Vec2d scale) {
63 int r = rgb.red, g = rgb.green, b = rgb.blue;
64 String base = iconTexts.get(iconName);
65 if(base == null) return null;
67 int c4 = (r << 16) + (g << 8) + b;
68 String key = iconName+c4+"#" + scale.x + "#" + scale.y;
70 String cached = iconDocumentCache.get(key);
72 int c1 = (saturate(r,0.83) << 16) + (saturate(g, 0.83) << 8) + saturate(b, 0.83);
73 int c2 = (saturate(r,0.5) << 16) + (saturate(g, 0.5) << 8) + saturate(b, 0.5);
74 int c3 = (saturate(r,0.16) << 16) + (saturate(g, 0.16) << 8) + saturate(b, 0.16);
75 int c5 = (darken(r,0.83) << 16) + (darken(g, 0.83) << 8) + darken(b, 0.83);
76 base = base.replace("!!c1!!", hex2(c1));
77 base = base.replace("!!c2!!", hex2(c2));
78 base = base.replace("!!c3!!", hex2(c3));
79 base = base.replace("!!c4!!", hex2(c4));
80 base = base.replace("!!scalex!!", "" + scale.x);
81 base = base.replace("!!scaley!!", "" + scale.y);
82 cached = base.replace("!!c5!!", hex2(c5));
83 iconDocumentCache.put(key, cached);
93 * @see org.eclipse.core.runtime.Plugins#start(org.osgi.framework.BundleContext)
96 public void start(BundleContext context) throws Exception {
100 Bundle bundle = context.getBundle();
102 iconTexts.put("LED", FileUtils.getContents(bundle.getResource("icons/IconLed.svg")));
103 iconTexts.put("TERMINAL", FileUtils.getContents(bundle.getResource("icons/IconTerminal.svg")));
104 iconTexts.put("SQUARE", FileUtils.getContents(bundle.getResource("icons/IconSquare.svg")));
106 iconTexts.put("BUTTON_ON", FileUtils.getContents(bundle.getResource("icons/ButtonOn.svg")));
107 iconTexts.put("BUTTON_OFF", FileUtils.getContents(bundle.getResource("icons/ButtonOff.svg")));
109 LINK_ICON = ImageDescriptor.createFromURL(bundle.getResource("icons/link.png"));
110 LINK_BREAK_ICON = ImageDescriptor.createFromURL(bundle.getResource("icons/link_break.png"));
116 * @see org.eclipse.core.runtime.Plugin#stop(org.osgi.framework.BundleContext)
119 public void stop(BundleContext context) throws Exception {
125 * Returns the shared instance
127 * @return the shared instance
129 public static Activator getDefault() {