Template:Xconv

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Template documentationview · edit · history · purge ]
This documentation is transcluded from Template:Xconv/doc.

This template can perform different conversions ⇄ between decimal and hexadecimal values.

Four main conversions can be performed:


decimal to hexadecimal number


hexadecimal to decimal number


hexadecimal to decimal color triplet


decimal to hexadecimal color triplet


💚 It is quite easy to use but all the options require detailled documentation.

Usage

{{Xconv}}

The parameter(s) for the template depend on the required function.

The template takes no parameters.

Additional information

The template is intended to be used in the following namespaces: no namespace specified

The template is intended to be used by the following user groups: no user group specified

See also

Localization

This template is not intended to be localized.

Input format and flags

The template analizes and checks the input, and tries to determine which of the actions is required. Hexadecimal input will be converted to decimal.
When the input is decimal, it is assumed that conversion to hexadecimal is wanted; by setting a flag it can be declared to be hexadecimal notation.
This flag, the 2nd parameter, describes the input format, it can be either n or h (for 'num' or 'hex').
When color code conversion occurs, with a flag o a formatted output can be required; combination with the flag for the input format is possible.
The 3rd parameter needs to be unused (or empty), except when it is used for a value (③ⓒ or ④ⓐ).
Input separator: the character ' is always ignored, it can be used at any position for grouping parts of the input string, e.g.
{{Xconv|111'222'333}}= 6A11E3Dh, {{Xconv|111''222'333|h}}= 4582417203d, {{Xconv|#'FC'DD'09}}= rgb(252,221,9)

Conversion of numbers

① Conversion decimal to hexadecimal

Parameter Type Description Default Status
1 string unsigned integer required
2 flag character n (flag to distinguish from hex numbers) empty optional
3 not used/empty empty required
others ignored empty deprecated

The flag n may symbolize that the numeric input is interpreted as a decimal number - this is the default.

Example: {{Xconv|16}} gives 10h

② Conversion hexadecimal to decimal

Parameter Type Description Default Status
1 string hexadecimal number required
2 flag character h (flag to distinguish from integers) empty optional
3 not used/empty empty required
others ignored empty deprecated

The flag h can ensure that a numeric input is interpreted as a hexadecimal number - not necessary when hex digits a...f are contained

Example: {{Xconv|10|h}} gives 16d


Conversion of color codes

③ Conversion #rgb to three decimal numbers

Parameter Type Description Default Status
1 string string: first character #, followed by 3 or 6 hexadecimal characters required
2 flag character o to get an edited output, or % to get the rgb% notation empty optional
3 not used/empty empty required
others ignored empty deprecated
③ⓑ another input possibility:
a string of three hexadecimal numbers, each one between 0 and FF, separated by either space, comma, minus, slash, or the combination comma and space
Care should be taken: single digits 1 ... F stand for 01 ... 0F, they are not doubled as with ③ⓐ !
With the flag o a formatted output is required; also allowed is ho, h; or % for rgb% output cut to 3 decimal fractions
With the flag r the prefix "rgb" precedes the brackets of the three colors, otherwise it doesn't.
The flag code characters ( h, o, r, % ) can be specified in any order.

Examples:
3a {{Xconv|#123}} gives (17,34,51), or {{Xconv|#123|%}} gives (6.666%,13.333%,20%)
3b {{Xconv|1,2,11|h}} gives (1,2,17), or {{Xconv|1/2/11|hr}} gives rgb(1,2,17)
3b {{Xconv|1-2-22|ho}} gives (1,2,34), or {{Xconv|1, 2, 22|hor}} gives rgb(1,2,34)
3b {{Xconv|1,2,11|%}} gives (0.392%,0.784%,6.666%), or {{Xconv|1,2,11|r%}} gives rgb(0.392%,0.784%,6.666%)

③ⓒ also allowed is:
three parameters like ④ⓐ, only when the hexadecimal format can be recognized by (at least one) character A...F;
this input format is not encouraged, therefore no flag is provided to determine between decimal and hexadecimal input.

Example:
3c {{Xconv|11|22|b}} gives (17,34,11)

④ Conversion of decimal color triplet to #rgb

④ⓐ with three parameters:
Parameter Type Description Default Status
1 string integer number between 0 and 255, for red required
2 string integer number between 0 and 255, for green required
3 string integer number between 0 and 255, for blue required
others ignored empty deprecated
④ⓑ with one parameter:
Parameter Type Description Default Status
1 string ④ⓑ string of three integer numbers between 0 and 255 for r/g/b,
separated by either single characters: space, comma, minus or slash, or the combination comma and space

④ⓒ or the rgb notation: three such numbers within brackets and separated by commata , or ,  ; may be preceded by the string rgb


④ⓓ or the rgb % notation: three (rational) numbers, each one between 0 and 100, and followed by either %, %, or %, , which will be converted to (rounded) output values between 00 and FF

required
2 flag character o to get an edited output, or s to shorten output (if possible) empty optional
3 not used/empty empty required
others ignored empty deprecated
With the flag o a formatted output is required; also allowed is no
With the flag a the output #rrggbb is shortened to #rbg, if possible

Examples:
4a {{Xconv|1|2|11}} gives #01020B
4b {{Xconv|1,2,11}} gives #01020B
4c {{Xconv|(1,2,11)}} or {{Xconv|rgb(1,2,11)}} gives #01020B
4c {{Xconv|(17,204,34)}} gives #11CC22, {{Xconv|rgb(17,204,34)|s}} gives #1C2
4d {{Xconv|rgb(1%,2%,3%)}} gives #030508


More examples

0 <1 > 1114111 = 10FFFFh

1 <2 > 1114111 | h = 17907985d

2 <2 > 10FFFF = 1114111d

3 <3a> #1a2bFc = (26,43,252)

3 <3a> #48c = (68,136,204)

3 <3a> #010 = (0,17,0)

3 <3a> #00ff00 = (0,255,0)

4 <3b> 1A,2b,Fc = (26,43,252)

5 <3C> 1a|2B|Fc = (26,43,252)

6 <4A> 125|1|2 = #7D0102

7 <4b> 0-87-184 = #0057B8

8 <4c> (31,63,127) = #1F3F7F

8 <4c> RgB(31,63,127|box=y) = #1F3F7F

9 <4d> (20.32%, 66%, 100.0000%) = #34A8FF

A <4d> rgb(40%,60%,80%)|s = #69C

B <4d> (0%,10%,20%) = #001A33

C <4d> rgb(1%,2%,3%) = #030508

D <4d> 40%50%60%|s = #667F99

E <4d> (36.5%72%12.5%) = #5DB820

F <4d> 36.5%72%12.5% = #5DB820


G <4c> 31,63,27 | h = (49,99,39)

H <4c> 32,64,28 | h = (50,100,40)

I <3b> 32,64,28 0n0 = #20401C

J #000102|% → (0%,0.392%,0.784%), #030405|% → (1.176%,1.568%,1.96%), #060708|% → (2.352%,2.745%,3.137%)
J #091011|% → (3.529%,6.274%,6.666%), #121314|% → (7.058%,7.45%,7.843%), #151617|% → (8.235%,8.627%,9.019%)

K (.784%,7.843%,78.431%) → #0214C8 ⇔ #0214C8|% → (0.784%,7.843%,78.431%)

The parameter "box"

It is possible to show input and result of color codes in a color box, e.g. {{Xconv|32 237 0|box=y}} gives  32 237 0 = #20ED00 
or {{Xconv| (100%,80%,26.70%) | box=1}} gives  (100%,80%,26.70%) = #FFCC44 

Parameter errors

The template tries hard to detect the input and tolerates some minor inconsistencies of valid input.
When invalid parameter input is detected it is returned with the text "Error = " and an error number.
Numbers:

  1. character not numeric/hexa (unrecogniced)
  2. '(' or '#' occurs at not first position
  3. separator or separator combination wrong
  4. parm2 input wrong function code (not h,n,o,r,s,%)
  5. decimal point error
  6. % sign error
  7. not three "%" signs
  8. not all decimal color codes are ≤ 255
  9. not all the percent values are ≤ 100%

For example, 140%50%60% gives Error = 9 because value > 100% is wrong