Debian Codegraph @

closure-library/closure/goog/demos/color-contrast.html

raw
1 <!DOCTYPE html>
2
3 <html>
4 <!--
5 Copyright 2010 The Closure Library Authors. All Rights Reserved.
6
7 Use of this source code is governed by the Apache License, Version 2.0.
8 See the COPYING file for details.
9 -->
10 <head>
11 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
12 <title>Color Contrast Test</title>
13 <style type="text/css" media="screen">
14 #preview {
15 margin-left: 1em;
16 padding: 0.5em 1em;
17 background: #ccc;
18 }
19 </style>
20 </head>
21 <body>
22 <script type="text/javascript" src="../base.js"></script>
23
24 <p>
25 #<input type="text" value="cccccc" id="color" size="6" maxlength="6">
26 <input
27 type="button"
28 value="Black or White?"
29 id="submit"
30 onclick="blackOrWhite()">
31 <span id="preview">This text should be readable</span>
32 </p>
33 <p>(Only choosing from black and white.)</p>
34
35 <script type="text/javascript" charset="utf-8">
36
37 goog.require('goog.color');
38
39 function blackOrWhite() {
40
41 var colorInput = document.getElementById('color');
42 var previewElement = document.getElementById('preview');
43
44 var bgRgb = goog.color.hexToRgbStyle('#' + colorInput.value);
45 var bgRgbArr = goog.color.parseRgb(bgRgb);
46
47 var bestColor = goog.color.highContrast(
48 bgRgbArr, [[0, 0, 0], [255, 255, 255]]);
49
50 var bestColorHex = goog.color.rgbArrayToHex(bestColor);
51
52 console.info(bestColorHex + ' wins on sum');
53 previewElement.style.backgroundColor = '#' + colorInput.value;
54 previewElement.style.color = bestColorHex;
55 }
56
57 </script>
58
59 </body>
60 </html>