color-picker

Gesponsorde koppelingen

PHP script bestanden

  1. color-picker

« Lees de omschrijving en reacties

Downloaden http://phphulp.phpclub.nl/656/color.zip

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
cpick.htm
  Choose A color: <input type="text" id="color1" size="12"><input id="colora1" type="button" value="" style="cursor:pointer;width:22px;height:22px;border:1px inset gray;background-color:#FF0000"  onclick="ShowLayer(1);"><BR>
  Choose A color: <input type="text" id="color2" size="12"><input id="colora2" type="button" value="" style="cursor:pointer;width:22px;height:22px;border:1px inset gray;background-color:#FF0000"  onclick="ShowLayer(2);"><BR>            
  Choose A color: <input type="text" id="color3" size="12"><input id="colora3" type="button" value="" style="cursor:pointer;width:22px;height:22px;border:1px inset gray;background-color:#FF0000"  onclick="ShowLayer(3);"><BR>
  Choose A color: <input type="text" id="color4" size="12"><input id="colora4" type="button" value="" style="cursor:pointer;width:22px;height:22px;border:1px inset gray;background-color:#FF0000"  onclick="ShowLayer(4);"><BR>            

  <!--COLOR PICKER WIDGET BEGIN -->
<div style="position:absolute;border:1px solid black;background-color:white;display:none;width:340px;height:420px;color:#000000;" id="main" imgLoc="">
</div>
<script language="JavaScript" src="cpick.js"></script>
<!--COLOR PICKER WIDGET END -->
</div>

cpick.js
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/*
Author: Eric Simmons
Contact: [email protected]
Website: http://www.jswitch.com
Version: 2.0 12/2004      
Browser Target: Mozilla 7+/FireFox Netscape 7+, IE 5.0+
Purpose : Its time for a badly needed update.

DISCLAIMER
THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT
ANY EXPRESS OR IMPLIED WARRANTIES, JSWITCH.COM
IS NOT RESPONSIBLE FOR ANY ADVERSE AFFECTS TO
YOUR COMPUTER OR SYSTEM RUNNING THESE SCRIPTS.

v 1.0
Initial implementation of the color tool.
Basic RGB hex color picker.

v2.0
new features:
Displayable <DIV> layer acts as a popup dailog.
Only requires 2 graphics files.
Can retian up to 8 personal colors.

v 2.1 6/05
When choosing a color on the color map it gets selected.                  
Fixed probelms with IE scrolling.
Added addition custom color space.
UI change for cleaner look and more defined user of controls.
                
  

fixes:
Cleaner JS for more clarity.
Code optimized for speed.


*/

//Changable variables
var STR_CAPTION = "JSwitch Color Picker"
/*This is the function that you can add your code to, or overide and access the color
in the SEL_COLOR variable. The SEL_COLOR variable is a hex value of the currently
selected color. It is set everytime a user clicks on the ok button.
After setting the SEL_COLOR var the BtnOk() function calls the "BtnOkClicked()";
function  BtnOkClicked()
{}
*/


var mouseX =0; //current horizontal position of the mouse
var mouseY =0; //current veritcal position of the mouse
var boxX =0;
var boxY =0;
var cpaneX = 0;
var cpaneY = 0;  
var cmapX = 0;
var cmapY = 0;
var cPaneT = 0;
var cPaneL = 0;
var cmapRegSel = 1;
var cmapReg = 1;
var cmapSel = 10;
var InCPane = false;
var InCMap = false;
var U_COL_NUM = 10;
var usrColor = new Array("NA","NA","NA","NA","NA","NA","NA","NA","NA","NA");
var SEL_COLOR;
var IMG_PATH

var PALLET_LAYER = null;


//CSS STYLES
var STYLE_BTN = 'text-align:center;cursor:pointer;font-family:\"TAHOMA\";font-size:11pt;font-weight:bold;width:60px;height:20px;background-color:ThreeDHighlight;position:absolute;';
var STYLE_BTN_OVER ='position:absolute;border:1px solid black;text-align:center;cursor:pointer;font-family:"TAHOMA";font-size:11pt;font-weight:bold;background-color:ThreeDHighlight';
var STYLE_BTN_DOWN ='border-bottom:1px solid ButtonHighlight;border-right:1px solid ButtonHighlight;border-top:1px solid ButtonShadow;border-left:1px solid ButtonShadow;';
var STYLE_BORD =    'position:absolute;border:1px solid black;';
var STYLE_CMAP =    'position:absolute;border:1px solid black;background-image:url(\''+document.getElementById("main").getAttribute("imgLoc")+'cmap.jpg\'); background-repeat:no-repeat;';
//var STYLE_MASK_MZ = 'position:absolute;border:1px solid black;cursor:crosshair;width:257px;height:258px;left:5px;top:30px;';
var STYLE_MASK_MS = 'position:absolute;border:1px solid black;cursor:crosshair;width:257px;height:258px;left:5px;top:30px;';
var STYLE_USER =    'position:absolute;border:1px solid black;width:20px;height:20px;cursor:pointer;';
var STYLE_TAG =     'position:absolute;border:1px solid black;color:white;text-align:center;width:40px;height:20px';
var STYLE_TEXT =    'width:30px;height:20px;position:absolute;';
var STYLE_COMP =    'position:absolute;border:1px solid black;width:60px;height:20px;';

document.onmousemove = getXY; //get new cords when mouse is moved  
if (navigator.appName == "Microsoft Internet Explorer")
{
    cPaneT = 34;
    cPaneL = 9;
} else
{
    cPaneT = 32;
    cPaneL = 6;
    document.captureEvents(Event.MOUSEMOVE);
}

GetCookieArray();

function ShowLayer(id,id2)
{
    var mainDiv = document.getElementById("main");
    
        boxX = mouseX;
        boxY = mouseY;
        mainDiv.style.top = boxY
        mainDiv.style.left = boxX;
        
        
        mainDiv.style.display = 'inline';
           
    date = new Date();
    date.setTime(date.getTime() + (1000 * 60 * 60 * 24 * 30));
  
         document.cookie = 'Field='+id+'; expires=' + date.toGMTString();
//        document.cookie = 'Field2='+id2+';  expires=' + date.toGMTString();
         
        IMG_PATH = mainDiv.getAttribute("imgLoc");
        mainDiv.innerHTML= MakeDivLayer();
        //mainDiv.innerHTML=CreatePalette();

        if(navigator.appName == "Microsoft Internet Explorer")
            document.getElementById("cPaneMask").style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+ IMG_PATH +"cmask.png', sizingMethod='scale')" ;
        else
            document.getElementById("cPaneMask").style.backgroundImage = 'url('+IMG_PATH+'cmask.png)';
        UpdateUserColor();
        ChangePaneColor("FF0000");
}

function SetUserColor()
{
    curCol = document.getElementById("cComp1").style.backgroundColor;
    for (i=U_COL_NUM-1;i>0;i--)
    {
        usrColor[i] = usrColor[i-1];
    }
    if(curCol.substr(0,1) == "r")
        curCol = rgb2hex(curCol);
    usrColor[0] = curCol;
    UpdateUserColor();
    UpdateUserCookie();
}

function UpdateUserColor()
{  
    divName = "cUser";
    for (i=0;i<U_COL_NUM;i++)
    {
        tempName =divName + i;
        if (usrColor[i] != "NA")
            document.getElementById(tempName).style.backgroundColor = usrColor[i];
    }
}

function UpdateUserCookie()
{
    date = new Date();
    date.setTime(date.getTime() + (1000 * 60 * 60 * 24 * 30));
    document.cookie = "cUser" + "=" + escape(usrColor.toString()) + "; expires=" + date.toGMTString();  
}

function GetCookieArray()
{      
   tmpColor = GetUserCookie("cUser").split(",");
   for(i=0;i<tmpColor.length;i++)
   {
       if(tmpColor[i] != "")
       {    
           usrColor[i] = tmpColor[i];
       }
              
   }
}

function SetRetainColor(userObj)
{
    newColor =  userObj.style.backgroundColor;
    document.getElementById("cComp1").style.backgroundColor = newColor;

    if(newColor.substr(0,1) == "r")
        document.getElementById("thex").value = rgb2hex(newColor);
    else
        document.getElementById("thex").value = newColor;
}

function GetUserCookie(crumbName)
{
    colCookie = document.cookie.split("; ");
    
    for (a=0; a < colCookie.length; a++)
    {
        colCrumb = colCookie[a].split("=");                    
        if(colCrumb[0] == crumbName)
            return unescape(colCrumb[1]);
    }

    return "";

}

function getXY(e)
{
    if (!e)
    {
        mouseX = event.clientX + document.body.scrollLeft;
        mouseY = event.clientY + document.body.scrollTop;
    } else
    {
        mouseX = e.pageX;
        mouseY = e.pageY;
    }    

    
    if (InCPane)
    {
        cpaneX = mouseX - boxX-cPaneL;
        cpaneY = mouseY - boxY-cPaneT;
        if (cpaneX < 0) cpaneX = 0;
        if (cpaneX > 255) cpaneX = 255;
        if (cpaneY < 0) cpaneY = 0;
        if (cpaneY > 255) cpaneY = 255;
        FindColor();
    }

    if (InCMap)
    {
        cmapX =  mouseX - boxX-272;
        cmapY =  mouseY - boxY-32;
        if (cmapX < 1) cmapX = 1;
        if (cmapX > 60) cmapX = 60;
        if (cmapY < 0) cmapY = 0;
        if (cmapY > 255) cmapY = 255;
        cmapReg =  Math.ceil(cmapX/10);
    }

}

function GetMapColor()
{
    var rtnColor
  
    cmapSel = cmapY;
    document.getElementById("cmapCur").style.top  =cmapY-1;
    document.getElementById("cmapCur").style.left  = ((cmapReg-1)*10);
    cmapRegSel = cmapReg;
    switch (cmapReg)
    {
    case 1:
        rtnColor = '#' + dec2hex(255) + dec2hex(0) + dec2hex(cmapSel);
        break;
    case 2:
        rtnColor = '#' + dec2hex(cmapSel) + dec2hex(0) + dec2hex(255);
        break;
    case 3:
        rtnColor = '#' + dec2hex(0) + dec2hex(cmapSel) + dec2hex(255);
        break;
    case 4:
        rtnColor = '#' + dec2hex(0) + dec2hex(255) + dec2hex(cmapSel);
        break;
    case 5:
        rtnColor = '#' + dec2hex(cmapSel) + dec2hex(255) + dec2hex(0);
        break;
    case 6:
        rtnColor = '#' + dec2hex(255) + dec2hex(cmapSel) + dec2hex(0);
        break;  
    }  
    return rtnColor;
}

function GetNewColor()
{
    newColor = FindColor();
    document.getElementById("cComp1").style.backgroundColor = newColor;
    document.getElementById("thex").value = newColor;
}

function FindColor()
{
    var X;
    var Y;
    var Z;
    var R;
    var G;
    var B;
    
    X = cpaneX;
    Y = Math.round (cpaneY * (X/255));
    Z = (cpaneY * ((255-cmapSel)/255)) + cmapSel;
    Z = Math.round( Z * (cpaneX/255));
    
    switch (cmapRegSel)
    {
    case 1:
        R = X;G = Y;B = Z;
        break;
    case 2:
        R = Z;G = Y;B = X;
        break;
    case 3:
        R = Y;G = Z;B = X;
        break;
    case 4:
        R = Y;G = X;B = Z;
        break;
    case 5:
        R = Z;G = X;B = Y;
        break;
    case 6:
        R = X;G = Z;B = Y;
        break;  
    }

    bgRed   = '#' + dec2hex(R) + dec2hex(0) + dec2hex(0);
    bgGrn   = '#' + dec2hex(0) + dec2hex(G) + dec2hex(0);
    bgBlu   = '#' + dec2hex(0) + dec2hex(0) + dec2hex(B);
    rtnColor = '#' + dec2hex(R) + dec2hex(G) + dec2hex(B);
    
    document.getElementById("red").value = String(R);  
    document.getElementById("grn").value = String(G);
    document.getElementById("blu").value = String(B);
    document.getElementById("red").style.backgroundColor = bgRed;  
    document.getElementById("grn").style.backgroundColor = bgGrn;
    document.getElementById("blu").style.backgroundColor = bgBlu;
    document.getElementById("cComp2").style.backgroundColor = rtnColor;
    return rtnColor;
}

function FindColorPoint(pointX, pointY)
{
    var X;
    var Y;
    var Z;
    var R;
    var G;
    var B;
    
    X = pointX;
    Y = Math.round (pointY * (X/255));
    Z = (cpaneY * ((255-cmapSel)/255)) + cmapSel;
    Z = Math.round( Z * (pointX/255));
    
    switch (cmapRegSel)
    {
    case 1:
        R = X;G = Y;B = Z;
        break;
    case 2:
        R = Z;G = Y;B = X;
        break;
    case 3:
        R = Y;G = Z;B = X;
        break;
    case 4:
        R = Y;G = X;B = Z;
        break;
    case 5:
        R = Z;G = X;B = Y;
        break;
    case 6:
        R = X;G = Z;B = Y;
        break;  
    }

    bgRed   = '#' + dec2hex(R) + dec2hex(0) + dec2hex(0);
    bgGrn   = '#' + dec2hex(0) + dec2hex(G) + dec2hex(0);
    bgBlu   = '#' + dec2hex(0) + dec2hex(0) + dec2hex(B);
    rtnColor = '#' + dec2hex(R) + dec2hex(G) + dec2hex(B);
    return rtnColor;
}


function ChangePaneColor(nColor)
{
    document.getElementById("cPane").style.backgroundColor = nColor;
    document.getElementById("cComp1").style.backgroundColor = nColor;
    document.getElementById("thex").value = nColor;
}

function rgb2hex(rgb)
{
    rExp = new RegExp("RGB", "i");
    rgb = rgb.replace(rExp,"");
    rgb = rgb.substring(1,rgb.length-1);
    rgbAry = rgb.split(", ");
    rtnStr = "#" + dec2hex(parseInt(rgbAry[0])) + dec2hex(parseInt(rgbAry[1])) + dec2hex(parseInt(rgbAry[2]))
    return rtnStr;
}
function dec2hex(n)
{
    s=(n.toString(16));
    s=s.toUpperCase();
    if (s.length == 1)
        s = '0' + s
    return(s);
}

function BtnCancel()
{
    document.getElementById("main").style.display = "none";
    cmapRegSel = 1;
    cmapSel = 0;
}


function BtnOk()
{
    SEL_COLOR = document.getElementById("thex").value
    BtnCancel();
    BtnOkClicked();
}


function CreatePalette()
{
    var newPalLayer = "";
    ptop = 0;
    pleft = 5;
    cNum = 0;
    for(rows=0;rows < 10; rows++)
    {
        for (cols=0;cols<10;cols++)
        {
            newPalLayer += '<div style="background-color:'+ FindColorPoint(10, cNum) +';left:'+pleft +'px;top:'+ ptop +'px;'+STYLE_USER+'"  onclick="SetRetainColor(this);"></div>';
            pleft += 25;
            cNum += 2;
        }
        pleft = 5;
        ptop += 25;
        cNum += 2;
    }
    //alert(newPalLayer);
    return newPalLayer;
}

function MakeDivLayer()
{
    var newDivLayer;
    newDivLayer = '<div id="title" style="width:337px;height:22px;border-bottom:1px solid black;font-weight:bold;color:white;text-align:center;background-color:#0C58C2;">' + STR_CAPTION + '</div>';
    newDivLayer += '<div id="cPane" style="border-left:1px solid black;position:absolute;width:257px;height:258px;left:5px;top:30px;background-color:#FF0000;"></div>';
    newDivLayer += '<div style="'+STYLE_MASK_MS+'" id="cPaneMask" onmouseout="InCPane=false;" onMouseMove="InCPane=true;" onclick="GetNewColor();"></div>';
    newDivLayer += '<div style="cursor:crosshair;width:60px;height:258px;left:270px;top:30px;'+STYLE_CMAP+'" id="cMap" onclick="ChangePaneColor(GetMapColor());" onmouseout="InCMap=false;" onMouseMove="InCMap=true;">';
    newDivLayer += '<div id="cmapCur" style="position:absolute;left:0px;top:0px;width:10px;height:1px;border-bottom:1px solid black"><!-- --></div>';
    newDivLayer += '</div>';
  
    newDivLayer += '<span style="position:absolute;left:5px;top:290px;">Custom colors:</span>';
    newDivLayer += '<input type="button" style="height:20px;width:45px;left:7px;top:310px;'+STYLE_BTN_OVER+'" id="btnUser" onclick="SetUserColor();" value=">>">';
    newDivLayer += '<div style="left:55px;top:310px;'+STYLE_USER+'"  id="cUser0" onclick="SetRetainColor(this);"></div>';
    newDivLayer += '<div style="left:80px;top:310px;'+STYLE_USER+'"  id="cUser1" onclick="SetRetainColor(this);"></div>';
    newDivLayer += '<div style="left:5px;top:335px;'+STYLE_USER+'" id="cUser2" onclick="SetRetainColor(this);"></div>';
    newDivLayer += '<div style="left:30px;top:335px;'+STYLE_USER+'" id="cUser3" onclick="SetRetainColor(this);"></div>';
    newDivLayer += '<div style="left:55px;top:335px;'+STYLE_USER+'" id="cUser4" onclick="SetRetainColor(this);"></div>';
    newDivLayer += '<div style="left:80px;top:335px;'+STYLE_USER+'" id="cUser5" onclick="SetRetainColor(this);"></div>';
    newDivLayer += '<div style="left:5px;top:360px;'+STYLE_USER+'" id="cUser6" onclick="SetRetainColor(this);"></div>';
    newDivLayer += '<div style="left:30px;top:360px;'+STYLE_USER+'" id="cUser7" onclick="SetRetainColor(this);"></div>';
    newDivLayer += '<div style="left:55px;top:360px;'+STYLE_USER+'" id="cUser8" onclick="SetRetainColor(this);"></div>';
    newDivLayer += '<div style="left:80px;top:360px;'+STYLE_USER+'" id="cUser9" onclick="SetRetainColor(this);"></div>';
        
    newDivLayer += '<span style="position:absolute;left:137px;top:290px;">RGB:</span>';    
    newDivLayer += '<div style="left:110px;top:310px;'+STYLE_TEXT+'"  >red:</div>';
    newDivLayer += '<div style="left:110px;top:335px;'+STYLE_TEXT+'"  >grn:</div>';
    newDivLayer += '<div style="left:110px;top:360px;'+STYLE_TEXT+'" >blu:</div>';
    newDivLayer += '<input type="text" style="none;left:135px;top:310px;'+STYLE_TAG+'"  id="red">';
    newDivLayer += '<input type="text" style="none;left:135px;top:335px;'+STYLE_TAG+'" id="grn">';
    newDivLayer += '<input type="text" style="none;left:135px;top:360px;'+STYLE_TAG+'" id="blu">';
    
    newDivLayer += '<span style="position:absolute;left:277px;top:290px;">Color:</span>';
    newDivLayer += '<span style="position:absolute;left:190px;top:310px;">Finder:</span>';
    newDivLayer += '<span style="position:absolute;left:190px;top:335px;">Selected:</span>';
    newDivLayer +='<div  style="left:270px;top:310px;'+STYLE_COMP+'" id="cComp2"></div>';
    newDivLayer += '<div style="left:270px;top:335px;'+STYLE_COMP+'" id="cComp1"></div>';
    newDivLayer += '<div style="'+STYLE_TEXT+';width:90px;left:190px;top:360px;">Triple Hex:</div>';
    newDivLayer += '<input style="text-align:center;width:70px;height:20px;left:260px;top:360px;'+STYLE_BORD+'" type="text" ID="thex">';
    
    newDivLayer += '<input type="button" style="width:60px;left:5px;top:390px;'+STYLE_BTN_OVER+'"  id="btnOK"  onclick="BtnOk();" value="ok">';
    newDivLayer += '<input type="button" style="width:60px;left:70px;top:390px;'+STYLE_BTN_OVER+'"  id="btnCan" onclick="BtnCancel();" value="close" >';
    //newDivLayer += "</div>
    return newDivLayer;
}
function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=")
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1
    c_end=document.cookie.indexOf(";",c_start)
    if (c_end==-1) c_end=document.cookie.length
    return unescape(document.cookie.substring(c_start,c_end))
    }
  }
return null
}
function BtnOkClicked()
{
    var field=getCookie('Field');
    //document.write(document.cookie);
    
     document.getElementById('color'+field).value = SEL_COLOR;
     document.getElementById('colora'+field).style.backgroundColor = SEL_COLOR;
}

function UpdateColor(id){
    //get value
    var color = document.getElementById('color'+id).value;
        x = document.getElementById('colora'+id);
        x.style.backgroundColor= color;
        return;
}

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.