Add preview of hex colors inside code chunk #5908
Replies: 6 comments 13 replies
-
Could you clarify, where you want this exactly? |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Here an updated version. ---
format: html
include-after-body:
- text: |
<script type="text/javascript">
// Author: Mickaël Canouil
// Version: <1.0.0>
// Description: Scan HTML to find hex colour code and add a coloured square next to it.
// License: MIT
function scanElements(element) {
if (element.nodeType === Node.TEXT_NODE) {
const regex = /#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})\b/g;
const newText = element.textContent.replace(
regex,
`$&<span style="display: inline-block; color: $&;">■</span>`
);
if (newText !== element.textContent) {
const newElement = document.createElement('span');
newElement.innerHTML = newText;
element.parentNode.replaceChild(newElement, element);
}
} else if (element.nodeType === Node.ELEMENT_NODE && !element.classList.contains('sourceCode')) {
for (let i = 0; i < element.childNodes.length; i++) {
scanElements(element.childNodes[i]);
}
}
}
scanElements(document.body);
</script>
---
The `#b22222` colour is a dark red colour, also known as firebrick.
It is a web safe colour.
The #b22222 colour hex could be obtained by blending #ff4444 with #640000.
Closest websafe colour is: #cc3333.
```{r}
my_colour <- "#b22222"
``` |
Beta Was this translation helpful? Give feedback.
-
I have been working on a similar project last night! right now it is written in con:
pro:
This is how it interfaces right now, but I would like to move to using shortcodes like
|
Beta Was this translation helpful? Give feedback.
-
Feature request: Copy to clipboard on click (maybe this could be an option inside the extension) |
Beta Was this translation helpful? Give feedback.
-
First release (v0.1.0): https://github.com/mcanouil/quarto-preview-colour. |
Beta Was this translation helpful? Give feedback.
-
Description
As seen in the new version of RStudio (or Github itself), it would be great if we could have a preview of the colors when typing the hex code, like here:
#ffc127
.When sharing plots' code, it's helpful to know what color each one is.
Beta Was this translation helpful? Give feedback.
All reactions