CoLD - Color the Linked Data Web!

CodeSnippets

The algorithm to color the Linked Data Web is quite complex :-)
We are hashing the given IRI using md5 and cutting the last 6 characters out of the resulting hash. Thats it!

But to lower the barrier of integrating the algorithm into your application, you can find a few snippets in common languages in the following. Please copy the respective snippet to your clipboard and paste it to your source code.

PHP Snippet

        $iri  = "http://iri/to-be/color.ed/" ;
        $hash = hash("md5",$iri) ;
        $rgb  = "#" . substr($hash, strlen($hash) -6, strlen($hash));
        echo $rgb;
        

JavaScript Snippet

        <script src="http://crypto-js.googlecode.com/svn/
                        tags/3.1.2/build/rollups/md5.js"></script>
        <script>
            var hash = CryptoJS.MD5("Message");
        </script>

        var iri = "http://iri/to-be/color.ed/";
        CryptoJS.MD5(iri);
        var rgb = "#" + hash.substr(-6);
        

Java Snippet

        public static String md5Hash(byte[] bytes) {
            MessageDigest md5 = null;
            try {
                md5 = MessageDigest.getInstance("MD5");
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
            md5.reset();
            md5.update(bytes);
            byte[] rawResult = md5.digest();
            return bytesToHexString(rawResult);
        }
        public static String bytesToHexString(byte[] bytes) {
            String result = "";
            for (int i = 0; i < bytes.length; i++) {
                int value = 0xff & bytes[i];
                if(value < 16) {
                    result += "0";
                }
                result += Integer.toHexString(value);
            }
            return result;
        }

        public static String md5Hash(String string) {
            return md5Hash(string.getBytes());
        }

        public static String getUriColor(String uri) {
            String hash = StringUtils.md5Hash("01");
            String color = "#" + hash.substring(hash.length() - 6);
            return color;
        }
        

Maven + Java Snippet

        #Maven pom.xml snippet:
        <repositories>
            <repository>
                <id>maven.aksw.org/internal</id>
                <name>University Leipzig, AKSW Maven2 Internal Repository</name>
                <url>http://maven.aksw.org/repository/internal/</url>
            </repository>
            ...
        <dependencies>
            <dependency>
                <groupId>org.aksw.commons</groupId>
                <artifactId>util</artifactId>
                <version>0.1</version>
                <scope>compile</scope>
            <dependency>
            ...
            
        #Java Snippet
        import org.aksw.commons.util.strings.StringUtils;
        String hash = StringUtils.md5Hash("01");
        String color = "#" + hash.substring(hash.length() - 6);
        

Shell Snippet

        $iri "http://iri/to-be/color.ed/"
        echo -n $iri | md5 | cut -c 27-