Skip to content

Commit cba4898

Browse files
committed
3D color dependent on the theme
1 parent f6b617b commit cba4898

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

packages/jupytercad-app/style/base.css

+2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
body[data-jp-theme-light='false'] {
2+
--jp-brand-color0: #0fa8b0 !important;
23
--jp-brand-color1: #0fa8b0 !important;
34
--jp-inverse-layout-color3: #0fa8b0 !important;
45
--jp-accept-color-normal: #0fa8b0 !important;
56
--jc-brand-color: #032122 !important;
67
}
78

89
body[data-jp-theme-light='true'] {
10+
--jp-brand-color0: #09777c !important;
911
--jp-brand-color1: #09777c !important;
1012
--jp-inverse-layout-color3: #09777c !important;
1113
--jp-accept-color-normal: #09777c !important;

packages/jupytercad-extension/src/mainview.tsx

+13-11
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,18 @@ import {
3131
WorkerAction
3232
} from './types';
3333
import { FloatingAnnotation } from './annotation/view';
34-
import { throttle } from './tools';
34+
import { getCSSVariableColor, throttle } from './tools';
3535
import { Vector2 } from 'three';
3636

3737
// Apply the BVH extension
3838
THREE.BufferGeometry.prototype.computeBoundsTree = computeBoundsTree;
3939
THREE.BufferGeometry.prototype.disposeBoundsTree = disposeBoundsTree;
4040
THREE.Mesh.prototype.raycast = acceleratedRaycast;
4141

42-
const DEFAULT_MESH_COLOR = new THREE.Color('#434442');
43-
const SELECTED_MESH_COLOR = new THREE.Color('#AB5118');
42+
const DEFAULT_MESH_COLOR = () => new THREE.Color(getCSSVariableColor('--jp-inverse-layout-color4'));
43+
const DEFAULT_EDGE_COLOR = () => new THREE.Color(getCSSVariableColor('--jp-inverse-layout-color2'));
44+
const SELECTED_MESH_COLOR = () => new THREE.Color(getCSSVariableColor('--jp-brand-color0'));
45+
4446

4547
export type BasicMesh = THREE.Mesh<
4648
THREE.BufferGeometry,
@@ -491,7 +493,7 @@ export class MainView extends React.Component<IProps, IStates> {
491493
if (selection) {
492494
// Deselect old selection
493495
if (this._selectedMesh) {
494-
let originalColor = DEFAULT_MESH_COLOR;
496+
let originalColor = DEFAULT_MESH_COLOR();
495497
if (
496498
guidata &&
497499
guidata[this._selectedMesh.name] &&
@@ -517,7 +519,7 @@ export class MainView extends React.Component<IProps, IStates> {
517519
}
518520

519521
if (this._selectedMesh) {
520-
this._selectedMesh.material.color = SELECTED_MESH_COLOR;
522+
this._selectedMesh.material.color = SELECTED_MESH_COLOR();
521523
this._model.syncSelectedObject(this._selectedMesh.name, this.state.id);
522524
} else {
523525
this._model.syncSelectedObject(undefined, this.state.id);
@@ -568,7 +570,7 @@ export class MainView extends React.Component<IProps, IStates> {
568570

569571
const objdata = guidata ? guidata?.[objName] : null;
570572

571-
let color = DEFAULT_MESH_COLOR;
573+
let color = DEFAULT_MESH_COLOR();
572574
let visible = true;
573575
if (objdata) {
574576
if (Object.prototype.hasOwnProperty.call(objdata, 'color')) {
@@ -618,12 +620,12 @@ export class MainView extends React.Component<IProps, IStates> {
618620

619621
if (this._selectedMesh?.name === objName) {
620622
this._selectedMesh = mesh;
621-
mesh.material.color = SELECTED_MESH_COLOR;
623+
mesh.material.color = SELECTED_MESH_COLOR();
622624
}
623625

624626
const edgeMaterial = new THREE.LineBasicMaterial({
625627
linewidth: 5,
626-
color: 'black'
628+
color: DEFAULT_EDGE_COLOR()
627629
});
628630
edgeList.forEach(edge => {
629631
const edgeVertices = new THREE.Float32BufferAttribute(
@@ -732,7 +734,7 @@ export class MainView extends React.Component<IProps, IStates> {
732734

733735
if (selected) {
734736
this._selectedMesh = selected as BasicMesh;
735-
this._selectedMesh.material.color = SELECTED_MESH_COLOR;
737+
this._selectedMesh.material.color = SELECTED_MESH_COLOR();
736738
}
737739
}
738740

@@ -741,7 +743,7 @@ export class MainView extends React.Component<IProps, IStates> {
741743
return;
742744
}
743745

744-
let originalColor = DEFAULT_MESH_COLOR;
746+
let originalColor = DEFAULT_MESH_COLOR();
745747
const guidata = this._model.sharedModel.getOption('guidata');
746748
if (
747749
guidata &&
@@ -986,7 +988,7 @@ export class MainView extends React.Component<IProps, IStates> {
986988

987989
// Draw lines
988990
const material = new THREE.LineBasicMaterial({
989-
color: 'black',
991+
color: DEFAULT_EDGE_COLOR(),
990992
linewidth: 2
991993
});
992994
const geometry = new THREE.BufferGeometry().setFromPoints([

packages/jupytercad-extension/src/tools.ts

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const d3Color: any = require('d3-color');
2+
13
import { LabIcon } from '@jupyterlab/ui-components';
24

35
import jvControlLight from '../style/icon/jvcontrol.svg';
@@ -196,3 +198,9 @@ export function nearest(n: number, tol: number): number {
196198
return n;
197199
}
198200
}
201+
202+
export function getCSSVariableColor(name: string): string {
203+
const color = window.getComputedStyle(document.body).getPropertyValue(name) || '#ffffff';
204+
205+
return d3Color.rgb(color).formatHex();
206+
}

0 commit comments

Comments
 (0)