Skip to content

Commit

Permalink
Inline color: Support for (semi-)transparent colors and minor improve…
Browse files Browse the repository at this point in the history
…ments (#2223)

- This changes the inline preview to include a background image, so (semi-)transparent colors can be identified as such instead of blending with the background color of the current theme.
- The black outline is now more transparent to better blend in with light themes.
- The `transparent` color was added to CSS Extras.
- This fixes the `parseHexColor` function.
  • Loading branch information
RunDevelopment authored Mar 10, 2020
1 parent bf4f7bf commit 8d2c5a3
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 17 deletions.
2 changes: 1 addition & 1 deletion components/prism-css-extras.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
alias: 'color'
},
'color': [
/\b(?:AliceBlue|AntiqueWhite|Aqua|Aquamarine|Azure|Beige|Bisque|Black|BlanchedAlmond|Blue|BlueViolet|Brown|BurlyWood|CadetBlue|Chartreuse|Chocolate|Coral|CornflowerBlue|Cornsilk|Crimson|Cyan|DarkBlue|DarkCyan|DarkGoldenRod|DarkGr[ae]y|DarkGreen|DarkKhaki|DarkMagenta|DarkOliveGreen|DarkOrange|DarkOrchid|DarkRed|DarkSalmon|DarkSeaGreen|DarkSlateBlue|DarkSlateGr[ae]y|DarkTurquoise|DarkViolet|DeepPink|DeepSkyBlue|DimGr[ae]y|DodgerBlue|FireBrick|FloralWhite|ForestGreen|Fuchsia|Gainsboro|GhostWhite|Gold|GoldenRod|Gr[ae]y|Green|GreenYellow|HoneyDew|HotPink|IndianRed|Indigo|Ivory|Khaki|Lavender|LavenderBlush|LawnGreen|LemonChiffon|LightBlue|LightCoral|LightCyan|LightGoldenRodYellow|LightGr[ae]y|LightGreen|LightPink|LightSalmon|LightSeaGreen|LightSkyBlue|LightSlateGr[ae]y|LightSteelBlue|LightYellow|Lime|LimeGreen|Linen|Magenta|Maroon|MediumAquaMarine|MediumBlue|MediumOrchid|MediumPurple|MediumSeaGreen|MediumSlateBlue|MediumSpringGreen|MediumTurquoise|MediumVioletRed|MidnightBlue|MintCream|MistyRose|Moccasin|NavajoWhite|Navy|OldLace|Olive|OliveDrab|Orange|OrangeRed|Orchid|PaleGoldenRod|PaleGreen|PaleTurquoise|PaleVioletRed|PapayaWhip|PeachPuff|Peru|Pink|Plum|PowderBlue|Purple|Red|RosyBrown|RoyalBlue|SaddleBrown|Salmon|SandyBrown|SeaGreen|SeaShell|Sienna|Silver|SkyBlue|SlateBlue|SlateGr[ae]y|Snow|SpringGreen|SteelBlue|Tan|Teal|Thistle|Tomato|Turquoise|Violet|Wheat|White|WhiteSmoke|Yellow|YellowGreen)\b/i,
/\b(?:AliceBlue|AntiqueWhite|Aqua|Aquamarine|Azure|Beige|Bisque|Black|BlanchedAlmond|Blue|BlueViolet|Brown|BurlyWood|CadetBlue|Chartreuse|Chocolate|Coral|CornflowerBlue|Cornsilk|Crimson|Cyan|DarkBlue|DarkCyan|DarkGoldenRod|DarkGr[ae]y|DarkGreen|DarkKhaki|DarkMagenta|DarkOliveGreen|DarkOrange|DarkOrchid|DarkRed|DarkSalmon|DarkSeaGreen|DarkSlateBlue|DarkSlateGr[ae]y|DarkTurquoise|DarkViolet|DeepPink|DeepSkyBlue|DimGr[ae]y|DodgerBlue|FireBrick|FloralWhite|ForestGreen|Fuchsia|Gainsboro|GhostWhite|Gold|GoldenRod|Gr[ae]y|Green|GreenYellow|HoneyDew|HotPink|IndianRed|Indigo|Ivory|Khaki|Lavender|LavenderBlush|LawnGreen|LemonChiffon|LightBlue|LightCoral|LightCyan|LightGoldenRodYellow|LightGr[ae]y|LightGreen|LightPink|LightSalmon|LightSeaGreen|LightSkyBlue|LightSlateGr[ae]y|LightSteelBlue|LightYellow|Lime|LimeGreen|Linen|Magenta|Maroon|MediumAquaMarine|MediumBlue|MediumOrchid|MediumPurple|MediumSeaGreen|MediumSlateBlue|MediumSpringGreen|MediumTurquoise|MediumVioletRed|MidnightBlue|MintCream|MistyRose|Moccasin|NavajoWhite|Navy|OldLace|Olive|OliveDrab|Orange|OrangeRed|Orchid|PaleGoldenRod|PaleGreen|PaleTurquoise|PaleVioletRed|PapayaWhip|PeachPuff|Peru|Pink|Plum|PowderBlue|Purple|Red|RosyBrown|RoyalBlue|SaddleBrown|Salmon|SandyBrown|SeaGreen|SeaShell|Sienna|Silver|SkyBlue|SlateBlue|SlateGr[ae]y|Snow|SpringGreen|SteelBlue|Tan|Teal|Thistle|Tomato|Transparent|Turquoise|Violet|Wheat|White|WhiteSmoke|Yellow|YellowGreen)\b/i,
{
pattern: /\b(?:rgb|hsl)\(\s*\d{1,3}\s*,\s*\d{1,3}%?\s*,\s*\d{1,3}%?\s*\)\B|\b(?:rgb|hsl)a\(\s*\d{1,3}\s*,\s*\d{1,3}%?\s*,\s*\d{1,3}%?\s*,\s*(?:0|0?\.\d+|1)\s*\)\B/i,
inside: {
Expand Down
2 changes: 1 addition & 1 deletion components/prism-css-extras.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion plugins/inline-color/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ <h2>CSS</h2>
}

span.bar {
background: rgba(105, 0, 12, .62);
background: rgba(105, 0, 12, .38);
color: hsl(30, 100%, 50%);
border-color: transparent;
}</code></pre>

<pre data-src="themes/prism.css"></pre>
Expand Down
28 changes: 26 additions & 2 deletions plugins/inline-color/prism-inline-color.css
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
span.inline-color {
span.inline-color-wrapper {
/*
* The background image is the following SVG inline in base 64:
*
* <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 2 2">
* <path fill="gray" d="M0 0h2v2H0z"/>
* <path fill="white" d="M0 0h1v1H0zM1 1h1v1H1z"/>
* </svg>
*
* SVG-inlining explained:
* https://stackoverflow.com/a/21626701/7595472
*/
background: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyIDIiPjxwYXRoIGZpbGw9ImdyYXkiIGQ9Ik0wIDBoMnYySDB6Ii8+PHBhdGggZmlsbD0id2hpdGUiIGQ9Ik0wIDBoMXYxSDB6TTEgMWgxdjFIMXoiLz48L3N2Zz4=");
/* This is to prevent visual glitches where one pixel from the repeating pattern could be seen. */
background-position: center;
background-size: 110%;

display: inline-block;
height: 1.333ch;
width: 1.333ch;
margin: 0 .333ch;
box-sizing: border-box;
border: 1px solid white;
outline: 1px solid black;
outline: 1px solid rgba(0,0,0,.5);
overflow: hidden;
}

span.inline-color {
display: block;
/* To prevent visual glitches again */
height: 120%;
width: 120%;
}
39 changes: 28 additions & 11 deletions plugins/inline-color/prism-inline-color.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@
// Copied from the markup language definition
var HTML_TAG = /<\/?(?!\d)[^\s>\/=$<%]+(?:\s(?:\s*[^\s>\/=]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+(?=[\s>]))|(?=[\s/>])))+)?\s*\/?>/g;

// the regex explained: In first lookahead we check whether the string is valid and then we use the
// capturing groups to split the string into its components.
var HEX_COLOR = /^#?(?=(?:[\da-f]{1,2}){3,4}$)([\da-f][\da-f]?)([\da-f][\da-f]?)([\da-f][\da-f]?)([\da-f][\da-f]?)?$/i;
// a regex to validate hexadecimal colors
var HEX_COLOR = /^#?((?:[\da-f]){3,4}|(?:[\da-f]{2}){3,4})$/i;

/**
* Parses the given hexadecimal representation and returns the parsed RGBA color.
*
* If the format of the given string is invalid, `undefined` will be returned.
* Valid formats are: `RGB`, `RGBA`, `RRGGBB`, and `RRGGBBAA`.
*
* Hexadecimal colors are parsed because they are not fully supported by older browsers, so converting them to
* `rgba` functions improves browser compatibility.
*
* @param {string} hex
* @returns {string | undefined}
*/
Expand All @@ -25,15 +27,30 @@
if (!match) {
return undefined;
}
hex = match[1]; // removes the leading "#"

// This is used to scale normalize 4bit and 8bit values
var scale = hex.length <= 4 ? 1 / 15 : 1 / 255;
// the width and number of channels
var channelWidth = hex.length >= 6 ? 2 : 1;
var channelCount = hex.length / channelWidth;

var rgb = match.slice(1, 4).map(function (c) {
return String(Math.round(parseInt(c, 16) * scale * 255));
}).join(',');
// the scale used to normalize 4bit and 8bit values
var scale = channelWidth == 1 ? 1 / 15 : 1 / 255;

// normalized RGBA channels
var channels = [];
for (var i = 0; i < channelCount; i++) {
var int = parseInt(hex.substr(i * channelWidth, channelWidth), 16);
channels.push(int * scale);
}
if (channelCount == 3) {
channels.push(1); // add alpha of 100%
}

var alpha = match[4] === undefined ? '1' : (parseInt(match[4], 16) * scale).toFixed(3);
// output
var rgb = channels.slice(0, 3).map(function (x) {
return String(Math.round(x * 255));
}).join(',');
var alpha = String(Number(channels[3].toFixed(3))); // easy way to round 3 decimal places

return 'rgba(' + rgb + ',' + alpha + ')';
}
Expand Down Expand Up @@ -65,7 +82,7 @@


Prism.hooks.add('wrap', function (env) {
if (env.type === 'color' || env.type === 'hexcode') {
if (env.type === 'color' || env.classes.indexOf('color') >= 0) {
var content = env.content;

// remove all HTML tags inside
Expand All @@ -80,7 +97,7 @@
return;
}

var previewElement = '<span class="inline-color" style="background-color:' + color + ';"></span>';
var previewElement = '<span class="inline-color-wrapper"><span class="inline-color" style="background-color:' + color + ';"></span></span>';
env.content = previewElement + content;
}
});
Expand Down
2 changes: 1 addition & 1 deletion plugins/inline-color/prism-inline-color.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8d2c5a3

Please sign in to comment.