Skip to content

Commit

Permalink
vscode support
Browse files Browse the repository at this point in the history
  • Loading branch information
gywn committed Nov 26, 2017
1 parent cc80a1e commit fe991ce
Show file tree
Hide file tree
Showing 16 changed files with 692 additions and 44 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
*.dat
*.dll
*.dylib
*.egg-info/
*.ipynb
*.o
*.so
.vscode/
build/
build/
dist/
15 changes: 7 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ set(LIB_SOURCES src/colorspace.cpp src/CIEDE2000.cpp src/fitness.cpp)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
find_package(PythonLibsNew REQUIRED) # -DPYTHON_EXECUTABLE should be set

if(NOT CMAKE_BUILD_TYPE MATCHES "Debug")

add_library(perception MODULE ${LIB_SOURCES} src/python.cpp)
target_link_libraries(perception ${PYTHON_LIBRARIES})
target_include_directories(perception PRIVATE include ${PYTHON_INCLUDE_DIR})
set_target_properties(perception PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}")
set_target_properties(perception PROPERTIES SUFFIX "${PYTHON_MODULE_EXTENSION}")
add_library(native MODULE ${LIB_SOURCES} src/python.cpp)
target_link_libraries(native ${PYTHON_LIBRARIES})
target_include_directories(native PRIVATE include ${PYTHON_INCLUDE_DIR})
set_target_properties(native PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}")
set_target_properties(native PROPERTIES SUFFIX "${PYTHON_MODULE_EXTENSION}")

else(NOT CMAKE_BUILD_TYPE MATCHES "Debug")
if(CMAKE_BUILD_TYPE MATCHES "Debug")

add_library(perception SHARED ${LIB_SOURCES})
target_include_directories(perception PUBLIC include)
Expand All @@ -42,4 +41,4 @@ else(NOT CMAKE_BUILD_TYPE MATCHES "Debug")
add_executable(testfitness2 test/testfitness2.cpp)
target_link_libraries(testfitness2 perception)

endif(NOT CMAKE_BUILD_TYPE MATCHES "Debug")
endif(CMAKE_BUILD_TYPE MATCHES "Debug")
9 changes: 9 additions & 0 deletions include/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ struct RGB {
};
using RGB = struct RGB;

struct CMY {
double c;
double m;
double y;
};
using CMY = struct CMY;

/**
* @brief
* Obtain Delta-E 2000 value.
Expand All @@ -58,9 +65,11 @@ constexpr double rad2Deg(const double rad);
RGB XYZtoRGB(const XYZ &xyz);
XYZ LABtoXYZ(const LAB &lab);
RGB LABtoRGB(const LAB &lab);
CMY RGBtoCMY(const RGB &rgb);

} // namespace color

std::ostream &operator<<(std::ostream &s, const color::LAB &lab);
std::ostream &operator<<(std::ostream &s, const color::XYZ &xyz);
std::ostream &operator<<(std::ostream &s, const color::RGB &rgb);
std::ostream &operator<<(std::ostream &s, const color::CMY &rgb);
18 changes: 18 additions & 0 deletions include/constant.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,22 @@ const double A23 = -0.03294208;
const double A31 = -0.03269638;
const double A32 = -0.18259425;
const double A33 = 1.4716528;

static const double r2 = 0.2; // cyan's absorbance of green
static const double r3 = 0.1; // cyan's absorbance of blue
static const double g1 = 0.0; // magenta's absorbance of red
static const double g3 = 0.1; // magenta's absorbance of blue
static const double b1 = 0; // yellow's absorbance of red
static const double b2 = 0.1; // yellow's absorbance of green

/* (1 - RGB) -> CMY */
const double B11 = 1 - b2 * g3;
const double B12 = -g1 + b1 * g3;
const double B13 = -b1 + b2 * g1;
const double B21 = -r2 + b2 * r3;
const double B22 = 1 - b1 * r3;
const double B23 = -b2 + b1 * r2;
const double B31 = g3 * r2 - r3;
const double B32 = -g3 + g1 * r3;
const double B33 = 1 - g1 * r2;
} // namespace color
12 changes: 9 additions & 3 deletions include/fitness.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,23 @@ inline double offRange(double x, double a, double b);

double offRGB(const color::LAB &lab);

double offChroma(const color::LAB &lab, double C);

/**
* @param lab a vector of color::LAB, inter-distances of lab[freeM:] are ignored
* @param M 0 < M <= lab.size(), set to lab.size() if 0
* @param maxC maximal chroma, < 0 to be ignored
* @return a lexicographical product of distances
*/
LexiProduct<double> fitnessFunc(const std::vector<color::LAB> &lab,
size_t M = 0);
size_t M = 0, double maxC = -1.);

class PerceptionResult {
public:
unsigned long flags;
double L;
double maxC;
std::vector<color::LAB> lab;
std::vector<color::RGB> rgb;
LexiProduct<double> fitness;
};
Expand All @@ -29,11 +34,12 @@ std::ostream &operator<<(std::ostream &os, const PerceptionResult &res);

/*
* @param M numbers of free colors
* @param L luminocity, < 0 to be ignored
* @param L luminocity constraint, < 0 to be ignored
* @param maxC maximal chroma, < 0 to be ignored
* @param fixed vector of fixed colors, optional
* @param quiet don't write info to stdout
* @return PerceptionResult
*/
PerceptionResult perception(size_t M, double L = -1,
PerceptionResult perception(size_t M, double L = -1, double maxC = -1,
std::vector<color::LAB> const *fixed = nullptr,
bool quiet = false);
156 changes: 156 additions & 0 deletions python/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
from argparse import ArgumentParser
import getpass
import os
from .native import perception, LABtoRGB
from .version import __version__
from .pystache_tree import recursive_render


def hex(x):
return '{:02x}'.format(min(round(256 * max(0, x)), 255)).upper()


def rgb_hex(rgb):
return ''.join([hex(x) for x in rgb])


def nearest(rgbs, pivot):
new = sorted(
rgbs,
key=
lambda rgb: (rgb[0] - pivot[0])**2 + (rgb[1] - pivot[1])**2 + (rgb[2] - pivot[2])**2
)
return new[0]


def argparser():
ps = ArgumentParser()

ps.add_argument(
'-n',
'--name',
type=str,
default='default',
help='theme name, default: \'default\'')
ps.add_argument(
'-f',
'--foreground',
type=float,
default=-1,
help='foreground luminocity between 0 and 100')
ps.add_argument(
'-b',
'--background',
type=float,
default=-1,
help='background luminocity between 0 and 100')
ps.add_argument(
'-L',
type=float,
default=-1,
help='main palette luminocity between 0 and 100')
ps.add_argument(
'--maxC',
type=float,
default=-1,
help='maximal chroma for main-palette, negative value for unconstrainted')
ps.add_argument(
'--sub-maxC',
type=float,
default=20,
help='maximal chroma for sub-palette, negative value for unconstrainted')
ps.add_argument(
'--mu',
type=float,
default=1.5,
help='mu')
ps.add_argument('-D', '--debug', action='store_true', default=False)

return ps


def main():
args = argparser().parse_args()
if args.background < 0:
args.background = 10
if args.foreground < 0:
args.foreground = 100 - args.background
# if args.background < 50:
# args.foreground = min(100, args.background + 90)
# else:
# args.foreground = max(0, args.background - 90)
sgn = 1 if args.foreground > args.background else -1
if args.L < 0:
if args.foreground > 50:
args.L = (args.foreground * 2 + 50) / (2 + 1)
else:
args.L = (args.foreground * 0.1 + 50) / (0.1 + 1)
if args.maxC < 0:
pass
# x = args.L - args.background
# x1 = -50
# y1 = -70
# x2 = 80
# y2 = 50
# args.maxC = (x * (y1 - y2) - x2 * y1 + x1 * y2) / (x1 - x2)
if (args.debug):
from pprint import pprint
pprint(args)
L_main = args.L
L_sub = args.background + sgn * 5

palette_main = perception(
7,
L=L_main,
maxC=args.maxC,
fixed=[(L_main, 0, 0)],
quiet=not args.debug)
palette_sub = perception(
7,
L=L_sub,
maxC=args.sub_maxC,
fixed=[(L_sub, 0, 0)],
quiet=not args.debug)
context = {
'name': args.name,
'version': __version__,
'user': getpass.getuser(),
'ui-theme': 'vs-dark' if args.background < 50 else 'vs'
}

# legacy
for i, rgb in enumerate(palette_main['rgb']):
context[f'base{i + 8:02X}-hex'] = rgb_hex(rgb)
for i in range(7):
context[f'base{i:02X}-hex'] = rgb_hex(
LABtoRGB((args.background + i * sgn * 5, 0, 0)))
context['base07-hex'] = rgb_hex(LABtoRGB((args.foreground, 0, 0)))

context['fg-hex'] = rgb_hex(LABtoRGB((args.foreground, 0, 0)))
# context['bg-hex'] = rgb_hex(LABtoRGB((args.background, 0, 0)))
for i, delta in enumerate([0, 5, 10]):
context[f'bg-{i}-hex'] = rgb_hex(
LABtoRGB((args.background + sgn * delta, 0, 0)))
for i, delta in enumerate([15, 25, 50]):
context[f'line-{i}-hex'] = rgb_hex(
LABtoRGB((args.background + sgn * delta, 0, 0)))
for i, rgb in enumerate(palette_main['rgb']):
context[f'main-{i}-hex'] = rgb_hex(rgb)
for i, rgb in enumerate(palette_sub['rgb']):
context[f'sub-{i}-hex'] = rgb_hex(rgb)
for name, rgb in [('red-hex', (1, 0, 0)), ('green-hex', (0, 0.5, 0)),
('blue-hex', (0, 0, 1)), ('yellow-hex', (0.5, 0.5, 0))]:
context['main-' + name] = rgb_hex(nearest(palette_main['rgb'], rgb))
context['sub-' + name] = rgb_hex(nearest(palette_sub['rgb'], rgb))

if (args.debug):
from pprint import pprint
palette_main["fitness"] = palette_main["fitness"][0];
palette_sub["fitness"] = palette_sub["fitness"][0];
pprint(palette_main)
pprint(palette_sub)
pprint(context)

recursive_render(
os.path.join(os.path.dirname(__file__), 'data/vscode'),
'/Users/kinker/.vscode/extensions', context)
17 changes: 17 additions & 0 deletions python/data/vscode/perception-theme-{{name}}/package.json.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "perception-theme-{{name}}",
"version": "{{version}}",
"engines": {
"vscode": "^0.9.0"
},
"publisher": "{{user}}",
"contributes": {
"themes": [
{
"label": "Perception Colors Theme {{name}}",
"uiTheme": "{{ui-theme}}",
"path": "./themes/perception-theme.json"
}
]
}
}
Loading

0 comments on commit fe991ce

Please sign in to comment.