Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for hexadecimal int literals #9

Merged
merged 3 commits into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@
"default": "hex",
"enum": [
"hex",
"hex int literal",
"rgb",
"hsl",
"hwb"
],
"enumDescriptions": [
"For example: #0000FF",
"For example: 0x0000FF",
"For example: rgb(0, 0, 255)",
"For example: hsl(240, 100%, 50%)",
"For example: hwb(240, 0%, 0%)"
Expand Down
53 changes: 35 additions & 18 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,26 @@ function runScript(script: string): Promise<any> {
});
}

function toHexEnhanced(color: colorString.Color) {
const config = vscode.workspace.getConfiguration('macColorPicker');

let result = colorString.to.hex(color);

if (config.get('lowercaseHexColors')) {
result = result.toLowerCase();
}

if (config.get('shortHexColors')) {
result = shortenHex(result);
}

return result;
function toHexEnhanced(intLiteral: boolean) {
return (color: colorString.Color) => {
const config = vscode.workspace.getConfiguration('macColorPicker');

let result = colorString.to.hex(color);

if (config.get('lowercaseHexColors')) {
result = result.toLowerCase();
}

if (config.get('shortHexColors')) {
result = shortenHex(result);
}

if (intLiteral) {
result = '0x' + result.slice(1);
}

return result;
};
}

function getDefaultColorModel(): ColorModel {
Expand All @@ -54,7 +60,7 @@ function getDefaultColorModel(): ColorModel {
return 'hsl';
case 'hwb':
return 'hwb';
default: // hex, rgb -> rgb
default: // hex, 'hex int literal', rgb -> rgb
return 'rgb';
}
}
Expand All @@ -69,8 +75,12 @@ function getDefaultOutputFunction(): Function {
return colorString.to.hsl;
case 'hwb':
return colorString.to.hwb;

case 'hex int literal': //'hex int literal' -> rgb
return toHexEnhanced(true);

default: // hex -> rgb
return toHexEnhanced;
return toHexEnhanced(false);
}
}

Expand Down Expand Up @@ -109,7 +119,14 @@ export function activate(context: vscode.ExtensionContext) {
// Loop over both selections to see if one of them contains a color recognizable by colorString
for (const selection of selections) {
if (selection !== undefined) {
const selectedText = editor.document.getText(selection).trim();
let selectedText = editor.document.getText(selection).trim();

const isIntLiteral = selectedText.startsWith('0x');

if (isIntLiteral) {
selectedText = '#' + selectedText.slice(2);
}

const selectedColorDescriptor = colorString.get(selectedText);

if (selectedColorDescriptor === null) {
Expand All @@ -134,7 +151,7 @@ export function activate(context: vscode.ExtensionContext) {
if (selectedText.startsWith('rgb')) {
outputFunction = colorString.to.rgb;
} else {
outputFunction = toHexEnhanced;
outputFunction = toHexEnhanced(isIntLiteral);
}
} else {
outputFunction = colorString.to[selectedColorModel];
Expand Down
1 change: 1 addition & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const color = 0xff0000;