Color to HSL value : Color « Mochkit « JavaScript DHTML






Color to HSL value

 
<!--
MochiKit is dual-licensed software.  It is available under the terms of the
MIT License, or the Academic Free License version 2.1.  The full text of
each license is included below.
-->

<!-- Code revised from MochiKit demo code -->

<html>
<head>
    <script type="text/javascript" src="MochiKit-1.4.2/lib/MochiKit/Base.js"></script>
    <script type="text/javascript" src="MochiKit-1.4.2/lib/MochiKit/Iter.js"></script>
    <script type="text/javascript" src="MochiKit-1.4.2/lib/MochiKit/DOM.js"></script>
    <script type="text/javascript" src="MochiKit-1.4.2/lib/MochiKit/Style.js"></script>
    <script type="text/javascript" src="MochiKit-1.4.2/lib/MochiKit/Logging.js"></script>
    <script type="text/javascript" src="MochiKit-1.4.2/lib/MochiKit/Color.js"></script>
</head>
<body>
<div style="position:absolute; top: 0px; left:0px; width:0px; height:0px">
    <span style="color: red" id="c_direct"></span>
    <span class="redtext" id="c_indirect"></span>
</div>
<pre id="test">
<script type="text/javascript">
    var approx = function (a, b, msg) {
        return alert(msg+":"+(a.toPrecision(4) == b.toPrecision(4)));
    };

    var hsl = Color.redColor().asHSL();
    approx( hsl.h, 0.0, "red hsl.h" );
    approx( hsl.s, 1.0, "red hsl.s" );
    approx( hsl.l, 0.5, "red hsl.l" );
    hsl = Color.fromRGB(0, 0, 0.5).asHSL();
    approx( hsl.h, 2/3, "darkblue hsl.h" );
    approx( hsl.s, 1.0, "darkblue hsl.s" );
    approx( hsl.l, 0.25, "darkblue hsl.l" );
    hsl = Color.fromString("#4169E1").asHSL();
    approx( hsl.h, (5/8), "4169e1 h");
    approx( hsl.s, (8/11), "4169e1 s");
    approx( hsl.l, (29/51), "4169e1 l");
    hsl = Color.fromString("#555544").asHSL();
    approx( hsl.h, (1/6), "555544 h" );
    approx( hsl.s, (1/9), "555544 s" );
    approx( hsl.l, (3/10), "555544 l" );
    hsl = Color.fromRGB(0.5, 1, 0.5).asHSL();
    approx( hsl.h, 1/3, "aqua hsl.h" );
    approx( hsl.s, 1.0, "aqua hsl.s" );
    approx( hsl.l, 0.75, "aqua hsl.l" );

</script>
</pre>
</body>
</html>

   
  








Related examples in the same category

1.Color.toRGBString()
2.Convert color from RGB value
3.Convert Hex string to color
4.Convert string to color
5.Convert RGB string to color
6.Color.fromString("rgb(190,222,173)").toHexString()
7.Color.fromString("hsl(120,100%,75%)").toHexString()
8.Convert color to HSL string
9.Convert color from HSL value
10.Reference components from HSL value
11.Convert HSL value to color and convert from RGB
12.Reference components from a HSL color
13.Color.fromName: Convert static string to color: aqua, transparent
14.Transparent color
15.Color.fromString: Covnert static string to color
16.Color.fromRGBString("rgba( 0, 255, 255, 50%)").asRGB().a
17.Convert color value to HSV
18.Convert HSV value to color
19.Reference components from HSV value
20.Get color from tag style
21.Convert string(2005-2-3) to iso Date
22.Compare two colors
23.Static color methods in Color class
24.Default colors are interned
25.Demo of MochiKit.Color