~whatsthepalette/whatsthepalette/main

« back to all changes in this revision

Viewing changes to app/javascripts/wtpimage.js

  • Committer: Jesse Dunlap
  • Date: 2011-10-02 05:11:06 UTC
  • Revision ID: jdunlap0@gmail.com-20111002051106-0v9id723xno911x0
Ported the PHP image algorithm to JavaScript and added a test file (js_test.html)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
var WTPImage = {
 
2
        
 
3
        getColor: function(image, x, y)
 
4
        {
 
5
                var img         =       new Image();
 
6
                img.src         =       image;
 
7
                var ctx         =       document.getElementById('wtpc').getContext('2d');
 
8
                
 
9
                ctx.drawImage(img, 0, 0);
 
10
                return ctx.getImageData(x, y, 1, 1).data;
 
11
        },
 
12
        
 
13
        getColors: function(image, jump)
 
14
        {
 
15
                if (jump == null) jump = 1;
 
16
                
 
17
                var colors      =       [];
 
18
                
 
19
                var img         =       new Image();
 
20
                img.src         =       image;
 
21
                
 
22
                var height      =       img.height;
 
23
                var width       =       img.width;
 
24
                
 
25
                var x = 0;
 
26
                var y = 0;
 
27
                
 
28
                while (y <= height)
 
29
                {
 
30
                        while (x <= width)
 
31
                        {
 
32
                                colors[colors.length] = WTPImage.getColor(image, x, y);
 
33
                                x += jump;
 
34
                        }
 
35
                        
 
36
                        x = 0;
 
37
                        y += jump;
 
38
                }
 
39
                
 
40
                return colors;
 
41
        },
 
42
        
 
43
        similar: function(color1, color2, tolerance)
 
44
        {
 
45
                if (tolerance == null) tolerance = 0.01;
 
46
                
 
47
                tolerance = tolerance * (255 * 255 *3) << 0;
 
48
                
 
49
                var distance = 0;
 
50
                
 
51
                distance += Math.pow(color1[0] - color2[0], 2);
 
52
                distance += Math.pow(color1[1] - color2[1], 2);
 
53
                distance += Math.pow(color1[2] - color2[2], 2);
 
54
                
 
55
                return distance <= tolerance;
 
56
        },
 
57
        
 
58
        different: function(color, colors, tolerance)
 
59
        {
 
60
                if (tolerance == null) tolerance = 0.01;
 
61
 
 
62
                for (var i = 0; i < colors.length; i++)
 
63
                {
 
64
                        if (WTPImage.similar(color, colors[i], tolerance))
 
65
                        {
 
66
                                return false;
 
67
                        }
 
68
                }
 
69
                
 
70
                return true;
 
71
        },
 
72
        
 
73
        uniqueColors: function(colors, maximum, tolerance)
 
74
        {
 
75
                if (tolerance == null) tolerance = 0.01;
 
76
                
 
77
                var unique = [];
 
78
                
 
79
                for (var i = 0; i < colors.length; i++)
 
80
                {
 
81
                        if (unique.length >= maximum) return unique;
 
82
                        
 
83
                        if (WTPImage.different(colors[i], unique, tolerance))
 
84
                        {
 
85
                                unique[unique.length] = colors[i];
 
86
                        }
 
87
                }
 
88
                
 
89
                return unique;
 
90
        },
 
91
        
 
92
        getRGB: function(color)
 
93
        {
 
94
                return 'rgb(' + color[0] + ', ' + color[1] + ',' + color[2] + ')';
 
95
        },
 
96
        
 
97
        getRGBA: function(color)
 
98
        {
 
99
                return 'rgba(' + color[0] + ', ' + color[1] + ',' + color[2] + ', ' + color[3] + ')';
 
100
        }
 
101
        
 
102
};
 
 
b'\\ No newline at end of file'