@@ -36,8 +36,75 @@ document.querySelectorAll('.copy-paste-icon').forEach(function(element) {
36
36
const toggleableContent = element . closest ( '.sk-toggleable__content' ) ;
37
37
const paramPrefix = toggleableContent ? toggleableContent . dataset . paramPrefix : '' ;
38
38
const paramName = element . parentElement . nextElementSibling
39
- . textContent . trim ( ) . split ( ' ' ) [ 0 ] ;
39
+ . textContent . trim ( ) . split ( ' ' ) [ 0 ] ;
40
40
const fullParamName = paramPrefix ? `${ paramPrefix } ${ paramName } ` : paramName ;
41
41
42
42
element . setAttribute ( 'title' , fullParamName ) ;
43
43
} ) ;
44
+
45
+
46
+ /**
47
+ * Adapted from Skrub
48
+ * https://github.com/skrub-data/skrub/blob/403466d1d5d4dc76a7ef569b3f8228db59a31dc3/skrub/_reporting/_data/templates/report.js#L789
49
+ * @returns "light" or "dark"
50
+ */
51
+ function detectTheme ( element ) {
52
+ const body = document . querySelector ( 'body' ) ;
53
+
54
+ // Check VSCode theme
55
+ const themeKindAttr = body . getAttribute ( 'data-vscode-theme-kind' ) ;
56
+ const themeNameAttr = body . getAttribute ( 'data-vscode-theme-name' ) ;
57
+
58
+ if ( themeKindAttr && themeNameAttr ) {
59
+ const themeKind = themeKindAttr . toLowerCase ( ) ;
60
+ const themeName = themeNameAttr . toLowerCase ( ) ;
61
+
62
+ if ( themeKind . includes ( "dark" ) || themeName . includes ( "dark" ) ) {
63
+ return "dark" ;
64
+ }
65
+ if ( themeKind . includes ( "light" ) || themeName . includes ( "light" ) ) {
66
+ return "light" ;
67
+ }
68
+ }
69
+
70
+ // Check Jupyter theme
71
+ if ( body . getAttribute ( 'data-jp-theme-light' ) === 'false' ) {
72
+ return 'dark' ;
73
+ } else if ( body . getAttribute ( 'data-jp-theme-light' ) === 'true' ) {
74
+ return 'light' ;
75
+ }
76
+
77
+ // Guess based on a reference element's color
78
+ const color = window . getComputedStyle ( element , null ) . getPropertyValue ( 'color' ) ;
79
+ const match = color . match ( / ^ r g b \s * \( \s * ( \d + ) \s * , \s * ( \d + ) \s * , \s * ( \d + ) \s * \) \s * $ / i) ;
80
+ if ( match ) {
81
+ const [ r , g , b ] = [ match [ 1 ] , match [ 2 ] , match [ 3 ] ] ;
82
+
83
+ // https://en.wikipedia.org/wiki/HSL_and_HSV#Lightness
84
+ const luma = 0.299 * r + 0.587 * g + 0.114 * b ;
85
+
86
+ if ( luma > 180 ) {
87
+ // If the text is very bright we have a dark theme
88
+ return 'dark' ;
89
+ }
90
+ if ( luma < 75 ) {
91
+ // If the text is very dark we have a light theme
92
+ return 'light' ;
93
+ }
94
+ // Otherwise fall back to the next heuristic.
95
+ }
96
+
97
+ // Fallback to system preference
98
+ return window . matchMedia ( '(prefers-color-scheme: dark)' ) . matches ? 'dark' : 'light' ;
99
+ }
100
+
101
+
102
+ function forceTheme ( elementId ) {
103
+ const estimatorElement = document . querySelector ( `#${ elementId } ` ) ;
104
+ if ( estimatorElement === null ) {
105
+ console . error ( `Element with id ${ elementId } not found.` ) ;
106
+ } else {
107
+ const theme = detectTheme ( estimatorElement ) ;
108
+ estimatorElement . classList . add ( theme ) ;
109
+ }
110
+ }
0 commit comments