README.md (3813B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # Hexadecimal Color 22 23 > [Regular expression][mdn-regexp] to match a hexadecimal color. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var reColorHexadecimal = require( '@stdlib/regexp/color-hexadecimal' ); 31 ``` 32 33 #### reColorHexadecimal( \[mode] ) 34 35 Returns a [regular expression][mdn-regexp] to match a full hexadecimal color. 36 37 ```javascript 38 var RE = reColorHexadecimal(); 39 // returns <RegExp> 40 41 var bool = RE.test( 'ffffff' ); 42 // returns true 43 44 bool = RE.test( '000' ); 45 // returns false 46 ``` 47 48 To return a [regular expression][mdn-regexp] that matches a shorthand hexadecimal color, set the `mode` argument to `shorthand`. 49 50 ```javascript 51 var RE = reColorHexadecimal( 'shorthand' ); 52 // returns <RegExp> 53 54 var bool = RE.test( '000' ); 55 // returns true 56 ``` 57 58 To return a [regular expression][mdn-regexp] that matches **either** a shorthand or a full length hexadecimal color, set the `mode` argument to `either`. 59 60 ```javascript 61 var RE = reColorHexadecimal( 'either' ); 62 // returns <RegExp> 63 64 var bool = RE.test( '000' ); 65 // returns true 66 ``` 67 68 #### reColorHexadecimal.REGEXP 69 70 [Regular expression][mdn-regexp] to match a full length hexadecimal color. 71 72 ```javascript 73 var bool = reColorHexadecimal.REGEXP.test( 'ffffff' ); 74 // returns true 75 76 bool = reColorHexadecimal.REGEXP.test( '000' ); 77 // returns false 78 ``` 79 80 #### reColorHexadecimal.REGEXP_SHORTHAND 81 82 [Regular expression][mdn-regexp] to match a shorthand hexadecimal color. 83 84 ```javascript 85 var bool = reColorHexadecimal.REGEXP_SHORTHAND.test( 'ffffff' ); 86 // returns false 87 88 bool = reColorHexadecimal.REGEXP_SHORTHAND.test( '000' ); 89 // returns true 90 ``` 91 92 #### reColorHexadecimal.REGEXP_EITHER 93 94 [Regular expression][mdn-regexp] to match **either** a shorthand or a full length hexadecimal color. 95 96 ```javascript 97 var bool = reColorHexadecimal.REGEXP_EITHER.test( 'ffffff' ); 98 // returns true 99 100 bool = reColorHexadecimal.REGEXP_EITHER.test( '000' ); 101 // returns true 102 ``` 103 104 </section> 105 106 <!-- /.usage --> 107 108 <section class="examples"> 109 110 ## Examples 111 112 <!-- eslint no-undef: "error" --> 113 114 ```javascript 115 var isString = require( '@stdlib/assert/is-string' ).isPrimitive; 116 var reColorHexadecimal = require( '@stdlib/regexp/color-hexadecimal' ); 117 118 function isHexColor( value, mode ) { 119 if ( !isString( value ) ) { 120 return false; 121 } 122 if ( mode === 'shorthand' ) { 123 return reColorHexadecimal.REGEXP_SHORTHAND.test( value ); 124 } 125 if ( mode === 'either' ) { 126 return reColorHexadecimal.REGEXP_EITHER.test( value ); 127 } 128 return reColorHexadecimal.REGEXP.test( value ); 129 } 130 131 var bool = isHexColor( 'ffffff', 'full' ); 132 // returns true 133 134 bool = isHexColor( '474747', 'either' ); 135 // returns true 136 137 bool = isHexColor( '0A5BBE', 'shorthand' ); 138 // returns false 139 140 bool = isHexColor( '000', 'full' ); 141 // returns false 142 143 bool = isHexColor( '000', 'either' ); 144 // returns true 145 146 bool = isHexColor( 'F7b', 'shorthand' ); 147 // returns true 148 149 bool = isHexColor( 'abcd', 'either' ); 150 // returns false 151 152 bool = isHexColor( '', 'either' ); 153 // returns false 154 155 bool = isHexColor( null, 'either' ); 156 // returns false 157 ``` 158 159 </section> 160 161 <!-- /.examples --> 162 163 <section class="links"> 164 165 [mdn-regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions 166 167 </section> 168 169 <!-- /.links -->