de.instanttouch.ui.scaffolding.swt.util.SnakeSwtUtils.java Source code

Java tutorial

Introduction

Here is the source code for de.instanttouch.ui.scaffolding.swt.util.SnakeSwtUtils.java

Source

/*******************************************************************************
 *  Copyright (c) 2015 Joachim Tessmer.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *  
 *  Contributors:
 *      Joachim Tessmer - initial API and implementation
 *******************************************************************************/
package de.instanttouch.ui.scaffolding.swt.util;

import java.net.URL;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Display;

public class SnakeSwtUtils {

    public static Image createImage(String urlStr) {

        if (urlStr != null && urlStr.length() > 0) {
            try {
                URL url = new URL(urlStr);
                ImageDescriptor imageDescriptor = ImageDescriptor.createFromURL(url);
                Image image = imageDescriptor.createImage();
                return image;
            } catch (Exception e) {
                try {
                    Image image = new Image(Display.getCurrent(), urlStr);
                    return image;
                } catch (Exception e1) {
                }
            }
        }

        return null;
    }

    public static Color createFromHexString(String colorStr) {

        Color color = null;
        try {
            if (colorStr != null) {
                int r = Integer.valueOf(colorStr.substring(1, 3), 16);
                int g = Integer.valueOf(colorStr.substring(3, 5), 16);
                int b = Integer.valueOf(colorStr.substring(5, 7), 16);

                RGB rgb = new RGB(r, g, b);
                color = new Color(Display.getCurrent(), rgb);
            }

        } catch (Exception e) {
        }

        return color;
    }
}