devtools-tracing 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/generate.ts +643 -0
- package/index.ts +16 -0
- package/lib/front_end/core/common/App.ts +7 -0
- package/lib/front_end/core/common/AppProvider.ts +32 -0
- package/lib/front_end/core/common/Base64.ts +47 -0
- package/lib/front_end/core/common/CharacterIdMap.ts +30 -0
- package/lib/front_end/core/common/Color.ts +2506 -0
- package/lib/front_end/core/common/ColorConverter.ts +402 -0
- package/lib/front_end/core/common/ColorUtils.ts +252 -0
- package/lib/front_end/core/common/Console.ts +114 -0
- package/lib/front_end/core/common/Debouncer.ts +15 -0
- package/lib/front_end/core/common/EventTarget.ts +52 -0
- package/lib/front_end/core/common/Gzip.ts +74 -0
- package/lib/front_end/core/common/JavaScriptMetaData.ts +29 -0
- package/lib/front_end/core/common/Lazy.ts +31 -0
- package/lib/front_end/core/common/Linkifier.ts +55 -0
- package/lib/front_end/core/common/MapWithDefault.ts +26 -0
- package/lib/front_end/core/common/Mutex.ts +55 -0
- package/lib/front_end/core/common/Object.ts +145 -0
- package/lib/front_end/core/common/ParsedURL.ts +554 -0
- package/lib/front_end/core/common/Progress.ts +180 -0
- package/lib/front_end/core/common/QueryParamHandler.ts +7 -0
- package/lib/front_end/core/common/ResolverBase.ts +85 -0
- package/lib/front_end/core/common/ResourceType.ts +588 -0
- package/lib/front_end/core/common/ReturnToPanel.ts +17 -0
- package/lib/front_end/core/common/Revealer.ts +192 -0
- package/lib/front_end/core/common/Runnable.ts +41 -0
- package/lib/front_end/core/common/SegmentedRange.ts +87 -0
- package/lib/front_end/core/common/SettingRegistration.ts +339 -0
- package/lib/front_end/core/common/Settings.ts +1497 -0
- package/lib/front_end/core/common/SimpleHistoryManager.ts +124 -0
- package/lib/front_end/core/common/StringOutputStream.ts +26 -0
- package/lib/front_end/core/common/TextDictionary.ts +48 -0
- package/lib/front_end/core/common/Throttler.ts +99 -0
- package/lib/front_end/core/common/Trie.ts +152 -0
- package/lib/front_end/core/common/Worker.ts +60 -0
- package/lib/front_end/core/common/common.ts +81 -0
- package/lib/front_end/core/host/AidaClient.ts +733 -0
- package/lib/front_end/core/host/GdpClient.ts +316 -0
- package/lib/front_end/core/host/InspectorFrontendHost.ts +648 -0
- package/lib/front_end/core/host/InspectorFrontendHostAPI.ts +551 -0
- package/lib/front_end/core/host/Platform.ts +76 -0
- package/lib/front_end/core/host/ResourceLoader.ts +282 -0
- package/lib/front_end/core/host/UserMetrics.ts +1230 -0
- package/lib/front_end/core/host/host.ts +23 -0
- package/lib/front_end/core/i18n/ByteUtilities.ts +82 -0
- package/lib/front_end/core/i18n/DevToolsLocale.ts +87 -0
- package/lib/front_end/core/i18n/NumberFormatter.ts +82 -0
- package/lib/front_end/core/i18n/i18n.ts +17 -0
- package/lib/front_end/core/i18n/i18nImpl.ts +204 -0
- package/lib/front_end/core/i18n/i18nTypes.ts +10 -0
- package/lib/front_end/core/i18n/locales.js +14 -0
- package/lib/front_end/core/i18n/time-utilities.ts +174 -0
- package/lib/front_end/core/platform/ArrayUtilities.ts +271 -0
- package/lib/front_end/core/platform/Brand.ts +23 -0
- package/lib/front_end/core/platform/Constructor.ts +10 -0
- package/lib/front_end/core/platform/DOMUtilities.ts +138 -0
- package/lib/front_end/core/platform/DateUtilities.ts +15 -0
- package/lib/front_end/core/platform/DevToolsPath.ts +53 -0
- package/lib/front_end/core/platform/KeyboardUtilities.ts +38 -0
- package/lib/front_end/core/platform/MapUtilities.ts +95 -0
- package/lib/front_end/core/platform/MimeType.ts +175 -0
- package/lib/front_end/core/platform/NumberUtilities.ts +80 -0
- package/lib/front_end/core/platform/StringUtilities.ts +588 -0
- package/lib/front_end/core/platform/Timing.ts +17 -0
- package/lib/front_end/core/platform/TypedArrayUtilities.ts +189 -0
- package/lib/front_end/core/platform/TypescriptUtilities.ts +86 -0
- package/lib/front_end/core/platform/UIString.ts +39 -0
- package/lib/front_end/core/platform/UserVisibleError.ts +28 -0
- package/lib/front_end/core/platform/platform.ts +45 -0
- package/lib/front_end/core/protocol_client/ConnectionTransport.ts +26 -0
- package/lib/front_end/core/protocol_client/InspectorBackend.ts +1050 -0
- package/lib/front_end/core/protocol_client/NodeURL.ts +42 -0
- package/lib/front_end/core/protocol_client/protocol_client.ts +13 -0
- package/lib/front_end/core/root/Runtime.ts +609 -0
- package/lib/front_end/core/root/root.ts +6 -0
- package/lib/front_end/core/sdk/AccessibilityModel.ts +353 -0
- package/lib/front_end/core/sdk/AnimationModel.ts +1041 -0
- package/lib/front_end/core/sdk/AutofillModel.ts +184 -0
- package/lib/front_end/core/sdk/CPUProfilerModel.ts +148 -0
- package/lib/front_end/core/sdk/CPUThrottlingManager.ts +282 -0
- package/lib/front_end/core/sdk/CSSContainerQuery.ts +139 -0
- package/lib/front_end/core/sdk/CSSFontFace.ts +40 -0
- package/lib/front_end/core/sdk/CSSLayer.ts +30 -0
- package/lib/front_end/core/sdk/CSSMatchedStyles.ts +1646 -0
- package/lib/front_end/core/sdk/CSSMedia.ts +121 -0
- package/lib/front_end/core/sdk/CSSMetadata.ts +1647 -0
- package/lib/front_end/core/sdk/CSSModel.ts +1128 -0
- package/lib/front_end/core/sdk/CSSProperty.ts +384 -0
- package/lib/front_end/core/sdk/CSSPropertyParser.ts +681 -0
- package/lib/front_end/core/sdk/CSSPropertyParserMatchers.ts +1395 -0
- package/lib/front_end/core/sdk/CSSQuery.ts +72 -0
- package/lib/front_end/core/sdk/CSSRule.ts +465 -0
- package/lib/front_end/core/sdk/CSSScope.ts +30 -0
- package/lib/front_end/core/sdk/CSSStartingStyle.ts +29 -0
- package/lib/front_end/core/sdk/CSSStyleDeclaration.ts +313 -0
- package/lib/front_end/core/sdk/CSSStyleSheetHeader.ts +196 -0
- package/lib/front_end/core/sdk/CSSSupports.ts +33 -0
- package/lib/front_end/core/sdk/CategorizedBreakpoint.ts +64 -0
- package/lib/front_end/core/sdk/ChildTargetManager.ts +314 -0
- package/lib/front_end/core/sdk/CompilerSourceMappingContentProvider.ts +62 -0
- package/lib/front_end/core/sdk/Connections.ts +293 -0
- package/lib/front_end/core/sdk/ConsoleModel.ts +808 -0
- package/lib/front_end/core/sdk/ConsoleModelTypes.ts +15 -0
- package/lib/front_end/core/sdk/Cookie.ts +319 -0
- package/lib/front_end/core/sdk/CookieModel.ts +239 -0
- package/lib/front_end/core/sdk/CookieParser.ts +185 -0
- package/lib/front_end/core/sdk/DOMDebuggerModel.ts +787 -0
- package/lib/front_end/core/sdk/DOMModel.ts +1961 -0
- package/lib/front_end/core/sdk/DebuggerModel.ts +1605 -0
- package/lib/front_end/core/sdk/EmulationModel.ts +648 -0
- package/lib/front_end/core/sdk/EnhancedTracesParser.ts +515 -0
- package/lib/front_end/core/sdk/EventBreakpointsModel.ts +183 -0
- package/lib/front_end/core/sdk/FrameAssociated.ts +11 -0
- package/lib/front_end/core/sdk/FrameManager.ts +259 -0
- package/lib/front_end/core/sdk/HeapProfilerModel.ts +225 -0
- package/lib/front_end/core/sdk/HttpReasonPhraseStrings.ts +77 -0
- package/lib/front_end/core/sdk/IOModel.ts +91 -0
- package/lib/front_end/core/sdk/IsolateManager.ts +257 -0
- package/lib/front_end/core/sdk/IssuesModel.ts +70 -0
- package/lib/front_end/core/sdk/LayerTreeBase.ts +169 -0
- package/lib/front_end/core/sdk/LogModel.ts +56 -0
- package/lib/front_end/core/sdk/NetworkManager.ts +2823 -0
- package/lib/front_end/core/sdk/NetworkRequest.ts +2253 -0
- package/lib/front_end/core/sdk/OverlayColorGenerator.ts +52 -0
- package/lib/front_end/core/sdk/OverlayModel.ts +1011 -0
- package/lib/front_end/core/sdk/OverlayPersistentHighlighter.ts +522 -0
- package/lib/front_end/core/sdk/PageLoad.ts +35 -0
- package/lib/front_end/core/sdk/PageResourceLoader.ts +435 -0
- package/lib/front_end/core/sdk/PaintProfiler.ts +110 -0
- package/lib/front_end/core/sdk/PerformanceMetricsModel.ts +84 -0
- package/lib/front_end/core/sdk/PreloadingModel.ts +863 -0
- package/lib/front_end/core/sdk/RehydratingConnection.ts +386 -0
- package/lib/front_end/core/sdk/RehydratingObject.ts +66 -0
- package/lib/front_end/core/sdk/RemoteObject.ts +1160 -0
- package/lib/front_end/core/sdk/Resource.ts +232 -0
- package/lib/front_end/core/sdk/ResourceTreeModel.ts +1160 -0
- package/lib/front_end/core/sdk/RuntimeModel.ts +732 -0
- package/lib/front_end/core/sdk/SDKModel.ts +65 -0
- package/lib/front_end/core/sdk/ScopeTreeCache.ts +45 -0
- package/lib/front_end/core/sdk/ScreenCaptureModel.ts +255 -0
- package/lib/front_end/core/sdk/Script.ts +534 -0
- package/lib/front_end/core/sdk/SecurityOriginManager.ts +76 -0
- package/lib/front_end/core/sdk/ServerSentEvents.ts +80 -0
- package/lib/front_end/core/sdk/ServerSentEventsProtocol.ts +122 -0
- package/lib/front_end/core/sdk/ServerTiming.ts +260 -0
- package/lib/front_end/core/sdk/ServiceWorkerCacheModel.ts +377 -0
- package/lib/front_end/core/sdk/ServiceWorkerManager.ts +605 -0
- package/lib/front_end/core/sdk/SourceMap.ts +867 -0
- package/lib/front_end/core/sdk/SourceMapCache.ts +54 -0
- package/lib/front_end/core/sdk/SourceMapFunctionRanges.ts +156 -0
- package/lib/front_end/core/sdk/SourceMapManager.ts +239 -0
- package/lib/front_end/core/sdk/SourceMapScopeChainEntry.ts +189 -0
- package/lib/front_end/core/sdk/SourceMapScopesInfo.ts +508 -0
- package/lib/front_end/core/sdk/StorageBucketsModel.ts +204 -0
- package/lib/front_end/core/sdk/StorageKeyManager.ts +98 -0
- package/lib/front_end/core/sdk/Target.ts +332 -0
- package/lib/front_end/core/sdk/TargetManager.ts +453 -0
- package/lib/front_end/core/sdk/TraceObject.ts +61 -0
- package/lib/front_end/core/sdk/WebAuthnModel.ts +104 -0
- package/lib/front_end/core/sdk/sdk.ts +174 -0
- package/lib/front_end/entrypoints/formatter_worker/FormatterActions.ts +59 -0
- package/lib/front_end/generated/InspectorBackendCommands.js +1617 -0
- package/lib/front_end/generated/SupportedCSSProperties.js +7512 -0
- package/lib/front_end/generated/protocol-proxy-api.d.ts +5022 -0
- package/lib/front_end/generated/protocol.ts +22014 -0
- package/lib/front_end/models/cpu_profile/CPUProfileDataModel.ts +571 -0
- package/lib/front_end/models/cpu_profile/ProfileTreeModel.ts +103 -0
- package/lib/front_end/models/cpu_profile/cpu_profile.ts +11 -0
- package/lib/front_end/models/formatter/FormatterWorkerPool.ts +219 -0
- package/lib/front_end/models/formatter/ScriptFormatter.ts +112 -0
- package/lib/front_end/models/formatter/formatter.ts +8 -0
- package/lib/front_end/models/text_utils/CodeMirrorUtils.ts +37 -0
- package/lib/front_end/models/text_utils/ContentData.ts +199 -0
- package/lib/front_end/models/text_utils/ContentProvider.ts +68 -0
- package/lib/front_end/models/text_utils/StaticContentProvider.ts +49 -0
- package/lib/front_end/models/text_utils/StreamingContentData.ts +108 -0
- package/lib/front_end/models/text_utils/Text.ts +90 -0
- package/lib/front_end/models/text_utils/TextCursor.ts +44 -0
- package/lib/front_end/models/text_utils/TextRange.ts +266 -0
- package/lib/front_end/models/text_utils/TextUtils.ts +401 -0
- package/lib/front_end/models/text_utils/WasmDisassembly.ts +87 -0
- package/lib/front_end/models/text_utils/text_utils.ts +27 -0
- package/lib/front_end/models/trace/EntityMapper.ts +141 -0
- package/lib/front_end/models/trace/EventsSerializer.ts +101 -0
- package/lib/front_end/models/trace/LanternComputationData.ts +438 -0
- package/lib/front_end/models/trace/ModelImpl.ts +236 -0
- package/lib/front_end/models/trace/Name.ts +136 -0
- package/lib/front_end/models/trace/Processor.ts +652 -0
- package/lib/front_end/models/trace/Styles.ts +1138 -0
- package/lib/front_end/models/trace/extras/FilmStrip.ts +78 -0
- package/lib/front_end/models/trace/extras/MainThreadActivity.ts +86 -0
- package/lib/front_end/models/trace/extras/ScriptDuplication.ts +236 -0
- package/lib/front_end/models/trace/extras/StackTraceForEvent.ts +203 -0
- package/lib/front_end/models/trace/extras/ThirdParties.ts +164 -0
- package/lib/front_end/models/trace/extras/TraceFilter.ts +62 -0
- package/lib/front_end/models/trace/extras/TraceTree.ts +701 -0
- package/lib/front_end/models/trace/extras/extras.ts +11 -0
- package/lib/front_end/models/trace/handlers/AnimationFramesHandler.ts +128 -0
- package/lib/front_end/models/trace/handlers/AnimationHandler.ts +36 -0
- package/lib/front_end/models/trace/handlers/AsyncJSCallsHandler.ts +239 -0
- package/lib/front_end/models/trace/handlers/AuctionWorkletsHandler.ts +183 -0
- package/lib/front_end/models/trace/handlers/DOMStatsHandler.ts +31 -0
- package/lib/front_end/models/trace/handlers/ExtensionTraceDataHandler.ts +306 -0
- package/lib/front_end/models/trace/handlers/FlowsHandler.ts +175 -0
- package/lib/front_end/models/trace/handlers/FramesHandler.ts +571 -0
- package/lib/front_end/models/trace/handlers/GPUHandler.ts +50 -0
- package/lib/front_end/models/trace/handlers/ImagePaintingHandler.ts +183 -0
- package/lib/front_end/models/trace/handlers/InitiatorsHandler.ts +193 -0
- package/lib/front_end/models/trace/handlers/InvalidationsHandler.ts +168 -0
- package/lib/front_end/models/trace/handlers/LargestImagePaintHandler.ts +109 -0
- package/lib/front_end/models/trace/handlers/LargestTextPaintHandler.ts +35 -0
- package/lib/front_end/models/trace/handlers/LayerTreeHandler.ts +123 -0
- package/lib/front_end/models/trace/handlers/LayoutShiftsHandler.ts +573 -0
- package/lib/front_end/models/trace/handlers/MemoryHandler.ts +31 -0
- package/lib/front_end/models/trace/handlers/MetaHandler.ts +525 -0
- package/lib/front_end/models/trace/handlers/ModelHandlers.ts +34 -0
- package/lib/front_end/models/trace/handlers/NetworkRequestsHandler.ts +672 -0
- package/lib/front_end/models/trace/handlers/PageFramesHandler.ts +52 -0
- package/lib/front_end/models/trace/handlers/PageLoadMetricsHandler.ts +460 -0
- package/lib/front_end/models/trace/handlers/RendererHandler.ts +428 -0
- package/lib/front_end/models/trace/handlers/SamplesHandler.ts +271 -0
- package/lib/front_end/models/trace/handlers/ScreenshotsHandler.ts +122 -0
- package/lib/front_end/models/trace/handlers/ScriptsHandler.ts +336 -0
- package/lib/front_end/models/trace/handlers/SelectorStatsHandler.ts +110 -0
- package/lib/front_end/models/trace/handlers/Threads.ts +139 -0
- package/lib/front_end/models/trace/handlers/UserInteractionsHandler.ts +400 -0
- package/lib/front_end/models/trace/handlers/UserTimingsHandler.ts +233 -0
- package/lib/front_end/models/trace/handlers/WarningsHandler.ts +162 -0
- package/lib/front_end/models/trace/handlers/WorkersHandler.ts +45 -0
- package/lib/front_end/models/trace/handlers/handlers.ts +8 -0
- package/lib/front_end/models/trace/handlers/helpers.ts +196 -0
- package/lib/front_end/models/trace/handlers/types.ts +75 -0
- package/lib/front_end/models/trace/helpers/Extensions.ts +54 -0
- package/lib/front_end/models/trace/helpers/Network.ts +129 -0
- package/lib/front_end/models/trace/helpers/SamplesIntegrator.ts +544 -0
- package/lib/front_end/models/trace/helpers/SyntheticEvents.ts +87 -0
- package/lib/front_end/models/trace/helpers/Timing.ts +248 -0
- package/lib/front_end/models/trace/helpers/Trace.ts +928 -0
- package/lib/front_end/models/trace/helpers/TreeHelpers.ts +320 -0
- package/lib/front_end/models/trace/helpers/helpers.ts +11 -0
- package/lib/front_end/models/trace/insights/CLSCulprits.ts +668 -0
- package/lib/front_end/models/trace/insights/Cache.ts +269 -0
- package/lib/front_end/models/trace/insights/Common.ts +453 -0
- package/lib/front_end/models/trace/insights/DOMSize.ts +223 -0
- package/lib/front_end/models/trace/insights/DocumentLatency.ts +319 -0
- package/lib/front_end/models/trace/insights/DuplicatedJavaScript.ts +126 -0
- package/lib/front_end/models/trace/insights/FontDisplay.ts +119 -0
- package/lib/front_end/models/trace/insights/ForcedReflow.ts +220 -0
- package/lib/front_end/models/trace/insights/INPBreakdown.ts +171 -0
- package/lib/front_end/models/trace/insights/ImageDelivery.ts +348 -0
- package/lib/front_end/models/trace/insights/LCPBreakdown.ts +268 -0
- package/lib/front_end/models/trace/insights/LCPDiscovery.ts +237 -0
- package/lib/front_end/models/trace/insights/LegacyJavaScript.ts +138 -0
- package/lib/front_end/models/trace/insights/Models.ts +22 -0
- package/lib/front_end/models/trace/insights/ModernHTTP.ts +257 -0
- package/lib/front_end/models/trace/insights/NetworkDependencyTree.ts +726 -0
- package/lib/front_end/models/trace/insights/RenderBlocking.ts +257 -0
- package/lib/front_end/models/trace/insights/SlowCSSSelector.ts +175 -0
- package/lib/front_end/models/trace/insights/Statistics.ts +101 -0
- package/lib/front_end/models/trace/insights/ThirdParties.ts +130 -0
- package/lib/front_end/models/trace/insights/Viewport.ts +138 -0
- package/lib/front_end/models/trace/insights/insights.ts +10 -0
- package/lib/front_end/models/trace/insights/types.ts +157 -0
- package/lib/front_end/models/trace/lantern/core/LanternError.ts +7 -0
- package/lib/front_end/models/trace/lantern/core/NetworkAnalyzer.ts +619 -0
- package/lib/front_end/models/trace/lantern/core/core.ts +6 -0
- package/lib/front_end/models/trace/lantern/graph/BaseNode.ts +345 -0
- package/lib/front_end/models/trace/lantern/graph/CPUNode.ts +80 -0
- package/lib/front_end/models/trace/lantern/graph/NetworkNode.ts +101 -0
- package/lib/front_end/models/trace/lantern/graph/PageDependencyGraph.ts +636 -0
- package/lib/front_end/models/trace/lantern/graph/graph.ts +8 -0
- package/lib/front_end/models/trace/lantern/lantern.ts +17 -0
- package/lib/front_end/models/trace/lantern/metrics/FirstContentfulPaint.ts +187 -0
- package/lib/front_end/models/trace/lantern/metrics/Interactive.ts +88 -0
- package/lib/front_end/models/trace/lantern/metrics/LargestContentfulPaint.ts +92 -0
- package/lib/front_end/models/trace/lantern/metrics/MaxPotentialFID.ts +72 -0
- package/lib/front_end/models/trace/lantern/metrics/Metric.ts +126 -0
- package/lib/front_end/models/trace/lantern/metrics/SpeedIndex.ts +126 -0
- package/lib/front_end/models/trace/lantern/metrics/TBTUtils.ts +82 -0
- package/lib/front_end/models/trace/lantern/metrics/TotalBlockingTime.ts +112 -0
- package/lib/front_end/models/trace/lantern/metrics/metrics.ts +12 -0
- package/lib/front_end/models/trace/lantern/simulation/ConnectionPool.ts +150 -0
- package/lib/front_end/models/trace/lantern/simulation/Constants.ts +46 -0
- package/lib/front_end/models/trace/lantern/simulation/DNSCache.ts +61 -0
- package/lib/front_end/models/trace/lantern/simulation/SimulationTimingMap.ts +196 -0
- package/lib/front_end/models/trace/lantern/simulation/Simulator.ts +556 -0
- package/lib/front_end/models/trace/lantern/simulation/TCPConnection.ts +192 -0
- package/lib/front_end/models/trace/lantern/simulation/simulation.ts +10 -0
- package/lib/front_end/models/trace/lantern/types/Lantern.ts +220 -0
- package/lib/front_end/models/trace/lantern/types/types.ts +5 -0
- package/lib/front_end/models/trace/trace.ts +33 -0
- package/lib/front_end/models/trace/types/Configuration.ts +110 -0
- package/lib/front_end/models/trace/types/Extensions.ts +136 -0
- package/lib/front_end/models/trace/types/File.ts +281 -0
- package/lib/front_end/models/trace/types/Overlays.ts +138 -0
- package/lib/front_end/models/trace/types/Timing.ts +30 -0
- package/lib/front_end/models/trace/types/TraceEvents.ts +3277 -0
- package/lib/front_end/models/trace/types/types.ts +10 -0
- package/lib/front_end/third_party/i18n/LICENSE +202 -0
- package/lib/front_end/third_party/i18n/README.chromium +15 -0
- package/lib/front_end/third_party/i18n/i18n-impl.ts +61 -0
- package/lib/front_end/third_party/i18n/i18n.ts +11 -0
- package/lib/front_end/third_party/i18n/localized-string-set.ts +129 -0
- package/lib/front_end/third_party/intl-messageformat/LICENSE +33 -0
- package/lib/front_end/third_party/intl-messageformat/README.chromium +24 -0
- package/lib/front_end/third_party/intl-messageformat/intl-messageformat-tsconfig.json +16 -0
- package/lib/front_end/third_party/intl-messageformat/intl-messageformat.ts +6 -0
- package/lib/front_end/third_party/intl-messageformat/package/LICENSE.md +33 -0
- package/lib/front_end/third_party/intl-messageformat/package/README.md +3 -0
- package/lib/front_end/third_party/intl-messageformat/package/index.d.ts +6 -0
- package/lib/front_end/third_party/intl-messageformat/package/index.d.ts.map +1 -0
- package/lib/front_end/third_party/intl-messageformat/package/index.js +13 -0
- package/lib/front_end/third_party/intl-messageformat/package/intl-messageformat.esm.d.ts +5 -0
- package/lib/front_end/third_party/intl-messageformat/package/intl-messageformat.esm.js +1710 -0
- package/lib/front_end/third_party/intl-messageformat/package/intl-messageformat.iife.js +1815 -0
- package/lib/front_end/third_party/intl-messageformat/package/lib/index.d.ts +6 -0
- package/lib/front_end/third_party/intl-messageformat/package/lib/index.d.ts.map +1 -0
- package/lib/front_end/third_party/intl-messageformat/package/lib/index.js +10 -0
- package/lib/front_end/third_party/intl-messageformat/package/lib/src/core.d.ts +34 -0
- package/lib/front_end/third_party/intl-messageformat/package/lib/src/core.d.ts.map +1 -0
- package/lib/front_end/third_party/intl-messageformat/package/lib/src/core.js +229 -0
- package/lib/front_end/third_party/intl-messageformat/package/lib/src/error.d.ts +28 -0
- package/lib/front_end/third_party/intl-messageformat/package/lib/src/error.d.ts.map +1 -0
- package/lib/front_end/third_party/intl-messageformat/package/lib/src/error.js +48 -0
- package/lib/front_end/third_party/intl-messageformat/package/lib/src/formatters.d.ts +34 -0
- package/lib/front_end/third_party/intl-messageformat/package/lib/src/formatters.d.ts.map +1 -0
- package/lib/front_end/third_party/intl-messageformat/package/lib/src/formatters.js +179 -0
- package/lib/front_end/third_party/intl-messageformat/package/package.json +42 -0
- package/lib/front_end/third_party/intl-messageformat/package/src/core.d.ts +34 -0
- package/lib/front_end/third_party/intl-messageformat/package/src/core.d.ts.map +1 -0
- package/lib/front_end/third_party/intl-messageformat/package/src/core.js +230 -0
- package/lib/front_end/third_party/intl-messageformat/package/src/error.d.ts +28 -0
- package/lib/front_end/third_party/intl-messageformat/package/src/error.d.ts.map +1 -0
- package/lib/front_end/third_party/intl-messageformat/package/src/error.js +51 -0
- package/lib/front_end/third_party/intl-messageformat/package/src/formatters.d.ts +34 -0
- package/lib/front_end/third_party/intl-messageformat/package/src/formatters.d.ts.map +1 -0
- package/lib/front_end/third_party/intl-messageformat/package/src/formatters.js +182 -0
- package/lib/front_end/third_party/intl-messageformat/package/src/icu-messageformat-parser/error.d.ts +79 -0
- package/lib/front_end/third_party/intl-messageformat/package/src/icu-messageformat-parser/index.d.ts +15 -0
- package/lib/front_end/third_party/intl-messageformat/package/src/icu-messageformat-parser/parser.d.ts +153 -0
- package/lib/front_end/third_party/intl-messageformat/package/src/icu-messageformat-parser/types.d.ts +139 -0
- package/lib/front_end/third_party/legacy-javascript/LICENSE +202 -0
- package/lib/front_end/third_party/legacy-javascript/README.chromium +13 -0
- package/lib/front_end/third_party/legacy-javascript/legacy-javascript-tsconfig.json +8 -0
- package/lib/front_end/third_party/legacy-javascript/legacy-javascript.ts +3 -0
- package/lib/front_end/third_party/legacy-javascript/lib/legacy-javascript.d.ts +18 -0
- package/lib/front_end/third_party/legacy-javascript/lib/legacy-javascript.js +943 -0
- package/lib/front_end/third_party/legacy-javascript/package.json +8 -0
- package/lib/front_end/third_party/legacy-javascript/rebuild.sh +9 -0
- package/lib/front_end/third_party/third-party-web/LICENSE +20 -0
- package/lib/front_end/third_party/third-party-web/README.chromium +13 -0
- package/lib/front_end/third_party/third-party-web/lib/nostats-subset.d.ts +2 -0
- package/lib/front_end/third_party/third-party-web/lib/nostats-subset.js +149 -0
- package/lib/front_end/third_party/third-party-web/package/LICENSE +20 -0
- package/lib/front_end/third_party/third-party-web/package/README.md +929 -0
- package/lib/front_end/third_party/third-party-web/package/dist/entities-httparchive-nostats.json +1 -0
- package/lib/front_end/third_party/third-party-web/package/dist/entities-httparchive.json +1 -0
- package/lib/front_end/third_party/third-party-web/package/dist/entities-nostats.json +1 -0
- package/lib/front_end/third_party/third-party-web/package/dist/entities.json +1 -0
- package/lib/front_end/third_party/third-party-web/package/facades.md +46 -0
- package/lib/front_end/third_party/third-party-web/package/httparchive-nostats-subset.d.ts +1 -0
- package/lib/front_end/third_party/third-party-web/package/httparchive-nostats-subset.js +1 -0
- package/lib/front_end/third_party/third-party-web/package/httparchive-subset.d.ts +1 -0
- package/lib/front_end/third_party/third-party-web/package/httparchive-subset.js +1 -0
- package/lib/front_end/third_party/third-party-web/package/lib/__snapshots__/index.test.js.snap +1006 -0
- package/lib/front_end/third_party/third-party-web/package/lib/create-entity-finder-api.js +139 -0
- package/lib/front_end/third_party/third-party-web/package/lib/create-entity-finder-api.test.js +44 -0
- package/lib/front_end/third_party/third-party-web/package/lib/entities.test.js +27 -0
- package/lib/front_end/third_party/third-party-web/package/lib/index.d.ts +34 -0
- package/lib/front_end/third_party/third-party-web/package/lib/index.js +3 -0
- package/lib/front_end/third_party/third-party-web/package/lib/index.test.js +246 -0
- package/lib/front_end/third_party/third-party-web/package/lib/markdown/faqs.partial.md +36 -0
- package/lib/front_end/third_party/third-party-web/package/lib/markdown/goals.partial.md +9 -0
- package/lib/front_end/third_party/third-party-web/package/lib/markdown/methodology.partial.md +5 -0
- package/lib/front_end/third_party/third-party-web/package/lib/markdown/template.md +151 -0
- package/lib/front_end/third_party/third-party-web/package/lib/markdown/updates/2019-02-01.md +1 -0
- package/lib/front_end/third_party/third-party-web/package/lib/markdown/updates/2019-03-01.md +1 -0
- package/lib/front_end/third_party/third-party-web/package/lib/markdown/updates/2019-05-06.md +1 -0
- package/lib/front_end/third_party/third-party-web/package/lib/markdown/updates/2019-05-13.md +14 -0
- package/lib/front_end/third_party/third-party-web/package/lib/markdown/updates/2021-01-01.md +1 -0
- package/lib/front_end/third_party/third-party-web/package/lib/markdown/updates/2024-07-01.md +3 -0
- package/lib/front_end/third_party/third-party-web/package/lib/subsets/httparchive-nostats.d.ts +1 -0
- package/lib/front_end/third_party/third-party-web/package/lib/subsets/httparchive-nostats.js +3 -0
- package/lib/front_end/third_party/third-party-web/package/lib/subsets/httparchive.d.ts +1 -0
- package/lib/front_end/third_party/third-party-web/package/lib/subsets/httparchive.js +3 -0
- package/lib/front_end/third_party/third-party-web/package/lib/subsets/nostats.d.ts +1 -0
- package/lib/front_end/third_party/third-party-web/package/lib/subsets/nostats.js +3 -0
- package/lib/front_end/third_party/third-party-web/package/nostats-subset.d.ts +1 -0
- package/lib/front_end/third_party/third-party-web/package/nostats-subset.js +1 -0
- package/lib/front_end/third_party/third-party-web/package/package.json +46 -0
- package/lib/front_end/third_party/third-party-web/package.json +8 -0
- package/lib/front_end/third_party/third-party-web/rebuild.sh +13 -0
- package/lib/front_end/third_party/third-party-web/third-party-web-tsconfig.json +8 -0
- package/lib/front_end/third_party/third-party-web/third-party-web.ts +3 -0
- package/package.json +24 -0
- package/patches/chrome-devtools-frontend+1.0.1533544.patch +187 -0
|
@@ -0,0 +1,2506 @@
|
|
|
1
|
+
// Copyright 2021 The Chromium Authors
|
|
2
|
+
// Use of this source code is governed by a BSD-style license that can be
|
|
3
|
+
// found in the LICENSE file.
|
|
4
|
+
|
|
5
|
+
/*
|
|
6
|
+
* Copyright (C) 2009 Apple Inc. All rights reserved.
|
|
7
|
+
* Copyright (C) 2009 Joseph Pecoraro
|
|
8
|
+
*
|
|
9
|
+
* Redistribution and use in source and binary forms, with or without
|
|
10
|
+
* modification, are permitted provided that the following conditions
|
|
11
|
+
* are met:
|
|
12
|
+
*
|
|
13
|
+
* 1. Redistributions of source code must retain the above copyright
|
|
14
|
+
* notice, this list of conditions and the following disclaimer.
|
|
15
|
+
* 2. Redistributions in binary form must reproduce the above copyright
|
|
16
|
+
* notice, this list of conditions and the following disclaimer in the
|
|
17
|
+
* documentation and/or other materials provided with the distribution.
|
|
18
|
+
* 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
|
|
19
|
+
* its contributors may be used to endorse or promote products derived
|
|
20
|
+
* from this software without specific prior written permission.
|
|
21
|
+
*
|
|
22
|
+
* THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
|
|
23
|
+
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
24
|
+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
25
|
+
* DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
|
|
26
|
+
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
27
|
+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
28
|
+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
29
|
+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
30
|
+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
31
|
+
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
import * as Platform from '../platform/platform.js';
|
|
35
|
+
|
|
36
|
+
import {ColorConverter} from './ColorConverter.js';
|
|
37
|
+
import {
|
|
38
|
+
blendColors,
|
|
39
|
+
type Color3D,
|
|
40
|
+
type Color4D,
|
|
41
|
+
type Color4DOr3D,
|
|
42
|
+
contrastRatioAPCA,
|
|
43
|
+
desiredLuminanceAPCA,
|
|
44
|
+
luminance,
|
|
45
|
+
luminanceAPCA,
|
|
46
|
+
rgbToHsl,
|
|
47
|
+
rgbToHwb,
|
|
48
|
+
} from './ColorUtils.js';
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* <hue> is defined as a <number> or <angle>
|
|
52
|
+
* and we hold this in degrees. However, after
|
|
53
|
+
* the conversions, these degrees can result in
|
|
54
|
+
* negative values. That's why we normalize the hue to be
|
|
55
|
+
* between [0 - 360].
|
|
56
|
+
**/
|
|
57
|
+
function normalizeHue(hue: number): number {
|
|
58
|
+
// Even though it is highly unlikely, hue can be
|
|
59
|
+
// very negative like -400. The initial modulo
|
|
60
|
+
// operation makes sure that the if the number is
|
|
61
|
+
// negative, it is between [-360, 0].
|
|
62
|
+
return ((hue % 360) + 360) % 360;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Parses angle in the form of
|
|
67
|
+
* `<angle>deg`, `<angle>turn`, `<angle>grad and `<angle>rad`
|
|
68
|
+
* and returns the canonicalized `degree`.
|
|
69
|
+
**/
|
|
70
|
+
function parseAngle(angleText: string): number|null {
|
|
71
|
+
const angle = angleText.replace(/(deg|g?rad|turn)$/, '');
|
|
72
|
+
// @ts-expect-error: isNaN can accept strings
|
|
73
|
+
if (isNaN(angle) || angleText.match(/\s+(deg|g?rad|turn)/)) {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const number = parseFloat(angle);
|
|
78
|
+
if (angleText.includes('turn')) {
|
|
79
|
+
// 1turn === 360deg
|
|
80
|
+
return number * 360;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (angleText.includes('grad')) {
|
|
84
|
+
// 1grad === 0.9deg
|
|
85
|
+
return number * 9 / 10;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (angleText.includes('rad')) {
|
|
89
|
+
// πrad === 180deg
|
|
90
|
+
return number * 180 / Math.PI;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// 1deg === 1deg ^_^
|
|
94
|
+
return number;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Returns the `Format` equivalent from the format text **/
|
|
98
|
+
export function getFormat(formatText: string): Format|null {
|
|
99
|
+
switch (formatText) {
|
|
100
|
+
case Format.HEX:
|
|
101
|
+
return Format.HEX;
|
|
102
|
+
case Format.HEXA:
|
|
103
|
+
return Format.HEXA;
|
|
104
|
+
case Format.RGB:
|
|
105
|
+
return Format.RGB;
|
|
106
|
+
case Format.RGBA:
|
|
107
|
+
return Format.RGBA;
|
|
108
|
+
case Format.HSL:
|
|
109
|
+
return Format.HSL;
|
|
110
|
+
case Format.HSLA:
|
|
111
|
+
return Format.HSLA;
|
|
112
|
+
case Format.HWB:
|
|
113
|
+
return Format.HWB;
|
|
114
|
+
case Format.HWBA:
|
|
115
|
+
return Format.HWBA;
|
|
116
|
+
case Format.LCH:
|
|
117
|
+
return Format.LCH;
|
|
118
|
+
case Format.OKLCH:
|
|
119
|
+
return Format.OKLCH;
|
|
120
|
+
case Format.LAB:
|
|
121
|
+
return Format.LAB;
|
|
122
|
+
case Format.OKLAB:
|
|
123
|
+
return Format.OKLAB;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return getColorSpace(formatText);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Returns the `ColorSpace` equivalent from the color space text
|
|
130
|
+
type ColorSpace = Format.SRGB|Format.SRGB_LINEAR|Format.DISPLAY_P3|Format.A98_RGB|Format.PROPHOTO_RGB|
|
|
131
|
+
Format.REC_2020|Format.XYZ|Format.XYZ_D50|Format.XYZ_D65;
|
|
132
|
+
function getColorSpace(colorSpaceText: string): ColorSpace|null {
|
|
133
|
+
switch (colorSpaceText) {
|
|
134
|
+
case Format.SRGB:
|
|
135
|
+
return Format.SRGB;
|
|
136
|
+
case Format.SRGB_LINEAR:
|
|
137
|
+
return Format.SRGB_LINEAR;
|
|
138
|
+
case Format.DISPLAY_P3:
|
|
139
|
+
return Format.DISPLAY_P3;
|
|
140
|
+
case Format.A98_RGB:
|
|
141
|
+
return Format.A98_RGB;
|
|
142
|
+
case Format.PROPHOTO_RGB:
|
|
143
|
+
return Format.PROPHOTO_RGB;
|
|
144
|
+
case Format.REC_2020:
|
|
145
|
+
return Format.REC_2020;
|
|
146
|
+
case Format.XYZ:
|
|
147
|
+
return Format.XYZ;
|
|
148
|
+
case Format.XYZ_D50:
|
|
149
|
+
return Format.XYZ_D50;
|
|
150
|
+
case Format.XYZ_D65:
|
|
151
|
+
return Format.XYZ_D65;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export const enum ColorChannel {
|
|
158
|
+
A = 'a',
|
|
159
|
+
ALPHA = 'alpha',
|
|
160
|
+
B = 'b',
|
|
161
|
+
C = 'c',
|
|
162
|
+
G = 'g',
|
|
163
|
+
H = 'h',
|
|
164
|
+
L = 'l',
|
|
165
|
+
R = 'r',
|
|
166
|
+
S = 's',
|
|
167
|
+
W = 'w',
|
|
168
|
+
X = 'x',
|
|
169
|
+
Y = 'y',
|
|
170
|
+
Z = 'z',
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Percents in color spaces are mapped to ranges.
|
|
175
|
+
* These ranges change based on the syntax.
|
|
176
|
+
* For example, for 'C' in lch() c: 0% = 0, 100% = 150.
|
|
177
|
+
* See: https://www.w3.org/TR/css-color-4/#funcdef-lch
|
|
178
|
+
* Some percentage values can be negative
|
|
179
|
+
* though their ranges don't change depending on the sign
|
|
180
|
+
* (for now, according to spec).
|
|
181
|
+
* @param percent % value of the number. 42 for 42%.
|
|
182
|
+
* @param range Range of [min, max]. Including `min` and `max`.
|
|
183
|
+
*/
|
|
184
|
+
function mapPercentToRange(percent: number, range: [number, number]): number {
|
|
185
|
+
const sign = Math.sign(percent);
|
|
186
|
+
const absPercent = Math.abs(percent);
|
|
187
|
+
const [outMin, outMax] = range;
|
|
188
|
+
|
|
189
|
+
return sign * (absPercent * (outMax - outMin) / 100 + outMin);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
interface SplitColorFunctionParametersOptions {
|
|
193
|
+
allowCommas: boolean;
|
|
194
|
+
convertNoneToZero: boolean;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export function parse(text: string): Color|null {
|
|
198
|
+
// #hex, nickname
|
|
199
|
+
if (!text.match(/\s/)) {
|
|
200
|
+
const match = text.toLowerCase().match(/^(?:#([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})|(\w+))$/i);
|
|
201
|
+
if (match) {
|
|
202
|
+
if (match[1]) {
|
|
203
|
+
return Legacy.fromHex(match[1], text);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if (match[2]) {
|
|
207
|
+
return Nickname.fromName(match[2], text);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return null;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// rgb/rgba(), hsl/hsla(), hwb/hwba(), lch(), oklch(), lab(), oklab() and color()
|
|
215
|
+
const match =
|
|
216
|
+
text.toLowerCase().match(/^\s*(?:(rgba?)|(hsla?)|(hwba?)|(lch)|(oklch)|(lab)|(oklab)|(color))\((.*)\)\s*$/);
|
|
217
|
+
if (match) {
|
|
218
|
+
const isRgbaMatch = Boolean(match[1]); // rgb/rgba()
|
|
219
|
+
const isHslaMatch = Boolean(match[2]); // hsl/hsla()
|
|
220
|
+
const isHwbaMatch = Boolean(match[3]); // hwb/hwba()
|
|
221
|
+
const isLchMatch = Boolean(match[4]); // lch()
|
|
222
|
+
const isOklchMatch = Boolean(match[5]); // oklch()
|
|
223
|
+
const isLabMatch = Boolean(match[6]); // lab()
|
|
224
|
+
const isOklabMatch = Boolean(match[7]); // oklab()
|
|
225
|
+
const isColorMatch = Boolean(match[8]); // color()
|
|
226
|
+
const valuesText = match[9];
|
|
227
|
+
|
|
228
|
+
// Parse color function first because extracting values for
|
|
229
|
+
// this function is not the same as the other ones
|
|
230
|
+
// so, we're not using any of the logic below.
|
|
231
|
+
if (isColorMatch) {
|
|
232
|
+
return ColorFunction.fromSpec(text, valuesText);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const isOldSyntax = isRgbaMatch || isHslaMatch || isHwbaMatch;
|
|
236
|
+
const allowCommas = isRgbaMatch || isHslaMatch;
|
|
237
|
+
const convertNoneToZero = !isOldSyntax; // Convert 'none' keyword to zero in new syntaxes
|
|
238
|
+
|
|
239
|
+
const values = splitColorFunctionParameters(valuesText, {allowCommas, convertNoneToZero});
|
|
240
|
+
if (!values) {
|
|
241
|
+
return null;
|
|
242
|
+
}
|
|
243
|
+
const spec: ColorParameterSpec = [values[0], values[1], values[2], values[3]];
|
|
244
|
+
if (isRgbaMatch) {
|
|
245
|
+
return Legacy.fromRGBAFunction(values[0], values[1], values[2], values[3], text);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
if (isHslaMatch) {
|
|
249
|
+
return HSL.fromSpec(spec, text);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
if (isHwbaMatch) {
|
|
253
|
+
return HWB.fromSpec(spec, text);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
if (isLchMatch) {
|
|
257
|
+
return LCH.fromSpec(spec, text);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
if (isOklchMatch) {
|
|
261
|
+
return Oklch.fromSpec(spec, text);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
if (isLabMatch) {
|
|
265
|
+
return Lab.fromSpec(spec, text);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if (isOklabMatch) {
|
|
269
|
+
return Oklab.fromSpec(spec, text);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
return null;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Split the color parameters of (e.g.) rgb(a), hsl(a), hwb(a) functions.
|
|
278
|
+
*/
|
|
279
|
+
function splitColorFunctionParameters(
|
|
280
|
+
content: string, {allowCommas, convertNoneToZero}: SplitColorFunctionParametersOptions): string[]|null {
|
|
281
|
+
const components = content.trim();
|
|
282
|
+
let values: string[] = [];
|
|
283
|
+
|
|
284
|
+
if (allowCommas) {
|
|
285
|
+
values = components.split(/\s*,\s*/);
|
|
286
|
+
}
|
|
287
|
+
if (!allowCommas || values.length === 1) {
|
|
288
|
+
values = components.split(/\s+/);
|
|
289
|
+
if (values[3] === '/') {
|
|
290
|
+
values.splice(3, 1);
|
|
291
|
+
if (values.length !== 4) {
|
|
292
|
+
return null;
|
|
293
|
+
}
|
|
294
|
+
} else if (
|
|
295
|
+
(values.length > 2 && values[2].indexOf('/') !== -1) || (values.length > 3 && values[3].indexOf('/') !== -1)) {
|
|
296
|
+
const alpha = values.slice(2, 4).join('');
|
|
297
|
+
values = values.slice(0, 2).concat(alpha.split(/\//)).concat(values.slice(4));
|
|
298
|
+
} else if (values.length >= 4) {
|
|
299
|
+
return null;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
if (values.length !== 3 && values.length !== 4 || values.indexOf('') > -1) {
|
|
303
|
+
return null;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// Question: what should we do with `alpha` being none?
|
|
307
|
+
if (convertNoneToZero) {
|
|
308
|
+
return values.map(value => value === 'none' ? '0' : value);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
return values;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
function clamp(value: number, {min, max}: {min?: number, max?: number}): number;
|
|
315
|
+
function clamp(value: null, {min, max}: {min?: number, max?: number}): null;
|
|
316
|
+
function clamp(value: number|null, {min, max}: {min?: number, max?: number}): number|null;
|
|
317
|
+
function clamp(value: number|null, {min, max}: {min?: number, max?: number}): number|null {
|
|
318
|
+
if (value === null) {
|
|
319
|
+
return value;
|
|
320
|
+
}
|
|
321
|
+
if (min !== undefined) {
|
|
322
|
+
value = Math.max(value, min);
|
|
323
|
+
}
|
|
324
|
+
if (max !== undefined) {
|
|
325
|
+
value = Math.min(value, max);
|
|
326
|
+
}
|
|
327
|
+
return value;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
function parsePercentage(value: string, range: [number, number]): number|null {
|
|
331
|
+
if (!value.endsWith('%')) {
|
|
332
|
+
return null;
|
|
333
|
+
}
|
|
334
|
+
const percentage = parseFloat(value.substr(0, value.length - 1));
|
|
335
|
+
return isNaN(percentage) ? null : mapPercentToRange(percentage, range);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
function parseNumber(value: string): number|null {
|
|
339
|
+
const number = parseFloat(value);
|
|
340
|
+
return isNaN(number) ? null : number;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
function parseAlpha(value: string|undefined): number|null {
|
|
344
|
+
if (value === undefined) {
|
|
345
|
+
return null;
|
|
346
|
+
}
|
|
347
|
+
return clamp(parsePercentage(value, [0, 1]) ?? parseNumber(value), {min: 0, max: 1});
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
*
|
|
352
|
+
* @param value Text value to be parsed in the form of 'number|percentage'.
|
|
353
|
+
* @param range Range to map the percentage.
|
|
354
|
+
* @returns If it is not percentage, returns number directly; otherwise,
|
|
355
|
+
* maps the percentage to the range. For example:
|
|
356
|
+
* - 30% in range [0, 100] is 30
|
|
357
|
+
* - 20% in range [0, 1] is 0.5
|
|
358
|
+
*/
|
|
359
|
+
function parsePercentOrNumber(value: string, range: [number, number] = [0, 1]): number|null {
|
|
360
|
+
// @ts-expect-error: isNaN can accept strings
|
|
361
|
+
if (isNaN(value.replace('%', ''))) {
|
|
362
|
+
return null;
|
|
363
|
+
}
|
|
364
|
+
const parsed = parseFloat(value);
|
|
365
|
+
|
|
366
|
+
if (value.indexOf('%') !== -1) {
|
|
367
|
+
if (value.indexOf('%') !== value.length - 1) {
|
|
368
|
+
return null;
|
|
369
|
+
}
|
|
370
|
+
return mapPercentToRange(parsed, range);
|
|
371
|
+
}
|
|
372
|
+
return parsed;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
function parseRgbNumeric(value: string): number|null {
|
|
376
|
+
const parsed = parsePercentOrNumber(value);
|
|
377
|
+
if (parsed === null) {
|
|
378
|
+
return null;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
if (value.indexOf('%') !== -1) {
|
|
382
|
+
return parsed;
|
|
383
|
+
}
|
|
384
|
+
return parsed / 255;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
export function parseHueNumeric(value: string): number|null {
|
|
388
|
+
const angle = value.replace(/(deg|g?rad|turn)$/, '');
|
|
389
|
+
// @ts-expect-error: isNaN can accept strings
|
|
390
|
+
if (isNaN(angle) || value.match(/\s+(deg|g?rad|turn)/)) {
|
|
391
|
+
return null;
|
|
392
|
+
}
|
|
393
|
+
const number = parseFloat(angle);
|
|
394
|
+
|
|
395
|
+
if (value.indexOf('turn') !== -1) {
|
|
396
|
+
return number % 1;
|
|
397
|
+
}
|
|
398
|
+
if (value.indexOf('grad') !== -1) {
|
|
399
|
+
return (number / 400) % 1;
|
|
400
|
+
}
|
|
401
|
+
if (value.indexOf('rad') !== -1) {
|
|
402
|
+
return (number / (2 * Math.PI)) % 1;
|
|
403
|
+
}
|
|
404
|
+
return (number / 360) % 1;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
function parseSatLightNumeric(value: string): number|null {
|
|
408
|
+
// @ts-expect-error: isNaN can accept strings
|
|
409
|
+
if (value.indexOf('%') !== value.length - 1 || isNaN(value.replace('%', ''))) {
|
|
410
|
+
return null;
|
|
411
|
+
}
|
|
412
|
+
const parsed = parseFloat(value);
|
|
413
|
+
return parsed / 100;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
function parseAlphaNumeric(value: string): number|null {
|
|
417
|
+
return parsePercentOrNumber(value);
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
function hsva2hsla(hsva: Color4D): Color4D {
|
|
421
|
+
const h = hsva[0];
|
|
422
|
+
let s: 0|number = hsva[1];
|
|
423
|
+
const v = hsva[2];
|
|
424
|
+
|
|
425
|
+
const t = (2 - s) * v;
|
|
426
|
+
if (v === 0 || s === 0) {
|
|
427
|
+
s = 0;
|
|
428
|
+
} else {
|
|
429
|
+
s *= v / (t < 1 ? t : 2 - t);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
return [h, s, t / 2, hsva[3]];
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
export function hsl2rgb(hsl: Color4D): Color4D {
|
|
436
|
+
const h = hsl[0];
|
|
437
|
+
let s: 0|number = hsl[1];
|
|
438
|
+
const l = hsl[2];
|
|
439
|
+
|
|
440
|
+
function hue2rgb(p: number, q: number, h: number): number {
|
|
441
|
+
if (h < 0) {
|
|
442
|
+
h += 1;
|
|
443
|
+
} else if (h > 1) {
|
|
444
|
+
h -= 1;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
if ((h * 6) < 1) {
|
|
448
|
+
return p + (q - p) * h * 6;
|
|
449
|
+
}
|
|
450
|
+
if ((h * 2) < 1) {
|
|
451
|
+
return q;
|
|
452
|
+
}
|
|
453
|
+
if ((h * 3) < 2) {
|
|
454
|
+
return p + (q - p) * ((2 / 3) - h) * 6;
|
|
455
|
+
}
|
|
456
|
+
return p;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
if (s < 0) {
|
|
460
|
+
s = 0;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
let q;
|
|
464
|
+
if (l <= 0.5) {
|
|
465
|
+
q = l * (1 + s);
|
|
466
|
+
} else {
|
|
467
|
+
q = l + s - (l * s);
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
const p = 2 * l - q;
|
|
471
|
+
|
|
472
|
+
const tr = h + (1 / 3);
|
|
473
|
+
const tg = h;
|
|
474
|
+
const tb = h - (1 / 3);
|
|
475
|
+
|
|
476
|
+
return [hue2rgb(p, q, tr), hue2rgb(p, q, tg), hue2rgb(p, q, tb), hsl[3]];
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
function hwb2rgb(hwb: Color4D): Color4D {
|
|
480
|
+
const h = hwb[0];
|
|
481
|
+
const w = hwb[1];
|
|
482
|
+
const b = hwb[2];
|
|
483
|
+
|
|
484
|
+
const whiteRatio = w / (w + b);
|
|
485
|
+
let result: Color4D = [whiteRatio, whiteRatio, whiteRatio, hwb[3]];
|
|
486
|
+
|
|
487
|
+
if (w + b < 1) {
|
|
488
|
+
result = hsl2rgb([h, 1, 0.5, hwb[3]]);
|
|
489
|
+
for (let i = 0; i < 3; ++i) {
|
|
490
|
+
result[i] += w - (w + b) * result[i];
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
return result;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
export function hsva2rgba(hsva: Color4D): Color4D {
|
|
498
|
+
return hsl2rgb(hsva2hsla(hsva));
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
export function rgb2hsv(rgba: Color3D): Color3D {
|
|
502
|
+
const hsla = rgbToHsl(rgba);
|
|
503
|
+
const h = hsla[0];
|
|
504
|
+
let s = hsla[1];
|
|
505
|
+
const l = hsla[2];
|
|
506
|
+
|
|
507
|
+
s *= l < 0.5 ? l : 1 - l;
|
|
508
|
+
return [h, s !== 0 ? 2 * s / (l + s) : 0, (l + s)];
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
/**
|
|
512
|
+
* Compute a desired luminance given a given luminance and a desired contrast
|
|
513
|
+
* ratio.
|
|
514
|
+
*/
|
|
515
|
+
export function desiredLuminance(luminance: number, contrast: number, lighter: boolean): number {
|
|
516
|
+
function computeLuminance(): number {
|
|
517
|
+
if (lighter) {
|
|
518
|
+
return (luminance + 0.05) * contrast - 0.05;
|
|
519
|
+
}
|
|
520
|
+
return (luminance + 0.05) / contrast - 0.05;
|
|
521
|
+
}
|
|
522
|
+
let desiredLuminance = computeLuminance();
|
|
523
|
+
if (desiredLuminance < 0 || desiredLuminance > 1) {
|
|
524
|
+
lighter = !lighter;
|
|
525
|
+
desiredLuminance = computeLuminance();
|
|
526
|
+
}
|
|
527
|
+
return desiredLuminance;
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
/**
|
|
531
|
+
* Approach a value of the given component of `candidateHSVA` such that the
|
|
532
|
+
* calculated luminance of `candidateHSVA` approximates `desiredLuminance`.
|
|
533
|
+
*/
|
|
534
|
+
export function approachColorValue(
|
|
535
|
+
candidateHSVA: Color4D, index: number, desiredLuminance: number,
|
|
536
|
+
candidateLuminance: (arg0: Color4D) => number): number|null {
|
|
537
|
+
const epsilon = 0.0002;
|
|
538
|
+
|
|
539
|
+
let x = candidateHSVA[index];
|
|
540
|
+
let multiplier = 1;
|
|
541
|
+
let dLuminance: number = candidateLuminance(candidateHSVA) - desiredLuminance;
|
|
542
|
+
let previousSign = Math.sign(dLuminance);
|
|
543
|
+
|
|
544
|
+
for (let guard = 100; guard; guard--) {
|
|
545
|
+
if (Math.abs(dLuminance) < epsilon) {
|
|
546
|
+
candidateHSVA[index] = x;
|
|
547
|
+
return x;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
const sign = Math.sign(dLuminance);
|
|
551
|
+
if (sign !== previousSign) {
|
|
552
|
+
// If `x` overshoots the correct value, halve the step size.
|
|
553
|
+
multiplier /= 2;
|
|
554
|
+
previousSign = sign;
|
|
555
|
+
} else if (x < 0 || x > 1) {
|
|
556
|
+
// If there is no overshoot and `x` is out of bounds, there is no
|
|
557
|
+
// acceptable value for `x`.
|
|
558
|
+
return null;
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
// Adjust `x` by a multiple of `dLuminance` to decrease step size as
|
|
562
|
+
// the computed luminance converges on `desiredLuminance`.
|
|
563
|
+
x += multiplier * (index === 2 ? -dLuminance : dLuminance);
|
|
564
|
+
|
|
565
|
+
candidateHSVA[index] = x;
|
|
566
|
+
|
|
567
|
+
dLuminance = candidateLuminance(candidateHSVA) - desiredLuminance;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
return null;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
export function findFgColorForContrast(fgColor: Legacy, bgColor: Legacy, requiredContrast: number): Legacy|null {
|
|
574
|
+
const candidateHSVA = fgColor.as(Format.HSL).hsva();
|
|
575
|
+
const bgRGBA = bgColor.rgba();
|
|
576
|
+
|
|
577
|
+
const candidateLuminance = (candidateHSVA: Color4D): number => {
|
|
578
|
+
return luminance(blendColors(Legacy.fromHSVA(candidateHSVA).rgba(), bgRGBA));
|
|
579
|
+
};
|
|
580
|
+
|
|
581
|
+
const bgLuminance = luminance(bgColor.rgba());
|
|
582
|
+
const fgLuminance = candidateLuminance(candidateHSVA);
|
|
583
|
+
const fgIsLighter = fgLuminance > bgLuminance;
|
|
584
|
+
|
|
585
|
+
const desired = desiredLuminance(bgLuminance, requiredContrast, fgIsLighter);
|
|
586
|
+
|
|
587
|
+
const saturationComponentIndex = 1;
|
|
588
|
+
const valueComponentIndex = 2;
|
|
589
|
+
|
|
590
|
+
if (approachColorValue(candidateHSVA, valueComponentIndex, desired, candidateLuminance)) {
|
|
591
|
+
return Legacy.fromHSVA(candidateHSVA);
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
candidateHSVA[valueComponentIndex] = 1;
|
|
595
|
+
if (approachColorValue(candidateHSVA, saturationComponentIndex, desired, candidateLuminance)) {
|
|
596
|
+
return Legacy.fromHSVA(candidateHSVA);
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
return null;
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
export function findFgColorForContrastAPCA(fgColor: Legacy, bgColor: Legacy, requiredContrast: number): Legacy|null {
|
|
603
|
+
const candidateHSVA = fgColor.as(Format.HSL).hsva();
|
|
604
|
+
|
|
605
|
+
const candidateLuminance = (candidateHSVA: Color4D): number => {
|
|
606
|
+
return luminanceAPCA(Legacy.fromHSVA(candidateHSVA).rgba());
|
|
607
|
+
};
|
|
608
|
+
|
|
609
|
+
const bgLuminance = luminanceAPCA(bgColor.rgba());
|
|
610
|
+
const fgLuminance = candidateLuminance(candidateHSVA);
|
|
611
|
+
const fgIsLighter = fgLuminance >= bgLuminance;
|
|
612
|
+
const desiredLuminance = desiredLuminanceAPCA(bgLuminance, requiredContrast, fgIsLighter);
|
|
613
|
+
|
|
614
|
+
const saturationComponentIndex = 1;
|
|
615
|
+
const valueComponentIndex = 2;
|
|
616
|
+
|
|
617
|
+
if (approachColorValue(candidateHSVA, valueComponentIndex, desiredLuminance, candidateLuminance)) {
|
|
618
|
+
const candidate = Legacy.fromHSVA(candidateHSVA);
|
|
619
|
+
if (Math.abs(contrastRatioAPCA(bgColor.rgba(), candidate.rgba())) >= requiredContrast) {
|
|
620
|
+
return candidate;
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
candidateHSVA[valueComponentIndex] = 1;
|
|
625
|
+
if (approachColorValue(candidateHSVA, saturationComponentIndex, desiredLuminance, candidateLuminance)) {
|
|
626
|
+
const candidate = Legacy.fromHSVA(candidateHSVA);
|
|
627
|
+
if (Math.abs(contrastRatioAPCA(bgColor.rgba(), candidate.rgba())) >= requiredContrast) {
|
|
628
|
+
return candidate;
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
return null;
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
type ColorParameterSpec = [string, string, string, string | undefined];
|
|
636
|
+
|
|
637
|
+
interface ColorConversions<T = void> {
|
|
638
|
+
[Format.HEX](self: T): Legacy;
|
|
639
|
+
[Format.HEXA](self: T): Legacy;
|
|
640
|
+
[Format.RGB](self: T): Legacy;
|
|
641
|
+
[Format.RGBA](self: T): Legacy;
|
|
642
|
+
[Format.HSL](self: T): HSL;
|
|
643
|
+
[Format.HSLA](self: T): HSL;
|
|
644
|
+
[Format.HWB](self: T): HWB;
|
|
645
|
+
[Format.HWBA](self: T): HWB;
|
|
646
|
+
[Format.LCH](self: T): LCH;
|
|
647
|
+
[Format.OKLCH](self: T): Oklch;
|
|
648
|
+
[Format.LAB](self: T): Lab;
|
|
649
|
+
[Format.OKLAB](self: T): Oklab;
|
|
650
|
+
|
|
651
|
+
[Format.SRGB](self: T): ColorFunction;
|
|
652
|
+
[Format.SRGB_LINEAR](self: T): ColorFunction;
|
|
653
|
+
[Format.DISPLAY_P3](self: T): ColorFunction;
|
|
654
|
+
[Format.A98_RGB](self: T): ColorFunction;
|
|
655
|
+
[Format.PROPHOTO_RGB](self: T): ColorFunction;
|
|
656
|
+
[Format.REC_2020](self: T): ColorFunction;
|
|
657
|
+
[Format.XYZ](self: T): ColorFunction;
|
|
658
|
+
[Format.XYZ_D50](self: T): ColorFunction;
|
|
659
|
+
[Format.XYZ_D65](self: T): ColorFunction;
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
export interface Color {
|
|
663
|
+
readonly alpha: number|null;
|
|
664
|
+
readonly channels: [ColorChannel, ColorChannel, ColorChannel, ColorChannel];
|
|
665
|
+
|
|
666
|
+
equal(color: Color): boolean;
|
|
667
|
+
asString(format?: Format): string;
|
|
668
|
+
setAlpha(alpha: number): Color;
|
|
669
|
+
format(): Format;
|
|
670
|
+
as<T extends Format>(format: T): ReturnType<ColorConversions[T]>;
|
|
671
|
+
is<T extends Format>(format: T): this is ReturnType<ColorConversions[T]>;
|
|
672
|
+
asLegacyColor(): Legacy;
|
|
673
|
+
|
|
674
|
+
// The authored text is the text that was used to define the color. If set, it may be different from what `asString`
|
|
675
|
+
// returns, for example if the latter normalizes or clamps color channel values. It is also possible that the authored
|
|
676
|
+
// text is not a parsable color outside of the context in which the color was produced, e.g., when the color stems
|
|
677
|
+
// from a custom property, the authored text may look like "var(--color)".
|
|
678
|
+
getAuthoredText(): string|null;
|
|
679
|
+
|
|
680
|
+
getRawParameters(): Color3D;
|
|
681
|
+
getAsRawString(format?: Format): string;
|
|
682
|
+
isGamutClipped(): boolean;
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
const EPSILON = 0.01;
|
|
686
|
+
const WIDE_RANGE_EPSILON = 1; // For comparisons on channels with a wider range than [0,1]
|
|
687
|
+
function equals(a: number[], b: number[], accuracy?: number): boolean;
|
|
688
|
+
function equals(a: number|null, b: number|null, accuracy?: number): boolean;
|
|
689
|
+
function equals(a: number|null|number[], b: number|null|number[], accuracy = EPSILON): boolean {
|
|
690
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
691
|
+
if (a.length !== b.length) {
|
|
692
|
+
return false;
|
|
693
|
+
}
|
|
694
|
+
for (const i in a) {
|
|
695
|
+
if (!equals(a[i], b[i])) {
|
|
696
|
+
return false;
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
return true;
|
|
700
|
+
}
|
|
701
|
+
if (Array.isArray(a) || Array.isArray(b)) {
|
|
702
|
+
return false;
|
|
703
|
+
}
|
|
704
|
+
if (a === null || b === null) {
|
|
705
|
+
return a === b;
|
|
706
|
+
}
|
|
707
|
+
return Math.abs(a - b) < accuracy;
|
|
708
|
+
}
|
|
709
|
+
function lessOrEquals(a: number, b: number, accuracy = EPSILON): boolean {
|
|
710
|
+
return a - b <= accuracy;
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
export const enum Format {
|
|
714
|
+
HEX = 'hex',
|
|
715
|
+
HEXA = 'hexa',
|
|
716
|
+
RGB = 'rgb',
|
|
717
|
+
RGBA = 'rgba',
|
|
718
|
+
HSL = 'hsl',
|
|
719
|
+
HSLA = 'hsla',
|
|
720
|
+
HWB = 'hwb',
|
|
721
|
+
HWBA = 'hwba',
|
|
722
|
+
LCH = 'lch',
|
|
723
|
+
OKLCH = 'oklch',
|
|
724
|
+
LAB = 'lab',
|
|
725
|
+
OKLAB = 'oklab',
|
|
726
|
+
SRGB = 'srgb',
|
|
727
|
+
SRGB_LINEAR = 'srgb-linear',
|
|
728
|
+
DISPLAY_P3 = 'display-p3',
|
|
729
|
+
A98_RGB = 'a98-rgb',
|
|
730
|
+
PROPHOTO_RGB = 'prophoto-rgb',
|
|
731
|
+
REC_2020 = 'rec2020',
|
|
732
|
+
XYZ = 'xyz',
|
|
733
|
+
XYZ_D50 = 'xyz-d50',
|
|
734
|
+
XYZ_D65 = 'xyz-d65',
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
export class Lab implements Color {
|
|
738
|
+
readonly l: number;
|
|
739
|
+
readonly a: number;
|
|
740
|
+
readonly b: number;
|
|
741
|
+
readonly alpha: number|null;
|
|
742
|
+
readonly #authoredText?: string;
|
|
743
|
+
readonly #rawParams: Color3D;
|
|
744
|
+
readonly channels: [ColorChannel, ColorChannel, ColorChannel, ColorChannel] =
|
|
745
|
+
[ColorChannel.L, ColorChannel.A, ColorChannel.B, ColorChannel.ALPHA];
|
|
746
|
+
|
|
747
|
+
static readonly #conversions: ColorConversions<Lab> = {
|
|
748
|
+
[Format.HEX]: (self: Lab) => new Legacy(self.#getRGBArray(/* withAlpha= */ false), Format.HEX),
|
|
749
|
+
[Format.HEXA]: (self: Lab) => new Legacy(self.#getRGBArray(/* withAlpha= */ true), Format.HEXA),
|
|
750
|
+
[Format.RGB]: (self: Lab) => new Legacy(self.#getRGBArray(/* withAlpha= */ false), Format.RGB),
|
|
751
|
+
[Format.RGBA]: (self: Lab) => new Legacy(self.#getRGBArray(/* withAlpha= */ true), Format.RGBA),
|
|
752
|
+
[Format.HSL]: (self: Lab) => new HSL(...rgbToHsl(self.#getRGBArray(/* withAlpha= */ false)), self.alpha),
|
|
753
|
+
[Format.HSLA]: (self: Lab) => new HSL(...rgbToHsl(self.#getRGBArray(/* withAlpha= */ false)), self.alpha),
|
|
754
|
+
[Format.HWB]: (self: Lab) => new HWB(...rgbToHwb(self.#getRGBArray(/* withAlpha= */ false)), self.alpha),
|
|
755
|
+
[Format.HWBA]: (self: Lab) => new HWB(...rgbToHwb(self.#getRGBArray(/* withAlpha= */ false)), self.alpha),
|
|
756
|
+
[Format.LCH]: (self: Lab) => new LCH(...ColorConverter.labToLch(self.l, self.a, self.b), self.alpha),
|
|
757
|
+
[Format.OKLCH]: (self: Lab) => new Oklch(...ColorConverter.xyzd50ToOklch(...self.#toXyzd50()), self.alpha),
|
|
758
|
+
[Format.LAB]: (self: Lab) => self,
|
|
759
|
+
[Format.OKLAB]: (self: Lab) =>
|
|
760
|
+
new Oklab(...ColorConverter.xyzd65ToOklab(...ColorConverter.xyzd50ToD65(...self.#toXyzd50())), self.alpha),
|
|
761
|
+
|
|
762
|
+
[Format.SRGB]: (self: Lab) =>
|
|
763
|
+
new ColorFunction(Format.SRGB, ...ColorConverter.xyzd50ToSrgb(...self.#toXyzd50()), self.alpha),
|
|
764
|
+
[Format.SRGB_LINEAR]: (self: Lab) =>
|
|
765
|
+
new ColorFunction(Format.SRGB_LINEAR, ...ColorConverter.xyzd50TosRGBLinear(...self.#toXyzd50()), self.alpha),
|
|
766
|
+
[Format.DISPLAY_P3]: (self: Lab) =>
|
|
767
|
+
new ColorFunction(Format.DISPLAY_P3, ...ColorConverter.xyzd50ToDisplayP3(...self.#toXyzd50()), self.alpha),
|
|
768
|
+
[Format.A98_RGB]: (self: Lab) =>
|
|
769
|
+
new ColorFunction(Format.A98_RGB, ...ColorConverter.xyzd50ToAdobeRGB(...self.#toXyzd50()), self.alpha),
|
|
770
|
+
[Format.PROPHOTO_RGB]: (self: Lab) =>
|
|
771
|
+
new ColorFunction(Format.PROPHOTO_RGB, ...ColorConverter.xyzd50ToProPhoto(...self.#toXyzd50()), self.alpha),
|
|
772
|
+
[Format.REC_2020]: (self: Lab) =>
|
|
773
|
+
new ColorFunction(Format.REC_2020, ...ColorConverter.xyzd50ToRec2020(...self.#toXyzd50()), self.alpha),
|
|
774
|
+
[Format.XYZ]: (self: Lab) =>
|
|
775
|
+
new ColorFunction(Format.XYZ, ...ColorConverter.xyzd50ToD65(...self.#toXyzd50()), self.alpha),
|
|
776
|
+
[Format.XYZ_D50]: (self: Lab) => new ColorFunction(Format.XYZ_D50, ...self.#toXyzd50(), self.alpha),
|
|
777
|
+
[Format.XYZ_D65]: (self: Lab) =>
|
|
778
|
+
new ColorFunction(Format.XYZ_D65, ...ColorConverter.xyzd50ToD65(...self.#toXyzd50()), self.alpha),
|
|
779
|
+
};
|
|
780
|
+
|
|
781
|
+
#toXyzd50(): Color3D {
|
|
782
|
+
return ColorConverter.labToXyzd50(this.l, this.a, this.b);
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
#getRGBArray(withAlpha: true): Color4DOr3D;
|
|
786
|
+
#getRGBArray(withAlpha: false): Color3D;
|
|
787
|
+
#getRGBArray(withAlpha = true): Color3D|Color4DOr3D {
|
|
788
|
+
const params = ColorConverter.xyzd50ToSrgb(...this.#toXyzd50());
|
|
789
|
+
if (withAlpha) {
|
|
790
|
+
return [...params, this.alpha ?? undefined];
|
|
791
|
+
}
|
|
792
|
+
return params;
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
constructor(l: number, a: number, b: number, alpha: number|null, authoredText?: string|undefined) {
|
|
796
|
+
this.#rawParams = [l, a, b];
|
|
797
|
+
this.l = clamp(l, {min: 0, max: 100});
|
|
798
|
+
if (equals(this.l, 0, WIDE_RANGE_EPSILON) || equals(this.l, 100, WIDE_RANGE_EPSILON)) {
|
|
799
|
+
a = b = 0;
|
|
800
|
+
}
|
|
801
|
+
this.a = a;
|
|
802
|
+
this.b = b;
|
|
803
|
+
this.alpha = clamp(alpha, {min: 0, max: 1});
|
|
804
|
+
this.#authoredText = authoredText;
|
|
805
|
+
}
|
|
806
|
+
is<T extends Format>(format: T): this is ReturnType<ColorConversions[T]> {
|
|
807
|
+
return format === this.format();
|
|
808
|
+
}
|
|
809
|
+
as<T extends Format>(format: T): ReturnType<ColorConversions[T]> {
|
|
810
|
+
return Lab.#conversions[format](this) as ReturnType<ColorConversions[T]>;
|
|
811
|
+
}
|
|
812
|
+
asLegacyColor(): Legacy {
|
|
813
|
+
return this.as(Format.RGBA);
|
|
814
|
+
}
|
|
815
|
+
equal(color: Color): boolean {
|
|
816
|
+
const lab = color.as(Format.LAB);
|
|
817
|
+
return equals(lab.l, this.l, WIDE_RANGE_EPSILON) && equals(lab.a, this.a) && equals(lab.b, this.b) &&
|
|
818
|
+
equals(lab.alpha, this.alpha);
|
|
819
|
+
}
|
|
820
|
+
format(): Format {
|
|
821
|
+
return Format.LAB;
|
|
822
|
+
}
|
|
823
|
+
setAlpha(alpha: number): Lab {
|
|
824
|
+
return new Lab(this.l, this.a, this.b, alpha, undefined);
|
|
825
|
+
}
|
|
826
|
+
asString(format?: Format): string {
|
|
827
|
+
if (format) {
|
|
828
|
+
return this.as(format).asString();
|
|
829
|
+
}
|
|
830
|
+
return this.#stringify(this.l, this.a, this.b);
|
|
831
|
+
}
|
|
832
|
+
#stringify(l: number, a: number, b: number): string {
|
|
833
|
+
const alpha = this.alpha === null || equals(this.alpha, 1) ?
|
|
834
|
+
'' :
|
|
835
|
+
` / ${Platform.StringUtilities.stringifyWithPrecision(this.alpha)}`;
|
|
836
|
+
return `lab(${Platform.StringUtilities.stringifyWithPrecision(l, 0)} ${
|
|
837
|
+
Platform.StringUtilities.stringifyWithPrecision(
|
|
838
|
+
a)} ${Platform.StringUtilities.stringifyWithPrecision(b)}${alpha})`;
|
|
839
|
+
}
|
|
840
|
+
getAuthoredText(): string|null {
|
|
841
|
+
return this.#authoredText ?? null;
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
getRawParameters(): Color3D {
|
|
845
|
+
return [...this.#rawParams];
|
|
846
|
+
}
|
|
847
|
+
getAsRawString(format?: Format): string {
|
|
848
|
+
if (format) {
|
|
849
|
+
return this.as(format).getAsRawString();
|
|
850
|
+
}
|
|
851
|
+
return this.#stringify(...this.#rawParams);
|
|
852
|
+
}
|
|
853
|
+
isGamutClipped(): boolean {
|
|
854
|
+
return false;
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
static fromSpec(spec: ColorParameterSpec, text: string): Lab|null {
|
|
858
|
+
const L = parsePercentage(spec[0], [0, 100]) ?? parseNumber(spec[0]);
|
|
859
|
+
if (L === null) {
|
|
860
|
+
return null;
|
|
861
|
+
}
|
|
862
|
+
const a = parsePercentage(spec[1], [0, 125]) ?? parseNumber(spec[1]);
|
|
863
|
+
if (a === null) {
|
|
864
|
+
return null;
|
|
865
|
+
}
|
|
866
|
+
const b = parsePercentage(spec[2], [0, 125]) ?? parseNumber(spec[2]);
|
|
867
|
+
if (b === null) {
|
|
868
|
+
return null;
|
|
869
|
+
}
|
|
870
|
+
const alpha = parseAlpha(spec[3]);
|
|
871
|
+
|
|
872
|
+
return new Lab(L, a, b, alpha, text);
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
export class LCH implements Color {
|
|
877
|
+
readonly #rawParams: Color3D;
|
|
878
|
+
readonly l: number;
|
|
879
|
+
readonly c: number;
|
|
880
|
+
readonly h: number;
|
|
881
|
+
readonly alpha: number|null;
|
|
882
|
+
readonly #authoredText?: string;
|
|
883
|
+
readonly channels: [ColorChannel, ColorChannel, ColorChannel, ColorChannel] =
|
|
884
|
+
[ColorChannel.L, ColorChannel.C, ColorChannel.H, ColorChannel.ALPHA];
|
|
885
|
+
|
|
886
|
+
static readonly #conversions: ColorConversions<LCH> = {
|
|
887
|
+
[Format.HEX]: (self: LCH) => new Legacy(self.#getRGBArray(/* withAlpha= */ false), Format.HEX),
|
|
888
|
+
[Format.HEXA]: (self: LCH) => new Legacy(self.#getRGBArray(/* withAlpha= */ true), Format.HEXA),
|
|
889
|
+
[Format.RGB]: (self: LCH) => new Legacy(self.#getRGBArray(/* withAlpha= */ false), Format.RGB),
|
|
890
|
+
[Format.RGBA]: (self: LCH) => new Legacy(self.#getRGBArray(/* withAlpha= */ true), Format.RGBA),
|
|
891
|
+
[Format.HSL]: (self: LCH) => new HSL(...rgbToHsl(self.#getRGBArray(/* withAlpha= */ false)), self.alpha),
|
|
892
|
+
[Format.HSLA]: (self: LCH) => new HSL(...rgbToHsl(self.#getRGBArray(/* withAlpha= */ false)), self.alpha),
|
|
893
|
+
[Format.HWB]: (self: LCH) => new HWB(...rgbToHwb(self.#getRGBArray(/* withAlpha= */ false)), self.alpha),
|
|
894
|
+
[Format.HWBA]: (self: LCH) => new HWB(...rgbToHwb(self.#getRGBArray(/* withAlpha= */ false)), self.alpha),
|
|
895
|
+
[Format.LCH]: (self: LCH) => self,
|
|
896
|
+
[Format.OKLCH]: (self: LCH) => new Oklch(...ColorConverter.xyzd50ToOklch(...self.#toXyzd50()), self.alpha),
|
|
897
|
+
[Format.LAB]: (self: LCH) => new Lab(...ColorConverter.lchToLab(self.l, self.c, self.h), self.alpha),
|
|
898
|
+
[Format.OKLAB]: (self: LCH) =>
|
|
899
|
+
new Oklab(...ColorConverter.xyzd65ToOklab(...ColorConverter.xyzd50ToD65(...self.#toXyzd50())), self.alpha),
|
|
900
|
+
|
|
901
|
+
[Format.SRGB]: (self: LCH) =>
|
|
902
|
+
new ColorFunction(Format.SRGB, ...ColorConverter.xyzd50ToSrgb(...self.#toXyzd50()), self.alpha),
|
|
903
|
+
[Format.SRGB_LINEAR]: (self: LCH) =>
|
|
904
|
+
new ColorFunction(Format.SRGB_LINEAR, ...ColorConverter.xyzd50TosRGBLinear(...self.#toXyzd50()), self.alpha),
|
|
905
|
+
[Format.DISPLAY_P3]: (self: LCH) =>
|
|
906
|
+
new ColorFunction(Format.DISPLAY_P3, ...ColorConverter.xyzd50ToDisplayP3(...self.#toXyzd50()), self.alpha),
|
|
907
|
+
[Format.A98_RGB]: (self: LCH) =>
|
|
908
|
+
new ColorFunction(Format.A98_RGB, ...ColorConverter.xyzd50ToAdobeRGB(...self.#toXyzd50()), self.alpha),
|
|
909
|
+
[Format.PROPHOTO_RGB]: (self: LCH) =>
|
|
910
|
+
new ColorFunction(Format.PROPHOTO_RGB, ...ColorConverter.xyzd50ToProPhoto(...self.#toXyzd50()), self.alpha),
|
|
911
|
+
[Format.REC_2020]: (self: LCH) =>
|
|
912
|
+
new ColorFunction(Format.REC_2020, ...ColorConverter.xyzd50ToRec2020(...self.#toXyzd50()), self.alpha),
|
|
913
|
+
[Format.XYZ]: (self: LCH) =>
|
|
914
|
+
new ColorFunction(Format.XYZ, ...ColorConverter.xyzd50ToD65(...self.#toXyzd50()), self.alpha),
|
|
915
|
+
[Format.XYZ_D50]: (self: LCH) => new ColorFunction(Format.XYZ_D50, ...self.#toXyzd50(), self.alpha),
|
|
916
|
+
[Format.XYZ_D65]: (self: LCH) =>
|
|
917
|
+
new ColorFunction(Format.XYZ_D65, ...ColorConverter.xyzd50ToD65(...self.#toXyzd50()), self.alpha),
|
|
918
|
+
};
|
|
919
|
+
|
|
920
|
+
#toXyzd50(): Color3D {
|
|
921
|
+
return ColorConverter.labToXyzd50(...ColorConverter.lchToLab(this.l, this.c, this.h));
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
#getRGBArray(withAlpha: true): Color4DOr3D;
|
|
925
|
+
#getRGBArray(withAlpha: false): Color3D;
|
|
926
|
+
#getRGBArray(withAlpha = true): Color4DOr3D|Color3D {
|
|
927
|
+
const params = ColorConverter.xyzd50ToSrgb(...this.#toXyzd50());
|
|
928
|
+
if (withAlpha) {
|
|
929
|
+
return [...params, this.alpha ?? undefined];
|
|
930
|
+
}
|
|
931
|
+
return params;
|
|
932
|
+
}
|
|
933
|
+
|
|
934
|
+
constructor(l: number, c: number, h: number, alpha: number|null, authoredText?: string|undefined) {
|
|
935
|
+
this.#rawParams = [l, c, h];
|
|
936
|
+
this.l = clamp(l, {min: 0, max: 100});
|
|
937
|
+
c = equals(this.l, 0, WIDE_RANGE_EPSILON) || equals(this.l, 100, WIDE_RANGE_EPSILON) ? 0 : c;
|
|
938
|
+
this.c = clamp(c, {min: 0});
|
|
939
|
+
h = equals(c, 0) ? 0 : h;
|
|
940
|
+
this.h = normalizeHue(h);
|
|
941
|
+
this.alpha = clamp(alpha, {min: 0, max: 1});
|
|
942
|
+
this.#authoredText = authoredText;
|
|
943
|
+
}
|
|
944
|
+
asLegacyColor(): Legacy {
|
|
945
|
+
return this.as(Format.RGBA);
|
|
946
|
+
}
|
|
947
|
+
is<T extends Format>(format: T): this is ReturnType<ColorConversions[T]> {
|
|
948
|
+
return format === this.format();
|
|
949
|
+
}
|
|
950
|
+
as<T extends Format>(format: T): ReturnType<ColorConversions[T]> {
|
|
951
|
+
return LCH.#conversions[format](this) as ReturnType<ColorConversions[T]>;
|
|
952
|
+
}
|
|
953
|
+
equal(color: Color): boolean {
|
|
954
|
+
const lch = color.as(Format.LCH);
|
|
955
|
+
return equals(lch.l, this.l, WIDE_RANGE_EPSILON) && equals(lch.c, this.c) && equals(lch.h, this.h) &&
|
|
956
|
+
equals(lch.alpha, this.alpha);
|
|
957
|
+
}
|
|
958
|
+
format(): Format {
|
|
959
|
+
return Format.LCH;
|
|
960
|
+
}
|
|
961
|
+
setAlpha(alpha: number): Color {
|
|
962
|
+
return new LCH(this.l, this.c, this.h, alpha);
|
|
963
|
+
}
|
|
964
|
+
asString(format?: Format): string {
|
|
965
|
+
if (format) {
|
|
966
|
+
return this.as(format).asString();
|
|
967
|
+
}
|
|
968
|
+
return this.#stringify(this.l, this.c, this.h);
|
|
969
|
+
}
|
|
970
|
+
#stringify(l: number, c: number, h: number): string {
|
|
971
|
+
const alpha = this.alpha === null || equals(this.alpha, 1) ?
|
|
972
|
+
'' :
|
|
973
|
+
` / ${Platform.StringUtilities.stringifyWithPrecision(this.alpha)}`;
|
|
974
|
+
return `lch(${Platform.StringUtilities.stringifyWithPrecision(l, 0)} ${
|
|
975
|
+
Platform.StringUtilities.stringifyWithPrecision(
|
|
976
|
+
c)} ${Platform.StringUtilities.stringifyWithPrecision(h)}${alpha})`;
|
|
977
|
+
}
|
|
978
|
+
getAuthoredText(): string|null {
|
|
979
|
+
return this.#authoredText ?? null;
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
getRawParameters(): Color3D {
|
|
983
|
+
return [...this.#rawParams];
|
|
984
|
+
}
|
|
985
|
+
getAsRawString(format?: Format): string {
|
|
986
|
+
if (format) {
|
|
987
|
+
return this.as(format).getAsRawString();
|
|
988
|
+
}
|
|
989
|
+
return this.#stringify(...this.#rawParams);
|
|
990
|
+
}
|
|
991
|
+
isGamutClipped(): boolean {
|
|
992
|
+
return false;
|
|
993
|
+
}
|
|
994
|
+
// See "powerless" component definitions in
|
|
995
|
+
// https://www.w3.org/TR/css-color-4/#specifying-lab-lch
|
|
996
|
+
isHuePowerless(): boolean {
|
|
997
|
+
return equals(this.c, 0);
|
|
998
|
+
}
|
|
999
|
+
static fromSpec(spec: ColorParameterSpec, text: string): LCH|null {
|
|
1000
|
+
const L = parsePercentage(spec[0], [0, 100]) ?? parseNumber(spec[0]);
|
|
1001
|
+
if (L === null) {
|
|
1002
|
+
return null;
|
|
1003
|
+
}
|
|
1004
|
+
const c = parsePercentage(spec[1], [0, 150]) ?? parseNumber(spec[1]);
|
|
1005
|
+
if (c === null) {
|
|
1006
|
+
return null;
|
|
1007
|
+
}
|
|
1008
|
+
const h = parseAngle(spec[2]);
|
|
1009
|
+
if (h === null) {
|
|
1010
|
+
return null;
|
|
1011
|
+
}
|
|
1012
|
+
const alpha = parseAlpha(spec[3]);
|
|
1013
|
+
|
|
1014
|
+
return new LCH(L, c, h, alpha, text);
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
|
|
1018
|
+
export class Oklab implements Color {
|
|
1019
|
+
readonly #rawParams: Color3D;
|
|
1020
|
+
readonly l: number;
|
|
1021
|
+
readonly a: number;
|
|
1022
|
+
readonly b: number;
|
|
1023
|
+
readonly alpha: number|null;
|
|
1024
|
+
readonly #authoredText?: string;
|
|
1025
|
+
readonly channels: [ColorChannel, ColorChannel, ColorChannel, ColorChannel] =
|
|
1026
|
+
[ColorChannel.L, ColorChannel.A, ColorChannel.B, ColorChannel.ALPHA];
|
|
1027
|
+
|
|
1028
|
+
static readonly #conversions: ColorConversions<Oklab> = {
|
|
1029
|
+
[Format.HEX]: (self: Oklab) => new Legacy(self.#getRGBArray(/* withAlpha= */ false), Format.HEX),
|
|
1030
|
+
[Format.HEXA]: (self: Oklab) => new Legacy(self.#getRGBArray(/* withAlpha= */ true), Format.HEXA),
|
|
1031
|
+
[Format.RGB]: (self: Oklab) => new Legacy(self.#getRGBArray(/* withAlpha= */ false), Format.RGB),
|
|
1032
|
+
[Format.RGBA]: (self: Oklab) => new Legacy(self.#getRGBArray(/* withAlpha= */ true), Format.RGBA),
|
|
1033
|
+
[Format.HSL]: (self: Oklab) => new HSL(...rgbToHsl(self.#getRGBArray(/* withAlpha= */ false)), self.alpha),
|
|
1034
|
+
[Format.HSLA]: (self: Oklab) => new HSL(...rgbToHsl(self.#getRGBArray(/* withAlpha= */ false)), self.alpha),
|
|
1035
|
+
[Format.HWB]: (self: Oklab) => new HWB(...rgbToHwb(self.#getRGBArray(/* withAlpha= */ false)), self.alpha),
|
|
1036
|
+
[Format.HWBA]: (self: Oklab) => new HWB(...rgbToHwb(self.#getRGBArray(/* withAlpha= */ false)), self.alpha),
|
|
1037
|
+
[Format.LCH]: (self: Oklab) =>
|
|
1038
|
+
new LCH(...ColorConverter.labToLch(...ColorConverter.xyzd50ToLab(...self.#toXyzd50())), self.alpha),
|
|
1039
|
+
[Format.OKLCH]: (self: Oklab) => new Oklch(...ColorConverter.xyzd50ToOklch(...self.#toXyzd50()), self.alpha),
|
|
1040
|
+
[Format.LAB]: (self: Oklab) => new Lab(...ColorConverter.xyzd50ToLab(...self.#toXyzd50()), self.alpha),
|
|
1041
|
+
[Format.OKLAB]: (self: Oklab) => self,
|
|
1042
|
+
|
|
1043
|
+
[Format.SRGB]: (self: Oklab) =>
|
|
1044
|
+
new ColorFunction(Format.SRGB, ...ColorConverter.xyzd50ToSrgb(...self.#toXyzd50()), self.alpha),
|
|
1045
|
+
[Format.SRGB_LINEAR]: (self: Oklab) =>
|
|
1046
|
+
new ColorFunction(Format.SRGB_LINEAR, ...ColorConverter.xyzd50TosRGBLinear(...self.#toXyzd50()), self.alpha),
|
|
1047
|
+
[Format.DISPLAY_P3]: (self: Oklab) =>
|
|
1048
|
+
new ColorFunction(Format.DISPLAY_P3, ...ColorConverter.xyzd50ToDisplayP3(...self.#toXyzd50()), self.alpha),
|
|
1049
|
+
[Format.A98_RGB]: (self: Oklab) =>
|
|
1050
|
+
new ColorFunction(Format.A98_RGB, ...ColorConverter.xyzd50ToAdobeRGB(...self.#toXyzd50()), self.alpha),
|
|
1051
|
+
[Format.PROPHOTO_RGB]: (self: Oklab) =>
|
|
1052
|
+
new ColorFunction(Format.PROPHOTO_RGB, ...ColorConverter.xyzd50ToProPhoto(...self.#toXyzd50()), self.alpha),
|
|
1053
|
+
[Format.REC_2020]: (self: Oklab) =>
|
|
1054
|
+
new ColorFunction(Format.REC_2020, ...ColorConverter.xyzd50ToRec2020(...self.#toXyzd50()), self.alpha),
|
|
1055
|
+
[Format.XYZ]: (self: Oklab) =>
|
|
1056
|
+
new ColorFunction(Format.XYZ, ...ColorConverter.xyzd50ToD65(...self.#toXyzd50()), self.alpha),
|
|
1057
|
+
[Format.XYZ_D50]: (self: Oklab) => new ColorFunction(Format.XYZ_D50, ...self.#toXyzd50(), self.alpha),
|
|
1058
|
+
[Format.XYZ_D65]: (self: Oklab) =>
|
|
1059
|
+
new ColorFunction(Format.XYZ_D65, ...ColorConverter.xyzd50ToD65(...self.#toXyzd50()), self.alpha),
|
|
1060
|
+
};
|
|
1061
|
+
|
|
1062
|
+
#toXyzd50(): Color3D {
|
|
1063
|
+
return ColorConverter.xyzd65ToD50(...ColorConverter.oklabToXyzd65(this.l, this.a, this.b));
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
#getRGBArray(withAlpha: true): Color4DOr3D;
|
|
1067
|
+
#getRGBArray(withAlpha: false): Color3D;
|
|
1068
|
+
#getRGBArray(withAlpha = true): Color4DOr3D|Color3D {
|
|
1069
|
+
const params = ColorConverter.xyzd50ToSrgb(...this.#toXyzd50());
|
|
1070
|
+
if (withAlpha) {
|
|
1071
|
+
return [...params, this.alpha ?? undefined];
|
|
1072
|
+
}
|
|
1073
|
+
return params;
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
constructor(l: number, a: number, b: number, alpha: number|null, authoredText?: string|undefined) {
|
|
1077
|
+
this.#rawParams = [l, a, b];
|
|
1078
|
+
this.l = clamp(l, {min: 0, max: 1});
|
|
1079
|
+
if (equals(this.l, 0) || equals(this.l, 1)) {
|
|
1080
|
+
a = b = 0;
|
|
1081
|
+
}
|
|
1082
|
+
this.a = a;
|
|
1083
|
+
this.b = b;
|
|
1084
|
+
this.alpha = clamp(alpha, {min: 0, max: 1});
|
|
1085
|
+
this.#authoredText = authoredText;
|
|
1086
|
+
}
|
|
1087
|
+
asLegacyColor(): Legacy {
|
|
1088
|
+
return this.as(Format.RGBA);
|
|
1089
|
+
}
|
|
1090
|
+
is<T extends Format>(format: T): this is ReturnType<ColorConversions[T]> {
|
|
1091
|
+
return format === this.format();
|
|
1092
|
+
}
|
|
1093
|
+
as<T extends Format>(format: T): ReturnType<ColorConversions[T]> {
|
|
1094
|
+
return Oklab.#conversions[format](this) as ReturnType<ColorConversions[T]>;
|
|
1095
|
+
}
|
|
1096
|
+
equal(color: Color): boolean {
|
|
1097
|
+
const oklab = color.as(Format.OKLAB);
|
|
1098
|
+
return equals(oklab.l, this.l) && equals(oklab.a, this.a) && equals(oklab.b, this.b) &&
|
|
1099
|
+
equals(oklab.alpha, this.alpha);
|
|
1100
|
+
}
|
|
1101
|
+
format(): Format {
|
|
1102
|
+
return Format.OKLAB;
|
|
1103
|
+
}
|
|
1104
|
+
setAlpha(alpha: number): Color {
|
|
1105
|
+
return new Oklab(this.l, this.a, this.b, alpha);
|
|
1106
|
+
}
|
|
1107
|
+
asString(format?: Format): string {
|
|
1108
|
+
if (format) {
|
|
1109
|
+
return this.as(format).asString();
|
|
1110
|
+
}
|
|
1111
|
+
return this.#stringify(this.l, this.a, this.b);
|
|
1112
|
+
}
|
|
1113
|
+
#stringify(l: number, a: number, b: number): string {
|
|
1114
|
+
const alpha = this.alpha === null || equals(this.alpha, 1) ?
|
|
1115
|
+
'' :
|
|
1116
|
+
` / ${Platform.StringUtilities.stringifyWithPrecision(this.alpha)}`;
|
|
1117
|
+
return `oklab(${Platform.StringUtilities.stringifyWithPrecision(l)} ${
|
|
1118
|
+
Platform.StringUtilities.stringifyWithPrecision(
|
|
1119
|
+
a)} ${Platform.StringUtilities.stringifyWithPrecision(b)}${alpha})`;
|
|
1120
|
+
}
|
|
1121
|
+
getAuthoredText(): string|null {
|
|
1122
|
+
return this.#authoredText ?? null;
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
getRawParameters(): Color3D {
|
|
1126
|
+
return [...this.#rawParams];
|
|
1127
|
+
}
|
|
1128
|
+
getAsRawString(format?: Format): string {
|
|
1129
|
+
if (format) {
|
|
1130
|
+
return this.as(format).getAsRawString();
|
|
1131
|
+
}
|
|
1132
|
+
return this.#stringify(...this.#rawParams);
|
|
1133
|
+
}
|
|
1134
|
+
isGamutClipped(): boolean {
|
|
1135
|
+
return false;
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
static fromSpec(spec: ColorParameterSpec, text: string): Oklab|null {
|
|
1139
|
+
const L = parsePercentage(spec[0], [0, 1]) ?? parseNumber(spec[0]);
|
|
1140
|
+
if (L === null) {
|
|
1141
|
+
return null;
|
|
1142
|
+
}
|
|
1143
|
+
const a = parsePercentage(spec[1], [0, 0.4]) ?? parseNumber(spec[1]);
|
|
1144
|
+
if (a === null) {
|
|
1145
|
+
return null;
|
|
1146
|
+
}
|
|
1147
|
+
const b = parsePercentage(spec[2], [0, 0.4]) ?? parseNumber(spec[2]);
|
|
1148
|
+
if (b === null) {
|
|
1149
|
+
return null;
|
|
1150
|
+
}
|
|
1151
|
+
const alpha = parseAlpha(spec[3]);
|
|
1152
|
+
|
|
1153
|
+
return new Oklab(L, a, b, alpha, text);
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
1156
|
+
|
|
1157
|
+
export class Oklch implements Color {
|
|
1158
|
+
readonly #rawParams: Color3D;
|
|
1159
|
+
readonly l: number;
|
|
1160
|
+
readonly c: number;
|
|
1161
|
+
readonly h: number;
|
|
1162
|
+
readonly alpha: number|null;
|
|
1163
|
+
readonly #authoredText?: string;
|
|
1164
|
+
readonly channels: [ColorChannel, ColorChannel, ColorChannel, ColorChannel] =
|
|
1165
|
+
[ColorChannel.L, ColorChannel.C, ColorChannel.H, ColorChannel.ALPHA];
|
|
1166
|
+
|
|
1167
|
+
static readonly #conversions: ColorConversions<Oklch> = {
|
|
1168
|
+
[Format.HEX]: (self: Oklch) => new Legacy(self.#getRGBArray(/* withAlpha= */ false), Format.HEX),
|
|
1169
|
+
[Format.HEXA]: (self: Oklch) => new Legacy(self.#getRGBArray(/* withAlpha= */ true), Format.HEXA),
|
|
1170
|
+
[Format.RGB]: (self: Oklch) => new Legacy(self.#getRGBArray(/* withAlpha= */ false), Format.RGB),
|
|
1171
|
+
[Format.RGBA]: (self: Oklch) => new Legacy(self.#getRGBArray(/* withAlpha= */ true), Format.RGBA),
|
|
1172
|
+
[Format.HSL]: (self: Oklch) => new HSL(...rgbToHsl(self.#getRGBArray(/* withAlpha= */ false)), self.alpha),
|
|
1173
|
+
[Format.HSLA]: (self: Oklch) => new HSL(...rgbToHsl(self.#getRGBArray(/* withAlpha= */ false)), self.alpha),
|
|
1174
|
+
[Format.HWB]: (self: Oklch) => new HWB(...rgbToHwb(self.#getRGBArray(/* withAlpha= */ false)), self.alpha),
|
|
1175
|
+
[Format.HWBA]: (self: Oklch) => new HWB(...rgbToHwb(self.#getRGBArray(/* withAlpha= */ false)), self.alpha),
|
|
1176
|
+
[Format.LCH]: (self: Oklch) =>
|
|
1177
|
+
new LCH(...ColorConverter.labToLch(...ColorConverter.xyzd50ToLab(...self.#toXyzd50())), self.alpha),
|
|
1178
|
+
[Format.OKLCH]: (self: Oklch) => self,
|
|
1179
|
+
[Format.LAB]: (self: Oklch) => new Lab(...ColorConverter.xyzd50ToLab(...self.#toXyzd50()), self.alpha),
|
|
1180
|
+
[Format.OKLAB]: (self: Oklch) =>
|
|
1181
|
+
new Oklab(...ColorConverter.xyzd65ToOklab(...ColorConverter.xyzd50ToD65(...self.#toXyzd50())), self.alpha),
|
|
1182
|
+
[Format.SRGB]: (self: Oklch) =>
|
|
1183
|
+
new ColorFunction(Format.SRGB, ...ColorConverter.xyzd50ToSrgb(...self.#toXyzd50()), self.alpha),
|
|
1184
|
+
[Format.SRGB_LINEAR]: (self: Oklch) =>
|
|
1185
|
+
new ColorFunction(Format.SRGB_LINEAR, ...ColorConverter.xyzd50TosRGBLinear(...self.#toXyzd50()), self.alpha),
|
|
1186
|
+
[Format.DISPLAY_P3]: (self: Oklch) =>
|
|
1187
|
+
new ColorFunction(Format.DISPLAY_P3, ...ColorConverter.xyzd50ToDisplayP3(...self.#toXyzd50()), self.alpha),
|
|
1188
|
+
[Format.A98_RGB]: (self: Oklch) =>
|
|
1189
|
+
new ColorFunction(Format.A98_RGB, ...ColorConverter.xyzd50ToAdobeRGB(...self.#toXyzd50()), self.alpha),
|
|
1190
|
+
[Format.PROPHOTO_RGB]: (self: Oklch) =>
|
|
1191
|
+
new ColorFunction(Format.PROPHOTO_RGB, ...ColorConverter.xyzd50ToProPhoto(...self.#toXyzd50()), self.alpha),
|
|
1192
|
+
[Format.REC_2020]: (self: Oklch) =>
|
|
1193
|
+
new ColorFunction(Format.REC_2020, ...ColorConverter.xyzd50ToRec2020(...self.#toXyzd50()), self.alpha),
|
|
1194
|
+
[Format.XYZ]: (self: Oklch) =>
|
|
1195
|
+
new ColorFunction(Format.XYZ, ...ColorConverter.xyzd50ToD65(...self.#toXyzd50()), self.alpha),
|
|
1196
|
+
[Format.XYZ_D50]: (self: Oklch) => new ColorFunction(Format.XYZ_D50, ...self.#toXyzd50(), self.alpha),
|
|
1197
|
+
[Format.XYZ_D65]: (self: Oklch) =>
|
|
1198
|
+
new ColorFunction(Format.XYZ_D65, ...ColorConverter.xyzd50ToD65(...self.#toXyzd50()), self.alpha),
|
|
1199
|
+
};
|
|
1200
|
+
|
|
1201
|
+
#toXyzd50(): Color3D {
|
|
1202
|
+
return ColorConverter.oklchToXyzd50(this.l, this.c, this.h);
|
|
1203
|
+
}
|
|
1204
|
+
|
|
1205
|
+
#getRGBArray(withAlpha: true): Color4DOr3D;
|
|
1206
|
+
#getRGBArray(withAlpha: false): Color3D;
|
|
1207
|
+
#getRGBArray(withAlpha = true): Color4DOr3D|Color3D {
|
|
1208
|
+
const params = ColorConverter.xyzd50ToSrgb(...this.#toXyzd50());
|
|
1209
|
+
if (withAlpha) {
|
|
1210
|
+
return [...params, this.alpha ?? undefined];
|
|
1211
|
+
}
|
|
1212
|
+
return params;
|
|
1213
|
+
}
|
|
1214
|
+
|
|
1215
|
+
constructor(l: number, c: number, h: number, alpha: number|null, authoredText?: string|undefined) {
|
|
1216
|
+
this.#rawParams = [l, c, h];
|
|
1217
|
+
this.l = clamp(l, {min: 0, max: 1});
|
|
1218
|
+
c = equals(this.l, 0) || equals(this.l, 1) ? 0 : c;
|
|
1219
|
+
this.c = clamp(c, {min: 0});
|
|
1220
|
+
h = equals(c, 0) ? 0 : h;
|
|
1221
|
+
this.h = normalizeHue(h);
|
|
1222
|
+
this.alpha = clamp(alpha, {min: 0, max: 1});
|
|
1223
|
+
this.#authoredText = authoredText;
|
|
1224
|
+
}
|
|
1225
|
+
asLegacyColor(): Legacy {
|
|
1226
|
+
return this.as(Format.RGBA);
|
|
1227
|
+
}
|
|
1228
|
+
is<T extends Format>(format: T): this is ReturnType<ColorConversions[T]> {
|
|
1229
|
+
return format === this.format();
|
|
1230
|
+
}
|
|
1231
|
+
as<T extends Format>(format: T): ReturnType<ColorConversions[T]> {
|
|
1232
|
+
return Oklch.#conversions[format](this) as ReturnType<ColorConversions[T]>;
|
|
1233
|
+
}
|
|
1234
|
+
equal(color: Color): boolean {
|
|
1235
|
+
const oklch = color.as(Format.OKLCH);
|
|
1236
|
+
return equals(oklch.l, this.l) && equals(oklch.c, this.c) && equals(oklch.h, this.h) &&
|
|
1237
|
+
equals(oklch.alpha, this.alpha);
|
|
1238
|
+
}
|
|
1239
|
+
format(): Format {
|
|
1240
|
+
return Format.OKLCH;
|
|
1241
|
+
}
|
|
1242
|
+
setAlpha(alpha: number): Color {
|
|
1243
|
+
return new Oklch(this.l, this.c, this.h, alpha);
|
|
1244
|
+
}
|
|
1245
|
+
asString(format?: Format): string {
|
|
1246
|
+
if (format) {
|
|
1247
|
+
return this.as(format).asString();
|
|
1248
|
+
}
|
|
1249
|
+
return this.#stringify(this.l, this.c, this.h);
|
|
1250
|
+
}
|
|
1251
|
+
#stringify(l: number, c: number, h: number): string {
|
|
1252
|
+
const alpha = this.alpha === null || equals(this.alpha, 1) ?
|
|
1253
|
+
'' :
|
|
1254
|
+
` / ${Platform.StringUtilities.stringifyWithPrecision(this.alpha)}`;
|
|
1255
|
+
return `oklch(${Platform.StringUtilities.stringifyWithPrecision(l)} ${
|
|
1256
|
+
Platform.StringUtilities.stringifyWithPrecision(
|
|
1257
|
+
c)} ${Platform.StringUtilities.stringifyWithPrecision(h)}${alpha})`;
|
|
1258
|
+
}
|
|
1259
|
+
getAuthoredText(): string|null {
|
|
1260
|
+
return this.#authoredText ?? null;
|
|
1261
|
+
}
|
|
1262
|
+
|
|
1263
|
+
getRawParameters(): Color3D {
|
|
1264
|
+
return [...this.#rawParams];
|
|
1265
|
+
}
|
|
1266
|
+
getAsRawString(format?: Format): string {
|
|
1267
|
+
if (format) {
|
|
1268
|
+
return this.as(format).getAsRawString();
|
|
1269
|
+
}
|
|
1270
|
+
return this.#stringify(...this.#rawParams);
|
|
1271
|
+
}
|
|
1272
|
+
isGamutClipped(): boolean {
|
|
1273
|
+
return false;
|
|
1274
|
+
}
|
|
1275
|
+
|
|
1276
|
+
static fromSpec(spec: ColorParameterSpec, text: string): Oklch|null {
|
|
1277
|
+
const L = parsePercentage(spec[0], [0, 1]) ?? parseNumber(spec[0]);
|
|
1278
|
+
if (L === null) {
|
|
1279
|
+
return null;
|
|
1280
|
+
}
|
|
1281
|
+
const c = parsePercentage(spec[1], [0, 0.4]) ?? parseNumber(spec[1]);
|
|
1282
|
+
if (c === null) {
|
|
1283
|
+
return null;
|
|
1284
|
+
}
|
|
1285
|
+
const h = parseAngle(spec[2]);
|
|
1286
|
+
if (h === null) {
|
|
1287
|
+
return null;
|
|
1288
|
+
}
|
|
1289
|
+
const alpha = parseAlpha(spec[3]);
|
|
1290
|
+
|
|
1291
|
+
return new Oklch(L, c, h, alpha, text);
|
|
1292
|
+
}
|
|
1293
|
+
}
|
|
1294
|
+
|
|
1295
|
+
export class ColorFunction implements Color {
|
|
1296
|
+
readonly #rawParams: Color3D;
|
|
1297
|
+
readonly p0: number;
|
|
1298
|
+
readonly p1: number;
|
|
1299
|
+
readonly p2: number;
|
|
1300
|
+
readonly alpha: number|null;
|
|
1301
|
+
readonly colorSpace: ColorSpace;
|
|
1302
|
+
readonly #authoredText?: string;
|
|
1303
|
+
get channels(): [ColorChannel, ColorChannel, ColorChannel, ColorChannel] {
|
|
1304
|
+
return this.isXYZ() ? [ColorChannel.X, ColorChannel.Y, ColorChannel.Z, ColorChannel.ALPHA] :
|
|
1305
|
+
[ColorChannel.R, ColorChannel.G, ColorChannel.B, ColorChannel.ALPHA];
|
|
1306
|
+
}
|
|
1307
|
+
|
|
1308
|
+
static readonly #conversions: ColorConversions<ColorFunction> = {
|
|
1309
|
+
[Format.HEX]: (self: ColorFunction) => new Legacy(self.#getRGBArray(/* withAlpha= */ false), Format.HEX),
|
|
1310
|
+
[Format.HEXA]: (self: ColorFunction) => new Legacy(self.#getRGBArray(/* withAlpha= */ true), Format.HEXA),
|
|
1311
|
+
[Format.RGB]: (self: ColorFunction) => new Legacy(self.#getRGBArray(/* withAlpha= */ false), Format.RGB),
|
|
1312
|
+
[Format.RGBA]: (self: ColorFunction) => new Legacy(self.#getRGBArray(/* withAlpha= */ true), Format.RGBA),
|
|
1313
|
+
[Format.HSL]: (self: ColorFunction) => new HSL(...rgbToHsl(self.#getRGBArray(/* withAlpha= */ false)), self.alpha),
|
|
1314
|
+
[Format.HSLA]: (self: ColorFunction) => new HSL(...rgbToHsl(self.#getRGBArray(/* withAlpha= */ false)), self.alpha),
|
|
1315
|
+
[Format.HWB]: (self: ColorFunction) => new HWB(...rgbToHwb(self.#getRGBArray(/* withAlpha= */ false)), self.alpha),
|
|
1316
|
+
[Format.HWBA]: (self: ColorFunction) => new HWB(...rgbToHwb(self.#getRGBArray(/* withAlpha= */ false)), self.alpha),
|
|
1317
|
+
[Format.LCH]: (self: ColorFunction) =>
|
|
1318
|
+
new LCH(...ColorConverter.labToLch(...ColorConverter.xyzd50ToLab(...self.#toXyzd50())), self.alpha),
|
|
1319
|
+
[Format.OKLCH]: (self: ColorFunction) =>
|
|
1320
|
+
new Oklch(...ColorConverter.xyzd50ToOklch(...self.#toXyzd50()), self.alpha),
|
|
1321
|
+
[Format.LAB]: (self: ColorFunction) => new Lab(...ColorConverter.xyzd50ToLab(...self.#toXyzd50()), self.alpha),
|
|
1322
|
+
[Format.OKLAB]: (self: ColorFunction) =>
|
|
1323
|
+
new Oklab(...ColorConverter.xyzd65ToOklab(...ColorConverter.xyzd50ToD65(...self.#toXyzd50())), self.alpha),
|
|
1324
|
+
|
|
1325
|
+
[Format.SRGB]: (self: ColorFunction) =>
|
|
1326
|
+
new ColorFunction(Format.SRGB, ...ColorConverter.xyzd50ToSrgb(...self.#toXyzd50()), self.alpha),
|
|
1327
|
+
[Format.SRGB_LINEAR]: (self: ColorFunction) =>
|
|
1328
|
+
new ColorFunction(Format.SRGB_LINEAR, ...ColorConverter.xyzd50TosRGBLinear(...self.#toXyzd50()), self.alpha),
|
|
1329
|
+
[Format.DISPLAY_P3]: (self: ColorFunction) =>
|
|
1330
|
+
new ColorFunction(Format.DISPLAY_P3, ...ColorConverter.xyzd50ToDisplayP3(...self.#toXyzd50()), self.alpha),
|
|
1331
|
+
[Format.A98_RGB]: (self: ColorFunction) =>
|
|
1332
|
+
new ColorFunction(Format.A98_RGB, ...ColorConverter.xyzd50ToAdobeRGB(...self.#toXyzd50()), self.alpha),
|
|
1333
|
+
[Format.PROPHOTO_RGB]: (self: ColorFunction) =>
|
|
1334
|
+
new ColorFunction(Format.PROPHOTO_RGB, ...ColorConverter.xyzd50ToProPhoto(...self.#toXyzd50()), self.alpha),
|
|
1335
|
+
[Format.REC_2020]: (self: ColorFunction) =>
|
|
1336
|
+
new ColorFunction(Format.REC_2020, ...ColorConverter.xyzd50ToRec2020(...self.#toXyzd50()), self.alpha),
|
|
1337
|
+
[Format.XYZ]: (self: ColorFunction) =>
|
|
1338
|
+
new ColorFunction(Format.XYZ, ...ColorConverter.xyzd50ToD65(...self.#toXyzd50()), self.alpha),
|
|
1339
|
+
[Format.XYZ_D50]: (self: ColorFunction) => new ColorFunction(Format.XYZ_D50, ...self.#toXyzd50(), self.alpha),
|
|
1340
|
+
[Format.XYZ_D65]: (self: ColorFunction) =>
|
|
1341
|
+
new ColorFunction(Format.XYZ_D65, ...ColorConverter.xyzd50ToD65(...self.#toXyzd50()), self.alpha),
|
|
1342
|
+
};
|
|
1343
|
+
|
|
1344
|
+
#toXyzd50(): Color3D {
|
|
1345
|
+
// With color(), out-of-gamut inputs are to be used for intermediate computations
|
|
1346
|
+
const [p0, p1, p2] = this.#rawParams;
|
|
1347
|
+
switch (this.colorSpace) {
|
|
1348
|
+
case Format.SRGB:
|
|
1349
|
+
return ColorConverter.srgbToXyzd50(p0, p1, p2);
|
|
1350
|
+
case Format.SRGB_LINEAR:
|
|
1351
|
+
return ColorConverter.srgbLinearToXyzd50(p0, p1, p2);
|
|
1352
|
+
case Format.DISPLAY_P3:
|
|
1353
|
+
return ColorConverter.displayP3ToXyzd50(p0, p1, p2);
|
|
1354
|
+
case Format.A98_RGB:
|
|
1355
|
+
return ColorConverter.adobeRGBToXyzd50(p0, p1, p2);
|
|
1356
|
+
case Format.PROPHOTO_RGB:
|
|
1357
|
+
return ColorConverter.proPhotoToXyzd50(p0, p1, p2);
|
|
1358
|
+
case Format.REC_2020:
|
|
1359
|
+
return ColorConverter.rec2020ToXyzd50(p0, p1, p2);
|
|
1360
|
+
case Format.XYZ_D50:
|
|
1361
|
+
return [p0, p1, p2];
|
|
1362
|
+
case Format.XYZ:
|
|
1363
|
+
case Format.XYZ_D65:
|
|
1364
|
+
return ColorConverter.xyzd65ToD50(p0, p1, p2);
|
|
1365
|
+
}
|
|
1366
|
+
throw new Error('Invalid color space');
|
|
1367
|
+
}
|
|
1368
|
+
|
|
1369
|
+
#getRGBArray(withAlpha: true): Color4DOr3D;
|
|
1370
|
+
#getRGBArray(withAlpha: false): Color3D;
|
|
1371
|
+
#getRGBArray(withAlpha = true): Color4DOr3D|Color3D {
|
|
1372
|
+
// With color(), out-of-gamut inputs are to be used for intermediate computations
|
|
1373
|
+
const [p0, p1, p2] = this.#rawParams;
|
|
1374
|
+
const params: Color3D =
|
|
1375
|
+
this.colorSpace === Format.SRGB ? [p0, p1, p2] : [...ColorConverter.xyzd50ToSrgb(...this.#toXyzd50())];
|
|
1376
|
+
if (withAlpha) {
|
|
1377
|
+
return [...params, this.alpha ?? undefined];
|
|
1378
|
+
}
|
|
1379
|
+
return params;
|
|
1380
|
+
}
|
|
1381
|
+
|
|
1382
|
+
constructor(
|
|
1383
|
+
colorSpace: ColorSpace, p0: number, p1: number, p2: number, alpha: number|null, authoredText?: string|undefined) {
|
|
1384
|
+
this.#rawParams = [p0, p1, p2];
|
|
1385
|
+
this.colorSpace = colorSpace;
|
|
1386
|
+
this.#authoredText = authoredText;
|
|
1387
|
+
if (this.colorSpace !== Format.XYZ_D50 && this.colorSpace !== Format.XYZ_D65 && this.colorSpace !== Format.XYZ) {
|
|
1388
|
+
p0 = clamp(p0, {min: 0, max: 1});
|
|
1389
|
+
p1 = clamp(p1, {min: 0, max: 1});
|
|
1390
|
+
p2 = clamp(p2, {min: 0, max: 1});
|
|
1391
|
+
}
|
|
1392
|
+
|
|
1393
|
+
this.p0 = p0;
|
|
1394
|
+
this.p1 = p1;
|
|
1395
|
+
this.p2 = p2;
|
|
1396
|
+
this.alpha = clamp(alpha, {min: 0, max: 1});
|
|
1397
|
+
}
|
|
1398
|
+
asLegacyColor(): Legacy {
|
|
1399
|
+
return this.as(Format.RGBA);
|
|
1400
|
+
}
|
|
1401
|
+
is<T extends Format>(format: T): this is ReturnType<ColorConversions[T]> {
|
|
1402
|
+
return format === this.format();
|
|
1403
|
+
}
|
|
1404
|
+
as<T extends Format>(format: T): ReturnType<ColorConversions[T]> {
|
|
1405
|
+
if (this.colorSpace === format) {
|
|
1406
|
+
return this as ReturnType<ColorConversions[T]>;
|
|
1407
|
+
}
|
|
1408
|
+
return ColorFunction.#conversions[format](this) as ReturnType<ColorConversions[T]>;
|
|
1409
|
+
}
|
|
1410
|
+
equal(color: Color): boolean {
|
|
1411
|
+
const space = color.as(this.colorSpace);
|
|
1412
|
+
return equals(this.p0, space.p0) && equals(this.p1, space.p1) && equals(this.p2, space.p2) &&
|
|
1413
|
+
equals(this.alpha, space.alpha);
|
|
1414
|
+
}
|
|
1415
|
+
format(): Format {
|
|
1416
|
+
return this.colorSpace;
|
|
1417
|
+
}
|
|
1418
|
+
setAlpha(alpha: number): Color {
|
|
1419
|
+
return new ColorFunction(this.colorSpace, this.p0, this.p1, this.p2, alpha);
|
|
1420
|
+
}
|
|
1421
|
+
asString(format?: Format): string {
|
|
1422
|
+
if (format) {
|
|
1423
|
+
return this.as(format).asString();
|
|
1424
|
+
}
|
|
1425
|
+
return this.#stringify(this.p0, this.p1, this.p2);
|
|
1426
|
+
}
|
|
1427
|
+
#stringify(p0: number, p1: number, p2: number): string {
|
|
1428
|
+
const alpha = this.alpha === null || equals(this.alpha, 1) ?
|
|
1429
|
+
'' :
|
|
1430
|
+
` / ${Platform.StringUtilities.stringifyWithPrecision(this.alpha)}`;
|
|
1431
|
+
return `color(${this.colorSpace} ${Platform.StringUtilities.stringifyWithPrecision(p0)} ${
|
|
1432
|
+
Platform.StringUtilities.stringifyWithPrecision(
|
|
1433
|
+
p1)} ${Platform.StringUtilities.stringifyWithPrecision(p2)}${alpha})`;
|
|
1434
|
+
}
|
|
1435
|
+
getAuthoredText(): string|null {
|
|
1436
|
+
return this.#authoredText ?? null;
|
|
1437
|
+
}
|
|
1438
|
+
|
|
1439
|
+
getRawParameters(): Color3D {
|
|
1440
|
+
return [...this.#rawParams];
|
|
1441
|
+
}
|
|
1442
|
+
getAsRawString(format?: Format): string {
|
|
1443
|
+
if (format) {
|
|
1444
|
+
return this.as(format).getAsRawString();
|
|
1445
|
+
}
|
|
1446
|
+
return this.#stringify(...this.#rawParams);
|
|
1447
|
+
}
|
|
1448
|
+
isGamutClipped(): boolean {
|
|
1449
|
+
if (this.colorSpace !== Format.XYZ_D50 && this.colorSpace !== Format.XYZ_D65 && this.colorSpace !== Format.XYZ) {
|
|
1450
|
+
return !equals(this.#rawParams, [this.p0, this.p1, this.p2]);
|
|
1451
|
+
}
|
|
1452
|
+
return false;
|
|
1453
|
+
}
|
|
1454
|
+
|
|
1455
|
+
isXYZ(): boolean {
|
|
1456
|
+
switch (this.colorSpace) {
|
|
1457
|
+
case Format.XYZ:
|
|
1458
|
+
case Format.XYZ_D50:
|
|
1459
|
+
case Format.XYZ_D65:
|
|
1460
|
+
return true;
|
|
1461
|
+
}
|
|
1462
|
+
return false;
|
|
1463
|
+
}
|
|
1464
|
+
|
|
1465
|
+
/**
|
|
1466
|
+
* Parses given `color()` function definition and returns the `Color` object.
|
|
1467
|
+
* We want to special case its parsing here because it's a bit different
|
|
1468
|
+
* than other color functions: rgb, lch etc. accepts 3 arguments with
|
|
1469
|
+
* optional alpha. This accepts 4 arguments with optional alpha.
|
|
1470
|
+
*
|
|
1471
|
+
* Instead of making `splitColorFunctionParameters` work for this case too
|
|
1472
|
+
* I've decided to implement it specifically.
|
|
1473
|
+
* @param authoredText Original definition of the color with `color`
|
|
1474
|
+
* @param parametersText Inside of the `color()` function. ex, `display-p3 0.1 0.2 0.3 / 0%`
|
|
1475
|
+
* @returns `Color` object
|
|
1476
|
+
*/
|
|
1477
|
+
static fromSpec(authoredText: string, parametersWithAlphaText: string): ColorFunction|null {
|
|
1478
|
+
const [parametersText, alphaText] = parametersWithAlphaText.split('/', 2);
|
|
1479
|
+
const parameters = parametersText.trim().split(/\s+/);
|
|
1480
|
+
const [colorSpaceText, ...remainingParams] = parameters;
|
|
1481
|
+
const colorSpace = getColorSpace(colorSpaceText);
|
|
1482
|
+
// Color space is not known to us, do not parse the Color.
|
|
1483
|
+
if (!colorSpace) {
|
|
1484
|
+
return null;
|
|
1485
|
+
}
|
|
1486
|
+
|
|
1487
|
+
// `color(<color-space>)` is a valid syntax
|
|
1488
|
+
if (remainingParams.length === 0 && alphaText === undefined) {
|
|
1489
|
+
return new ColorFunction(colorSpace, 0, 0, 0, null, authoredText);
|
|
1490
|
+
}
|
|
1491
|
+
|
|
1492
|
+
// Check if it contains `/ <alpha>` part, if so, it should be at the end
|
|
1493
|
+
if (remainingParams.length === 0 && alphaText !== undefined && alphaText.trim().split(/\s+/).length > 1) {
|
|
1494
|
+
// Invalid syntax: like `color(<space> / <alpha> <number>)`
|
|
1495
|
+
return null;
|
|
1496
|
+
}
|
|
1497
|
+
|
|
1498
|
+
// `color` cannot contain more than 3 parameters without alpha
|
|
1499
|
+
if (remainingParams.length > 3) {
|
|
1500
|
+
return null;
|
|
1501
|
+
}
|
|
1502
|
+
|
|
1503
|
+
// Replace `none`s with 0s
|
|
1504
|
+
const nonesReplacedParams = remainingParams.map(param => param === 'none' ? '0' : param);
|
|
1505
|
+
|
|
1506
|
+
// At this point, we know that all the values are there so we can
|
|
1507
|
+
// safely try to parse all the values as number or percentage
|
|
1508
|
+
const values = nonesReplacedParams.map(param => parsePercentOrNumber(param, [0, 1]));
|
|
1509
|
+
const containsNull = values.includes(null);
|
|
1510
|
+
// At least one value is malformatted (not a number or percentage)
|
|
1511
|
+
if (containsNull) {
|
|
1512
|
+
return null;
|
|
1513
|
+
}
|
|
1514
|
+
|
|
1515
|
+
const alphaValue = alphaText ? parsePercentOrNumber(alphaText, [0, 1]) ?? 1 : 1;
|
|
1516
|
+
|
|
1517
|
+
// Depending on the color space
|
|
1518
|
+
// this either reflects `rgb` parameters in that color space
|
|
1519
|
+
// or `xyz` parameters in the given `xyz` space.
|
|
1520
|
+
const rgbOrXyza: Color4D = [
|
|
1521
|
+
values[0] ?? 0,
|
|
1522
|
+
values[1] ?? 0,
|
|
1523
|
+
values[2] ?? 0,
|
|
1524
|
+
alphaValue,
|
|
1525
|
+
];
|
|
1526
|
+
|
|
1527
|
+
return new ColorFunction(colorSpace, ...rgbOrXyza, authoredText);
|
|
1528
|
+
}
|
|
1529
|
+
}
|
|
1530
|
+
|
|
1531
|
+
export class HSL implements Color {
|
|
1532
|
+
readonly h: number;
|
|
1533
|
+
readonly s: number;
|
|
1534
|
+
readonly l: number;
|
|
1535
|
+
readonly alpha: number|null;
|
|
1536
|
+
readonly #rawParams: Color3D;
|
|
1537
|
+
#authoredText: string|undefined;
|
|
1538
|
+
readonly channels: [ColorChannel, ColorChannel, ColorChannel, ColorChannel] =
|
|
1539
|
+
[ColorChannel.H, ColorChannel.S, ColorChannel.L, ColorChannel.ALPHA];
|
|
1540
|
+
|
|
1541
|
+
static readonly #conversions: ColorConversions<HSL> = {
|
|
1542
|
+
[Format.HEX]: (self: HSL) => new Legacy(self.#getRGBArray(/* withAlpha= */ false), Format.HEX),
|
|
1543
|
+
[Format.HEXA]: (self: HSL) => new Legacy(self.#getRGBArray(/* withAlpha= */ true), Format.HEXA),
|
|
1544
|
+
[Format.RGB]: (self: HSL) => new Legacy(self.#getRGBArray(/* withAlpha= */ false), Format.RGB),
|
|
1545
|
+
[Format.RGBA]: (self: HSL) => new Legacy(self.#getRGBArray(/* withAlpha= */ true), Format.RGBA),
|
|
1546
|
+
[Format.HSL]: (self: HSL) => self,
|
|
1547
|
+
[Format.HSLA]: (self: HSL) => self,
|
|
1548
|
+
[Format.HWB]: (self: HSL) => new HWB(...rgbToHwb(self.#getRGBArray(/* withAlpha= */ false)), self.alpha),
|
|
1549
|
+
[Format.HWBA]: (self: HSL) => new HWB(...rgbToHwb(self.#getRGBArray(/* withAlpha= */ false)), self.alpha),
|
|
1550
|
+
[Format.LCH]: (self: HSL) =>
|
|
1551
|
+
new LCH(...ColorConverter.labToLch(...ColorConverter.xyzd50ToLab(...self.#toXyzd50())), self.alpha),
|
|
1552
|
+
[Format.OKLCH]: (self: HSL) => new Oklch(...ColorConverter.xyzd50ToOklch(...self.#toXyzd50()), self.alpha),
|
|
1553
|
+
[Format.LAB]: (self: HSL) => new Lab(...ColorConverter.xyzd50ToLab(...self.#toXyzd50()), self.alpha),
|
|
1554
|
+
[Format.OKLAB]: (self: HSL) =>
|
|
1555
|
+
new Oklab(...ColorConverter.xyzd65ToOklab(...ColorConverter.xyzd50ToD65(...self.#toXyzd50())), self.alpha),
|
|
1556
|
+
|
|
1557
|
+
[Format.SRGB]: (self: HSL) =>
|
|
1558
|
+
new ColorFunction(Format.SRGB, ...ColorConverter.xyzd50ToSrgb(...self.#toXyzd50()), self.alpha),
|
|
1559
|
+
[Format.SRGB_LINEAR]: (self: HSL) =>
|
|
1560
|
+
new ColorFunction(Format.SRGB_LINEAR, ...ColorConverter.xyzd50TosRGBLinear(...self.#toXyzd50()), self.alpha),
|
|
1561
|
+
[Format.DISPLAY_P3]: (self: HSL) =>
|
|
1562
|
+
new ColorFunction(Format.DISPLAY_P3, ...ColorConverter.xyzd50ToDisplayP3(...self.#toXyzd50()), self.alpha),
|
|
1563
|
+
[Format.A98_RGB]: (self: HSL) =>
|
|
1564
|
+
new ColorFunction(Format.A98_RGB, ...ColorConverter.xyzd50ToAdobeRGB(...self.#toXyzd50()), self.alpha),
|
|
1565
|
+
[Format.PROPHOTO_RGB]: (self: HSL) =>
|
|
1566
|
+
new ColorFunction(Format.PROPHOTO_RGB, ...ColorConverter.xyzd50ToProPhoto(...self.#toXyzd50()), self.alpha),
|
|
1567
|
+
[Format.REC_2020]: (self: HSL) =>
|
|
1568
|
+
new ColorFunction(Format.REC_2020, ...ColorConverter.xyzd50ToRec2020(...self.#toXyzd50()), self.alpha),
|
|
1569
|
+
[Format.XYZ]: (self: HSL) =>
|
|
1570
|
+
new ColorFunction(Format.XYZ, ...ColorConverter.xyzd50ToD65(...self.#toXyzd50()), self.alpha),
|
|
1571
|
+
[Format.XYZ_D50]: (self: HSL) => new ColorFunction(Format.XYZ_D50, ...self.#toXyzd50(), self.alpha),
|
|
1572
|
+
[Format.XYZ_D65]: (self: HSL) =>
|
|
1573
|
+
new ColorFunction(Format.XYZ_D65, ...ColorConverter.xyzd50ToD65(...self.#toXyzd50()), self.alpha),
|
|
1574
|
+
};
|
|
1575
|
+
|
|
1576
|
+
#getRGBArray(withAlpha: true): Color4DOr3D;
|
|
1577
|
+
#getRGBArray(withAlpha: false): Color3D;
|
|
1578
|
+
#getRGBArray(withAlpha = true): Color4DOr3D|Color3D {
|
|
1579
|
+
const rgb = hsl2rgb([this.h, this.s, this.l, 0]);
|
|
1580
|
+
if (withAlpha) {
|
|
1581
|
+
return [rgb[0], rgb[1], rgb[2], this.alpha ?? undefined];
|
|
1582
|
+
}
|
|
1583
|
+
return [rgb[0], rgb[1], rgb[2]];
|
|
1584
|
+
}
|
|
1585
|
+
|
|
1586
|
+
#toXyzd50(): Color3D {
|
|
1587
|
+
const rgb = this.#getRGBArray(false);
|
|
1588
|
+
return ColorConverter.srgbToXyzd50(rgb[0], rgb[1], rgb[2]);
|
|
1589
|
+
}
|
|
1590
|
+
|
|
1591
|
+
constructor(h: number, s: number, l: number, alpha: number|null|undefined, authoredText?: string) {
|
|
1592
|
+
this.#rawParams = [h, s, l];
|
|
1593
|
+
this.l = clamp(l, {min: 0, max: 1});
|
|
1594
|
+
s = equals(this.l, 0) || equals(this.l, 1) ? 0 : s;
|
|
1595
|
+
this.s = clamp(s, {min: 0, max: 1});
|
|
1596
|
+
h = equals(this.s, 0) ? 0 : h;
|
|
1597
|
+
this.h = normalizeHue(h * 360) / 360;
|
|
1598
|
+
this.alpha = clamp(alpha ?? null, {min: 0, max: 1});
|
|
1599
|
+
this.#authoredText = authoredText;
|
|
1600
|
+
}
|
|
1601
|
+
|
|
1602
|
+
equal(color: Color): boolean {
|
|
1603
|
+
const hsl = color.as(Format.HSL);
|
|
1604
|
+
return equals(this.h, hsl.h) && equals(this.s, hsl.s) && equals(this.l, hsl.l) && equals(this.alpha, hsl.alpha);
|
|
1605
|
+
}
|
|
1606
|
+
asString(format?: Format|undefined): string {
|
|
1607
|
+
if (format) {
|
|
1608
|
+
return this.as(format).asString();
|
|
1609
|
+
}
|
|
1610
|
+
return this.#stringify(this.h, this.s, this.l);
|
|
1611
|
+
}
|
|
1612
|
+
#stringify(h: number, s: number, l: number): string {
|
|
1613
|
+
const start = Platform.StringUtilities.sprintf(
|
|
1614
|
+
'hsl(%sdeg %s% %s%', Platform.StringUtilities.stringifyWithPrecision(h * 360),
|
|
1615
|
+
Platform.StringUtilities.stringifyWithPrecision(s * 100),
|
|
1616
|
+
Platform.StringUtilities.stringifyWithPrecision(l * 100));
|
|
1617
|
+
if (this.alpha !== null && this.alpha !== 1) {
|
|
1618
|
+
return start +
|
|
1619
|
+
Platform.StringUtilities.sprintf(
|
|
1620
|
+
' / %s%)', Platform.StringUtilities.stringifyWithPrecision(this.alpha * 100));
|
|
1621
|
+
}
|
|
1622
|
+
return start + ')';
|
|
1623
|
+
}
|
|
1624
|
+
setAlpha(alpha: number): HSL {
|
|
1625
|
+
return new HSL(this.h, this.s, this.l, alpha);
|
|
1626
|
+
}
|
|
1627
|
+
format(): Format {
|
|
1628
|
+
return this.alpha === null || this.alpha === 1 ? Format.HSL : Format.HSLA;
|
|
1629
|
+
}
|
|
1630
|
+
is<T extends Format>(format: T): this is ReturnType<ColorConversions[T]> {
|
|
1631
|
+
return format === this.format();
|
|
1632
|
+
}
|
|
1633
|
+
as<T extends Format>(format: T): ReturnType<ColorConversions[T]> {
|
|
1634
|
+
if (format === this.format()) {
|
|
1635
|
+
return this as ReturnType<ColorConversions[T]>;
|
|
1636
|
+
}
|
|
1637
|
+
return HSL.#conversions[format](this) as ReturnType<ColorConversions[T]>;
|
|
1638
|
+
}
|
|
1639
|
+
asLegacyColor(): Legacy {
|
|
1640
|
+
return this.as(Format.RGBA);
|
|
1641
|
+
}
|
|
1642
|
+
getAuthoredText(): string|null {
|
|
1643
|
+
return this.#authoredText ?? null;
|
|
1644
|
+
}
|
|
1645
|
+
getRawParameters(): Color3D {
|
|
1646
|
+
return [...this.#rawParams];
|
|
1647
|
+
}
|
|
1648
|
+
getAsRawString(format?: Format): string {
|
|
1649
|
+
if (format) {
|
|
1650
|
+
return this.as(format).getAsRawString();
|
|
1651
|
+
}
|
|
1652
|
+
return this.#stringify(...this.#rawParams);
|
|
1653
|
+
}
|
|
1654
|
+
isGamutClipped(): boolean {
|
|
1655
|
+
return !lessOrEquals(this.#rawParams[1], 1) || !lessOrEquals(0, this.#rawParams[1]);
|
|
1656
|
+
}
|
|
1657
|
+
|
|
1658
|
+
static fromSpec(spec: ColorParameterSpec, text: string): HSL|null {
|
|
1659
|
+
const h = parseHueNumeric(spec[0]);
|
|
1660
|
+
if (h === null) {
|
|
1661
|
+
return null;
|
|
1662
|
+
}
|
|
1663
|
+
const s = parseSatLightNumeric(spec[1]);
|
|
1664
|
+
if (s === null) {
|
|
1665
|
+
return null;
|
|
1666
|
+
}
|
|
1667
|
+
const l = parseSatLightNumeric(spec[2]);
|
|
1668
|
+
if (l === null) {
|
|
1669
|
+
return null;
|
|
1670
|
+
}
|
|
1671
|
+
const alpha = parseAlpha(spec[3]);
|
|
1672
|
+
|
|
1673
|
+
return new HSL(h, s, l, alpha, text);
|
|
1674
|
+
}
|
|
1675
|
+
|
|
1676
|
+
hsva(): Color4D {
|
|
1677
|
+
const s = this.s * (this.l < 0.5 ? this.l : 1 - this.l);
|
|
1678
|
+
return [this.h, s !== 0 ? 2 * s / (this.l + s) : 0, (this.l + s), this.alpha ?? 1];
|
|
1679
|
+
}
|
|
1680
|
+
canonicalHSLA(): number[] {
|
|
1681
|
+
return [Math.round(this.h * 360), Math.round(this.s * 100), Math.round(this.l * 100), this.alpha ?? 1];
|
|
1682
|
+
}
|
|
1683
|
+
}
|
|
1684
|
+
|
|
1685
|
+
export class HWB implements Color {
|
|
1686
|
+
readonly h: number;
|
|
1687
|
+
readonly w: number;
|
|
1688
|
+
readonly b: number;
|
|
1689
|
+
readonly alpha: number|null;
|
|
1690
|
+
readonly #rawParams: Color3D;
|
|
1691
|
+
#authoredText: string|undefined;
|
|
1692
|
+
readonly channels: [ColorChannel, ColorChannel, ColorChannel, ColorChannel] =
|
|
1693
|
+
[ColorChannel.H, ColorChannel.W, ColorChannel.B, ColorChannel.ALPHA];
|
|
1694
|
+
|
|
1695
|
+
static readonly #conversions: ColorConversions<HWB> = {
|
|
1696
|
+
[Format.HEX]: (self: HWB) => new Legacy(self.#getRGBArray(/* withAlpha= */ false), Format.HEX),
|
|
1697
|
+
[Format.HEXA]: (self: HWB) => new Legacy(self.#getRGBArray(/* withAlpha= */ true), Format.HEXA),
|
|
1698
|
+
[Format.RGB]: (self: HWB) => new Legacy(self.#getRGBArray(/* withAlpha= */ false), Format.RGB),
|
|
1699
|
+
[Format.RGBA]: (self: HWB) => new Legacy(self.#getRGBArray(/* withAlpha= */ true), Format.RGBA),
|
|
1700
|
+
[Format.HSL]: (self: HWB) => new HSL(...rgbToHsl(self.#getRGBArray(/* withAlpha= */ false)), self.alpha),
|
|
1701
|
+
[Format.HSLA]: (self: HWB) => new HSL(...rgbToHsl(self.#getRGBArray(/* withAlpha= */ false)), self.alpha),
|
|
1702
|
+
[Format.HWB]: (self: HWB) => self,
|
|
1703
|
+
[Format.HWBA]: (self: HWB) => self,
|
|
1704
|
+
[Format.LCH]: (self: HWB) =>
|
|
1705
|
+
new LCH(...ColorConverter.labToLch(...ColorConverter.xyzd50ToLab(...self.#toXyzd50())), self.alpha),
|
|
1706
|
+
[Format.OKLCH]: (self: HWB) => new Oklch(...ColorConverter.xyzd50ToOklch(...self.#toXyzd50()), self.alpha),
|
|
1707
|
+
[Format.LAB]: (self: HWB) => new Lab(...ColorConverter.xyzd50ToLab(...self.#toXyzd50()), self.alpha),
|
|
1708
|
+
[Format.OKLAB]: (self: HWB) =>
|
|
1709
|
+
new Oklab(...ColorConverter.xyzd65ToOklab(...ColorConverter.xyzd50ToD65(...self.#toXyzd50())), self.alpha),
|
|
1710
|
+
|
|
1711
|
+
[Format.SRGB]: (self: HWB) =>
|
|
1712
|
+
new ColorFunction(Format.SRGB, ...ColorConverter.xyzd50ToSrgb(...self.#toXyzd50()), self.alpha),
|
|
1713
|
+
[Format.SRGB_LINEAR]: (self: HWB) =>
|
|
1714
|
+
new ColorFunction(Format.SRGB_LINEAR, ...ColorConverter.xyzd50TosRGBLinear(...self.#toXyzd50()), self.alpha),
|
|
1715
|
+
[Format.DISPLAY_P3]: (self: HWB) =>
|
|
1716
|
+
new ColorFunction(Format.DISPLAY_P3, ...ColorConverter.xyzd50ToDisplayP3(...self.#toXyzd50()), self.alpha),
|
|
1717
|
+
[Format.A98_RGB]: (self: HWB) =>
|
|
1718
|
+
new ColorFunction(Format.A98_RGB, ...ColorConverter.xyzd50ToAdobeRGB(...self.#toXyzd50()), self.alpha),
|
|
1719
|
+
[Format.PROPHOTO_RGB]: (self: HWB) =>
|
|
1720
|
+
new ColorFunction(Format.PROPHOTO_RGB, ...ColorConverter.xyzd50ToProPhoto(...self.#toXyzd50()), self.alpha),
|
|
1721
|
+
[Format.REC_2020]: (self: HWB) =>
|
|
1722
|
+
new ColorFunction(Format.REC_2020, ...ColorConverter.xyzd50ToRec2020(...self.#toXyzd50()), self.alpha),
|
|
1723
|
+
[Format.XYZ]: (self: HWB) =>
|
|
1724
|
+
new ColorFunction(Format.XYZ, ...ColorConverter.xyzd50ToD65(...self.#toXyzd50()), self.alpha),
|
|
1725
|
+
[Format.XYZ_D50]: (self: HWB) => new ColorFunction(Format.XYZ_D50, ...self.#toXyzd50(), self.alpha),
|
|
1726
|
+
[Format.XYZ_D65]: (self: HWB) =>
|
|
1727
|
+
new ColorFunction(Format.XYZ_D65, ...ColorConverter.xyzd50ToD65(...self.#toXyzd50()), self.alpha),
|
|
1728
|
+
};
|
|
1729
|
+
|
|
1730
|
+
#getRGBArray(withAlpha: true): Color4DOr3D;
|
|
1731
|
+
#getRGBArray(withAlpha: false): Color3D;
|
|
1732
|
+
#getRGBArray(withAlpha = true): Color4DOr3D|Color3D {
|
|
1733
|
+
const rgb = hwb2rgb([this.h, this.w, this.b, 0]);
|
|
1734
|
+
if (withAlpha) {
|
|
1735
|
+
return [rgb[0], rgb[1], rgb[2], this.alpha ?? undefined];
|
|
1736
|
+
}
|
|
1737
|
+
return [rgb[0], rgb[1], rgb[2]];
|
|
1738
|
+
}
|
|
1739
|
+
|
|
1740
|
+
#toXyzd50(): Color3D {
|
|
1741
|
+
const rgb = this.#getRGBArray(false);
|
|
1742
|
+
return ColorConverter.srgbToXyzd50(rgb[0], rgb[1], rgb[2]);
|
|
1743
|
+
}
|
|
1744
|
+
constructor(h: number, w: number, b: number, alpha: number|null, authoredText?: string) {
|
|
1745
|
+
this.#rawParams = [h, w, b];
|
|
1746
|
+
this.w = clamp(w, {min: 0, max: 1});
|
|
1747
|
+
this.b = clamp(b, {min: 0, max: 1});
|
|
1748
|
+
h = lessOrEquals(1, this.w + this.b) ? 0 : h;
|
|
1749
|
+
this.h = normalizeHue(h * 360) / 360;
|
|
1750
|
+
this.alpha = clamp(alpha, {min: 0, max: 1});
|
|
1751
|
+
if (lessOrEquals(1, this.w + this.b)) {
|
|
1752
|
+
// normalize to a sum of 100% respecting the ratio, see https://www.w3.org/TR/css-color-4/#the-hwb-notation
|
|
1753
|
+
const ratio = this.w / this.b;
|
|
1754
|
+
this.b = 1 / (1 + ratio);
|
|
1755
|
+
this.w = 1 - this.b;
|
|
1756
|
+
}
|
|
1757
|
+
this.#authoredText = authoredText;
|
|
1758
|
+
}
|
|
1759
|
+
equal(color: Color): boolean {
|
|
1760
|
+
const hwb = color.as(Format.HWB);
|
|
1761
|
+
return equals(this.h, hwb.h) && equals(this.w, hwb.w) && equals(this.b, hwb.b) && equals(this.alpha, hwb.alpha);
|
|
1762
|
+
}
|
|
1763
|
+
asString(format?: Format|undefined): string {
|
|
1764
|
+
if (format) {
|
|
1765
|
+
return this.as(format).asString();
|
|
1766
|
+
}
|
|
1767
|
+
return this.#stringify(this.h, this.w, this.b);
|
|
1768
|
+
}
|
|
1769
|
+
#stringify(h: number, w: number, b: number): string {
|
|
1770
|
+
const start = Platform.StringUtilities.sprintf(
|
|
1771
|
+
'hwb(%sdeg %s% %s%', Platform.StringUtilities.stringifyWithPrecision(h * 360),
|
|
1772
|
+
Platform.StringUtilities.stringifyWithPrecision(w * 100),
|
|
1773
|
+
Platform.StringUtilities.stringifyWithPrecision(b * 100));
|
|
1774
|
+
if (this.alpha !== null && this.alpha !== 1) {
|
|
1775
|
+
return start +
|
|
1776
|
+
Platform.StringUtilities.sprintf(
|
|
1777
|
+
' / %s%)', Platform.StringUtilities.stringifyWithPrecision(this.alpha * 100));
|
|
1778
|
+
}
|
|
1779
|
+
return start + ')';
|
|
1780
|
+
}
|
|
1781
|
+
setAlpha(alpha: number): HWB {
|
|
1782
|
+
return new HWB(this.h, this.w, this.b, alpha, this.#authoredText);
|
|
1783
|
+
}
|
|
1784
|
+
format(): Format {
|
|
1785
|
+
return this.alpha !== null && !equals(this.alpha, 1) ? Format.HWBA : Format.HWB;
|
|
1786
|
+
}
|
|
1787
|
+
is<T extends Format>(format: T): this is ReturnType<ColorConversions[T]> {
|
|
1788
|
+
return format === this.format();
|
|
1789
|
+
}
|
|
1790
|
+
as<T extends Format>(format: T): ReturnType<ColorConversions[T]> {
|
|
1791
|
+
if (format === this.format()) {
|
|
1792
|
+
return this as ReturnType<ColorConversions[T]>;
|
|
1793
|
+
}
|
|
1794
|
+
return HWB.#conversions[format](this) as ReturnType<ColorConversions[T]>;
|
|
1795
|
+
}
|
|
1796
|
+
asLegacyColor(): Legacy {
|
|
1797
|
+
return this.as(Format.RGBA);
|
|
1798
|
+
}
|
|
1799
|
+
getAuthoredText(): string|null {
|
|
1800
|
+
return this.#authoredText ?? null;
|
|
1801
|
+
}
|
|
1802
|
+
|
|
1803
|
+
canonicalHWBA(): number[] {
|
|
1804
|
+
return [
|
|
1805
|
+
Math.round(this.h * 360),
|
|
1806
|
+
Math.round(this.w * 100),
|
|
1807
|
+
Math.round(this.b * 100),
|
|
1808
|
+
this.alpha ?? 1,
|
|
1809
|
+
];
|
|
1810
|
+
}
|
|
1811
|
+
getRawParameters(): Color3D {
|
|
1812
|
+
return [...this.#rawParams];
|
|
1813
|
+
}
|
|
1814
|
+
getAsRawString(format?: Format): string {
|
|
1815
|
+
if (format) {
|
|
1816
|
+
return this.as(format).getAsRawString();
|
|
1817
|
+
}
|
|
1818
|
+
return this.#stringify(...this.#rawParams);
|
|
1819
|
+
}
|
|
1820
|
+
isGamutClipped(): boolean {
|
|
1821
|
+
return !lessOrEquals(this.#rawParams[1], 1) || !lessOrEquals(0, this.#rawParams[1]) ||
|
|
1822
|
+
!lessOrEquals(this.#rawParams[2], 1) || !lessOrEquals(0, this.#rawParams[2]);
|
|
1823
|
+
}
|
|
1824
|
+
|
|
1825
|
+
static fromSpec(spec: ColorParameterSpec, text: string): HWB|null {
|
|
1826
|
+
const h = parseHueNumeric(spec[0]);
|
|
1827
|
+
if (h === null) {
|
|
1828
|
+
return null;
|
|
1829
|
+
}
|
|
1830
|
+
const w = parseSatLightNumeric(spec[1]);
|
|
1831
|
+
if (w === null) {
|
|
1832
|
+
return null;
|
|
1833
|
+
}
|
|
1834
|
+
const b = parseSatLightNumeric(spec[2]);
|
|
1835
|
+
if (b === null) {
|
|
1836
|
+
return null;
|
|
1837
|
+
}
|
|
1838
|
+
const alpha = parseAlpha(spec[3]);
|
|
1839
|
+
return new HWB(h, w, b, alpha, text);
|
|
1840
|
+
}
|
|
1841
|
+
}
|
|
1842
|
+
|
|
1843
|
+
type LegacyColor = Format.HEX|Format.HEXA|Format.RGB|Format.RGBA;
|
|
1844
|
+
|
|
1845
|
+
function toRgbValue(value: number): number {
|
|
1846
|
+
return Math.round(value * 255);
|
|
1847
|
+
}
|
|
1848
|
+
|
|
1849
|
+
abstract class ShortFormatColorBase implements Color {
|
|
1850
|
+
protected readonly color: Legacy;
|
|
1851
|
+
readonly channels: [ColorChannel, ColorChannel, ColorChannel, ColorChannel] =
|
|
1852
|
+
[ColorChannel.R, ColorChannel.G, ColorChannel.B, ColorChannel.ALPHA];
|
|
1853
|
+
constructor(color: Legacy) {
|
|
1854
|
+
this.color = color;
|
|
1855
|
+
}
|
|
1856
|
+
get alpha(): number|null {
|
|
1857
|
+
return this.color.alpha;
|
|
1858
|
+
}
|
|
1859
|
+
rgba(): Color4D {
|
|
1860
|
+
return this.color.rgba();
|
|
1861
|
+
}
|
|
1862
|
+
equal(color: Color): boolean {
|
|
1863
|
+
return this.color.equal(color);
|
|
1864
|
+
}
|
|
1865
|
+
setAlpha(alpha: number): Color {
|
|
1866
|
+
return this.color.setAlpha(alpha);
|
|
1867
|
+
}
|
|
1868
|
+
format(): Format {
|
|
1869
|
+
return (this.alpha ?? 1) !== 1 ? Format.HEXA : Format.HEX;
|
|
1870
|
+
}
|
|
1871
|
+
as<T extends Format>(format: T): ReturnType<ColorConversions<void>[T]> {
|
|
1872
|
+
return this.color.as(format);
|
|
1873
|
+
}
|
|
1874
|
+
is<T extends Format>(format: T): this is ReturnType<ColorConversions<void>[T]> {
|
|
1875
|
+
return this.color.is(format);
|
|
1876
|
+
}
|
|
1877
|
+
asLegacyColor(): Legacy {
|
|
1878
|
+
return this.color.asLegacyColor();
|
|
1879
|
+
}
|
|
1880
|
+
getAuthoredText(): string|null {
|
|
1881
|
+
return this.color.getAuthoredText();
|
|
1882
|
+
}
|
|
1883
|
+
getRawParameters(): Color3D {
|
|
1884
|
+
return this.color.getRawParameters();
|
|
1885
|
+
}
|
|
1886
|
+
isGamutClipped(): boolean {
|
|
1887
|
+
return this.color.isGamutClipped();
|
|
1888
|
+
}
|
|
1889
|
+
asString(format?: Format|undefined): string {
|
|
1890
|
+
if (format) {
|
|
1891
|
+
return this.as(format).asString();
|
|
1892
|
+
}
|
|
1893
|
+
const [r, g, b] = this.color.rgba();
|
|
1894
|
+
return this.stringify(r, g, b);
|
|
1895
|
+
}
|
|
1896
|
+
getAsRawString(format?: Format): string {
|
|
1897
|
+
if (format) {
|
|
1898
|
+
return this.as(format).getAsRawString();
|
|
1899
|
+
}
|
|
1900
|
+
const [r, g, b] = this.getRawParameters();
|
|
1901
|
+
return this.stringify(r, g, b);
|
|
1902
|
+
}
|
|
1903
|
+
|
|
1904
|
+
protected abstract stringify(r: number, g: number, b: number): string;
|
|
1905
|
+
}
|
|
1906
|
+
|
|
1907
|
+
export class ShortHex extends ShortFormatColorBase {
|
|
1908
|
+
override setAlpha(alpha: number): Color {
|
|
1909
|
+
return new ShortHex(this.color.setAlpha(alpha));
|
|
1910
|
+
}
|
|
1911
|
+
|
|
1912
|
+
override asString(format?: Format|undefined): string {
|
|
1913
|
+
return format && format !== this.format() ? super.as(format).asString() : super.asString();
|
|
1914
|
+
}
|
|
1915
|
+
|
|
1916
|
+
protected override stringify(r: number, g: number, b: number): string {
|
|
1917
|
+
function toShortHexValue(value: number): string {
|
|
1918
|
+
return (Math.round(value * 255) / 17).toString(16);
|
|
1919
|
+
}
|
|
1920
|
+
|
|
1921
|
+
if (this.color.hasAlpha()) {
|
|
1922
|
+
return Platform.StringUtilities
|
|
1923
|
+
.sprintf(
|
|
1924
|
+
'#%s%s%s%s', toShortHexValue(r), toShortHexValue(g), toShortHexValue(b), toShortHexValue(this.alpha ?? 1))
|
|
1925
|
+
.toLowerCase();
|
|
1926
|
+
}
|
|
1927
|
+
return Platform.StringUtilities.sprintf('#%s%s%s', toShortHexValue(r), toShortHexValue(g), toShortHexValue(b))
|
|
1928
|
+
.toLowerCase();
|
|
1929
|
+
}
|
|
1930
|
+
}
|
|
1931
|
+
|
|
1932
|
+
export class Nickname extends ShortFormatColorBase {
|
|
1933
|
+
readonly nickname: string;
|
|
1934
|
+
constructor(nickname: string, color: Legacy) {
|
|
1935
|
+
super(color);
|
|
1936
|
+
this.nickname = nickname;
|
|
1937
|
+
}
|
|
1938
|
+
|
|
1939
|
+
static fromName(name: string, text: string): Nickname|null {
|
|
1940
|
+
const nickname = name.toLowerCase();
|
|
1941
|
+
const rgba = Nicknames.get(nickname);
|
|
1942
|
+
if (rgba !== undefined) {
|
|
1943
|
+
return new Nickname(nickname, Legacy.fromRGBA(rgba, text));
|
|
1944
|
+
}
|
|
1945
|
+
return null;
|
|
1946
|
+
}
|
|
1947
|
+
|
|
1948
|
+
protected override stringify(): string {
|
|
1949
|
+
return this.nickname;
|
|
1950
|
+
}
|
|
1951
|
+
|
|
1952
|
+
override getAsRawString(format?: Format|undefined): string {
|
|
1953
|
+
return this.color.getAsRawString(format);
|
|
1954
|
+
}
|
|
1955
|
+
}
|
|
1956
|
+
|
|
1957
|
+
export class Legacy implements Color {
|
|
1958
|
+
readonly #rawParams: Color3D;
|
|
1959
|
+
#rgba: Color4D;
|
|
1960
|
+
readonly #authoredText: string|null;
|
|
1961
|
+
#format: LegacyColor;
|
|
1962
|
+
readonly channels: [ColorChannel, ColorChannel, ColorChannel, ColorChannel] =
|
|
1963
|
+
[ColorChannel.R, ColorChannel.G, ColorChannel.B, ColorChannel.ALPHA];
|
|
1964
|
+
|
|
1965
|
+
static readonly #conversions: ColorConversions<Legacy> = {
|
|
1966
|
+
[Format.HEX]: (self: Legacy) => new Legacy(self.#rgba, Format.HEX),
|
|
1967
|
+
[Format.HEXA]: (self: Legacy) => new Legacy(self.#rgba, Format.HEXA),
|
|
1968
|
+
[Format.RGB]: (self: Legacy) => new Legacy(self.#rgba, Format.RGB),
|
|
1969
|
+
[Format.RGBA]: (self: Legacy) => new Legacy(self.#rgba, Format.RGBA),
|
|
1970
|
+
[Format.HSL]: (self: Legacy) => new HSL(...rgbToHsl([self.#rgba[0], self.#rgba[1], self.#rgba[2]]), self.alpha),
|
|
1971
|
+
[Format.HSLA]: (self: Legacy) => new HSL(...rgbToHsl([self.#rgba[0], self.#rgba[1], self.#rgba[2]]), self.alpha),
|
|
1972
|
+
[Format.HWB]: (self: Legacy) => new HWB(...rgbToHwb([self.#rgba[0], self.#rgba[1], self.#rgba[2]]), self.alpha),
|
|
1973
|
+
[Format.HWBA]: (self: Legacy) => new HWB(...rgbToHwb([self.#rgba[0], self.#rgba[1], self.#rgba[2]]), self.alpha),
|
|
1974
|
+
[Format.LCH]: (self: Legacy) =>
|
|
1975
|
+
new LCH(...ColorConverter.labToLch(...ColorConverter.xyzd50ToLab(...self.#toXyzd50())), self.alpha),
|
|
1976
|
+
[Format.OKLCH]: (self: Legacy) => new Oklch(...ColorConverter.xyzd50ToOklch(...self.#toXyzd50()), self.alpha),
|
|
1977
|
+
[Format.LAB]: (self: Legacy) => new Lab(...ColorConverter.xyzd50ToLab(...self.#toXyzd50()), self.alpha),
|
|
1978
|
+
[Format.OKLAB]: (self: Legacy) =>
|
|
1979
|
+
new Oklab(...ColorConverter.xyzd65ToOklab(...ColorConverter.xyzd50ToD65(...self.#toXyzd50())), self.alpha),
|
|
1980
|
+
[Format.SRGB]: (self: Legacy) =>
|
|
1981
|
+
new ColorFunction(Format.SRGB, ...ColorConverter.xyzd50ToSrgb(...self.#toXyzd50()), self.alpha),
|
|
1982
|
+
[Format.SRGB_LINEAR]: (self: Legacy) =>
|
|
1983
|
+
new ColorFunction(Format.SRGB_LINEAR, ...ColorConverter.xyzd50TosRGBLinear(...self.#toXyzd50()), self.alpha),
|
|
1984
|
+
[Format.DISPLAY_P3]: (self: Legacy) =>
|
|
1985
|
+
new ColorFunction(Format.DISPLAY_P3, ...ColorConverter.xyzd50ToDisplayP3(...self.#toXyzd50()), self.alpha),
|
|
1986
|
+
[Format.A98_RGB]: (self: Legacy) =>
|
|
1987
|
+
new ColorFunction(Format.A98_RGB, ...ColorConverter.xyzd50ToAdobeRGB(...self.#toXyzd50()), self.alpha),
|
|
1988
|
+
[Format.PROPHOTO_RGB]: (self: Legacy) =>
|
|
1989
|
+
new ColorFunction(Format.PROPHOTO_RGB, ...ColorConverter.xyzd50ToProPhoto(...self.#toXyzd50()), self.alpha),
|
|
1990
|
+
[Format.REC_2020]: (self: Legacy) =>
|
|
1991
|
+
new ColorFunction(Format.REC_2020, ...ColorConverter.xyzd50ToRec2020(...self.#toXyzd50()), self.alpha),
|
|
1992
|
+
[Format.XYZ]: (self: Legacy) =>
|
|
1993
|
+
new ColorFunction(Format.XYZ, ...ColorConverter.xyzd50ToD65(...self.#toXyzd50()), self.alpha),
|
|
1994
|
+
[Format.XYZ_D50]: (self: Legacy) => new ColorFunction(Format.XYZ_D50, ...self.#toXyzd50(), self.alpha),
|
|
1995
|
+
[Format.XYZ_D65]: (self: Legacy) =>
|
|
1996
|
+
new ColorFunction(Format.XYZ_D65, ...ColorConverter.xyzd50ToD65(...self.#toXyzd50()), self.alpha),
|
|
1997
|
+
};
|
|
1998
|
+
|
|
1999
|
+
#toXyzd50(): Color3D {
|
|
2000
|
+
const [r, g, b] = this.#rgba;
|
|
2001
|
+
return ColorConverter.srgbToXyzd50(r, g, b);
|
|
2002
|
+
}
|
|
2003
|
+
|
|
2004
|
+
get alpha(): number|null {
|
|
2005
|
+
switch (this.format()) {
|
|
2006
|
+
case Format.HEXA:
|
|
2007
|
+
case Format.RGBA:
|
|
2008
|
+
return this.#rgba[3];
|
|
2009
|
+
default:
|
|
2010
|
+
return null;
|
|
2011
|
+
}
|
|
2012
|
+
}
|
|
2013
|
+
|
|
2014
|
+
asLegacyColor(): Legacy {
|
|
2015
|
+
return this;
|
|
2016
|
+
}
|
|
2017
|
+
|
|
2018
|
+
nickname(): Nickname|null {
|
|
2019
|
+
const nickname = RGBAToNickname.get(String(this.canonicalRGBA()));
|
|
2020
|
+
return nickname ? new Nickname(nickname, this) : null;
|
|
2021
|
+
}
|
|
2022
|
+
|
|
2023
|
+
shortHex(): ShortHex|null {
|
|
2024
|
+
for (let i = 0; i < 4; ++i) {
|
|
2025
|
+
const c = Math.round(this.#rgba[i] * 255);
|
|
2026
|
+
// Check if the two digits of each are identical: #aabbcc => #abc
|
|
2027
|
+
if (c % 0x11) {
|
|
2028
|
+
return null;
|
|
2029
|
+
}
|
|
2030
|
+
}
|
|
2031
|
+
return new ShortHex(this);
|
|
2032
|
+
}
|
|
2033
|
+
|
|
2034
|
+
constructor(rgba: Color3D|Color4DOr3D, format: LegacyColor, authoredText?: string) {
|
|
2035
|
+
this.#authoredText = authoredText || null;
|
|
2036
|
+
this.#format = format;
|
|
2037
|
+
this.#rawParams = [rgba[0], rgba[1], rgba[2]];
|
|
2038
|
+
|
|
2039
|
+
this.#rgba = [
|
|
2040
|
+
clamp(rgba[0], {min: 0, max: 1}),
|
|
2041
|
+
clamp(rgba[1], {min: 0, max: 1}),
|
|
2042
|
+
clamp(rgba[2], {min: 0, max: 1}),
|
|
2043
|
+
clamp(rgba[3] ?? 1, {min: 0, max: 1}),
|
|
2044
|
+
];
|
|
2045
|
+
}
|
|
2046
|
+
|
|
2047
|
+
static fromHex(hex: string, text: string): Legacy|ShortHex {
|
|
2048
|
+
hex = hex.toLowerCase();
|
|
2049
|
+
// Possible hex representations with alpha are fffA and ffffffAA
|
|
2050
|
+
const hasAlpha = hex.length === 4 || hex.length === 8;
|
|
2051
|
+
const format = hasAlpha ? Format.HEXA : Format.HEX;
|
|
2052
|
+
const isShort = hex.length <= 4;
|
|
2053
|
+
if (isShort) {
|
|
2054
|
+
hex = hex.charAt(0) + hex.charAt(0) + hex.charAt(1) + hex.charAt(1) + hex.charAt(2) + hex.charAt(2) +
|
|
2055
|
+
hex.charAt(3) + hex.charAt(3);
|
|
2056
|
+
}
|
|
2057
|
+
const r = parseInt(hex.substring(0, 2), 16);
|
|
2058
|
+
const g = parseInt(hex.substring(2, 4), 16);
|
|
2059
|
+
const b = parseInt(hex.substring(4, 6), 16);
|
|
2060
|
+
let a = 1;
|
|
2061
|
+
if (hex.length === 8) {
|
|
2062
|
+
a = parseInt(hex.substring(6, 8), 16) / 255;
|
|
2063
|
+
}
|
|
2064
|
+
const color = new Legacy([r / 255, g / 255, b / 255, a], format, text);
|
|
2065
|
+
return isShort ? new ShortHex(color) : color;
|
|
2066
|
+
}
|
|
2067
|
+
|
|
2068
|
+
static fromRGBAFunction(r: string, g: string, b: string, alpha: string|undefined, text: string): Legacy|null {
|
|
2069
|
+
const rgba = [
|
|
2070
|
+
parseRgbNumeric(r),
|
|
2071
|
+
parseRgbNumeric(g),
|
|
2072
|
+
parseRgbNumeric(b),
|
|
2073
|
+
alpha ? parseAlphaNumeric(alpha) : 1,
|
|
2074
|
+
];
|
|
2075
|
+
|
|
2076
|
+
if (!Platform.ArrayUtilities.arrayDoesNotContainNullOrUndefined(rgba)) {
|
|
2077
|
+
return null;
|
|
2078
|
+
}
|
|
2079
|
+
return new Legacy(rgba as Color4D, alpha ? Format.RGBA : Format.RGB, text);
|
|
2080
|
+
}
|
|
2081
|
+
|
|
2082
|
+
static fromRGBA(rgba: number[], authoredText?: string): Legacy {
|
|
2083
|
+
return new Legacy([rgba[0] / 255, rgba[1] / 255, rgba[2] / 255, rgba[3]], Format.RGBA, authoredText);
|
|
2084
|
+
}
|
|
2085
|
+
|
|
2086
|
+
static fromHSVA(hsva: Color4D): Legacy {
|
|
2087
|
+
const rgba = hsva2rgba(hsva);
|
|
2088
|
+
return new Legacy(rgba, Format.RGBA);
|
|
2089
|
+
}
|
|
2090
|
+
|
|
2091
|
+
is<T extends Format>(format: T): this is ReturnType<ColorConversions[T]> {
|
|
2092
|
+
return format === this.format();
|
|
2093
|
+
}
|
|
2094
|
+
as<T extends Format>(format: T): ReturnType<ColorConversions[T]> {
|
|
2095
|
+
if (format === this.format()) {
|
|
2096
|
+
return this as ReturnType<ColorConversions[T]>;
|
|
2097
|
+
}
|
|
2098
|
+
return Legacy.#conversions[format](this) as ReturnType<ColorConversions[T]>;
|
|
2099
|
+
}
|
|
2100
|
+
|
|
2101
|
+
format(): LegacyColor {
|
|
2102
|
+
return this.#format;
|
|
2103
|
+
}
|
|
2104
|
+
|
|
2105
|
+
hasAlpha(): boolean {
|
|
2106
|
+
return this.#rgba[3] !== 1;
|
|
2107
|
+
}
|
|
2108
|
+
|
|
2109
|
+
detectHEXFormat(): Format {
|
|
2110
|
+
const hasAlpha = this.hasAlpha();
|
|
2111
|
+
return hasAlpha ? Format.HEXA : Format.HEX;
|
|
2112
|
+
}
|
|
2113
|
+
|
|
2114
|
+
asString(format?: Format): string {
|
|
2115
|
+
if (format) {
|
|
2116
|
+
return this.as(format).asString();
|
|
2117
|
+
}
|
|
2118
|
+
return this.#stringify(format, this.#rgba[0], this.#rgba[1], this.#rgba[2]);
|
|
2119
|
+
}
|
|
2120
|
+
#stringify(format: LegacyColor|undefined, r: number, g: number, b: number): string {
|
|
2121
|
+
if (!format) {
|
|
2122
|
+
format = this.#format;
|
|
2123
|
+
}
|
|
2124
|
+
|
|
2125
|
+
function toHexValue(value: number): string {
|
|
2126
|
+
const hex = Math.round(value * 255).toString(16);
|
|
2127
|
+
return hex.length === 1 ? '0' + hex : hex;
|
|
2128
|
+
}
|
|
2129
|
+
|
|
2130
|
+
switch (format) {
|
|
2131
|
+
case Format.RGB:
|
|
2132
|
+
case Format.RGBA: {
|
|
2133
|
+
const start = Platform.StringUtilities.sprintf('rgb(%d %d %d', toRgbValue(r), toRgbValue(g), toRgbValue(b));
|
|
2134
|
+
if (this.hasAlpha()) {
|
|
2135
|
+
return start + Platform.StringUtilities.sprintf(' / %d%)', Math.round(this.#rgba[3] * 100));
|
|
2136
|
+
}
|
|
2137
|
+
return start + ')';
|
|
2138
|
+
}
|
|
2139
|
+
case Format.HEX:
|
|
2140
|
+
case Format.HEXA: {
|
|
2141
|
+
if (this.hasAlpha()) {
|
|
2142
|
+
return Platform.StringUtilities
|
|
2143
|
+
.sprintf('#%s%s%s%s', toHexValue(r), toHexValue(g), toHexValue(b), toHexValue(this.#rgba[3]))
|
|
2144
|
+
.toLowerCase();
|
|
2145
|
+
}
|
|
2146
|
+
return Platform.StringUtilities.sprintf('#%s%s%s', toHexValue(r), toHexValue(g), toHexValue(b)).toLowerCase();
|
|
2147
|
+
}
|
|
2148
|
+
}
|
|
2149
|
+
}
|
|
2150
|
+
getAuthoredText(): string|null {
|
|
2151
|
+
return this.#authoredText ?? null;
|
|
2152
|
+
}
|
|
2153
|
+
|
|
2154
|
+
getRawParameters(): Color3D {
|
|
2155
|
+
return [...this.#rawParams];
|
|
2156
|
+
}
|
|
2157
|
+
getAsRawString(format?: Format): string {
|
|
2158
|
+
if (format) {
|
|
2159
|
+
return this.as(format).getAsRawString();
|
|
2160
|
+
}
|
|
2161
|
+
return this.#stringify(format, ...this.#rawParams);
|
|
2162
|
+
}
|
|
2163
|
+
isGamutClipped(): boolean {
|
|
2164
|
+
return !equals(
|
|
2165
|
+
this.#rawParams.map(toRgbValue), [this.#rgba[0], this.#rgba[1], this.#rgba[2]].map(toRgbValue),
|
|
2166
|
+
WIDE_RANGE_EPSILON);
|
|
2167
|
+
}
|
|
2168
|
+
|
|
2169
|
+
rgba(): Color4D {
|
|
2170
|
+
return [...this.#rgba];
|
|
2171
|
+
}
|
|
2172
|
+
|
|
2173
|
+
canonicalRGBA(): Color4D {
|
|
2174
|
+
const rgba = new Array(4);
|
|
2175
|
+
for (let i = 0; i < 3; ++i) {
|
|
2176
|
+
rgba[i] = Math.round(this.#rgba[i] * 255);
|
|
2177
|
+
}
|
|
2178
|
+
rgba[3] = this.#rgba[3];
|
|
2179
|
+
return rgba as Color4D;
|
|
2180
|
+
}
|
|
2181
|
+
|
|
2182
|
+
toProtocolRGBA(): {
|
|
2183
|
+
r: number,
|
|
2184
|
+
g: number,
|
|
2185
|
+
b: number,
|
|
2186
|
+
a: (number|undefined),
|
|
2187
|
+
} {
|
|
2188
|
+
const rgba = this.canonicalRGBA();
|
|
2189
|
+
const result: {
|
|
2190
|
+
r: number,
|
|
2191
|
+
g: number,
|
|
2192
|
+
b: number,
|
|
2193
|
+
a: number|undefined,
|
|
2194
|
+
} = {r: rgba[0], g: rgba[1], b: rgba[2], a: undefined};
|
|
2195
|
+
if (rgba[3] !== 1) {
|
|
2196
|
+
result.a = rgba[3];
|
|
2197
|
+
}
|
|
2198
|
+
return result;
|
|
2199
|
+
}
|
|
2200
|
+
|
|
2201
|
+
invert(): Legacy {
|
|
2202
|
+
const rgba: Color4D = [0, 0, 0, 0];
|
|
2203
|
+
rgba[0] = 1 - this.#rgba[0];
|
|
2204
|
+
rgba[1] = 1 - this.#rgba[1];
|
|
2205
|
+
rgba[2] = 1 - this.#rgba[2];
|
|
2206
|
+
rgba[3] = this.#rgba[3];
|
|
2207
|
+
return new Legacy(rgba, Format.RGBA);
|
|
2208
|
+
}
|
|
2209
|
+
|
|
2210
|
+
/**
|
|
2211
|
+
* Returns a new color using the NTSC formula for making a RGB color grayscale.
|
|
2212
|
+
* Note: We override with an alpha of 50% to enhance the dimming effect.
|
|
2213
|
+
*/
|
|
2214
|
+
grayscale(): Legacy {
|
|
2215
|
+
const [r, g, b] = this.#rgba;
|
|
2216
|
+
const gray = r * 0.299 + g * 0.587 + b * 0.114;
|
|
2217
|
+
return new Legacy([gray, gray, gray, 0.5], Format.RGBA);
|
|
2218
|
+
}
|
|
2219
|
+
|
|
2220
|
+
setAlpha(alpha: number): Legacy {
|
|
2221
|
+
const rgba: Color4D = [...this.#rgba];
|
|
2222
|
+
rgba[3] = alpha;
|
|
2223
|
+
return new Legacy(rgba, Format.RGBA);
|
|
2224
|
+
}
|
|
2225
|
+
|
|
2226
|
+
blendWith(fgColor: Legacy): Legacy {
|
|
2227
|
+
const rgba: Color4D = blendColors(fgColor.#rgba, this.#rgba);
|
|
2228
|
+
return new Legacy(rgba, Format.RGBA);
|
|
2229
|
+
}
|
|
2230
|
+
|
|
2231
|
+
blendWithAlpha(alpha: number): Legacy {
|
|
2232
|
+
const rgba: Color4D = [...this.#rgba];
|
|
2233
|
+
rgba[3] *= alpha;
|
|
2234
|
+
return new Legacy(rgba, Format.RGBA);
|
|
2235
|
+
}
|
|
2236
|
+
|
|
2237
|
+
setFormat(format: LegacyColor): void {
|
|
2238
|
+
this.#format = format;
|
|
2239
|
+
}
|
|
2240
|
+
|
|
2241
|
+
equal(other: Color): boolean {
|
|
2242
|
+
const legacy = other.as(this.#format);
|
|
2243
|
+
return equals(toRgbValue(this.#rgba[0]), toRgbValue(legacy.#rgba[0]), WIDE_RANGE_EPSILON) &&
|
|
2244
|
+
equals(toRgbValue(this.#rgba[1]), toRgbValue(legacy.#rgba[1]), WIDE_RANGE_EPSILON) &&
|
|
2245
|
+
equals(toRgbValue(this.#rgba[2]), toRgbValue(legacy.#rgba[2]), WIDE_RANGE_EPSILON) &&
|
|
2246
|
+
equals(this.#rgba[3], legacy.#rgba[3]);
|
|
2247
|
+
}
|
|
2248
|
+
}
|
|
2249
|
+
|
|
2250
|
+
export const Regex =
|
|
2251
|
+
/((?:rgba?|hsla?|hwba?|lab|lch|oklab|oklch|color)\([^)]+\)|#[0-9a-fA-F]{8}|#[0-9a-fA-F]{6}|#[0-9a-fA-F]{3,4}|\b[a-zA-Z]+\b(?!-))/g;
|
|
2252
|
+
export const ColorMixRegex = /color-mix\(.*,\s*(?<firstColor>.+)\s*,\s*(?<secondColor>.+)\s*\)/g;
|
|
2253
|
+
|
|
2254
|
+
const COLOR_TO_RGBA_ENTRIES: Array<readonly[string, number[]]> = [
|
|
2255
|
+
['aliceblue', [240, 248, 255]],
|
|
2256
|
+
['antiquewhite', [250, 235, 215]],
|
|
2257
|
+
['aqua', [0, 255, 255]],
|
|
2258
|
+
['aquamarine', [127, 255, 212]],
|
|
2259
|
+
['azure', [240, 255, 255]],
|
|
2260
|
+
['beige', [245, 245, 220]],
|
|
2261
|
+
['bisque', [255, 228, 196]],
|
|
2262
|
+
['black', [0, 0, 0]],
|
|
2263
|
+
['blanchedalmond', [255, 235, 205]],
|
|
2264
|
+
['blue', [0, 0, 255]],
|
|
2265
|
+
['blueviolet', [138, 43, 226]],
|
|
2266
|
+
['brown', [165, 42, 42]],
|
|
2267
|
+
['burlywood', [222, 184, 135]],
|
|
2268
|
+
['cadetblue', [95, 158, 160]],
|
|
2269
|
+
['chartreuse', [127, 255, 0]],
|
|
2270
|
+
['chocolate', [210, 105, 30]],
|
|
2271
|
+
['coral', [255, 127, 80]],
|
|
2272
|
+
['cornflowerblue', [100, 149, 237]],
|
|
2273
|
+
['cornsilk', [255, 248, 220]],
|
|
2274
|
+
['crimson', [237, 20, 61]],
|
|
2275
|
+
['cyan', [0, 255, 255]],
|
|
2276
|
+
['darkblue', [0, 0, 139]],
|
|
2277
|
+
['darkcyan', [0, 139, 139]],
|
|
2278
|
+
['darkgoldenrod', [184, 134, 11]],
|
|
2279
|
+
['darkgray', [169, 169, 169]],
|
|
2280
|
+
['darkgrey', [169, 169, 169]],
|
|
2281
|
+
['darkgreen', [0, 100, 0]],
|
|
2282
|
+
['darkkhaki', [189, 183, 107]],
|
|
2283
|
+
['darkmagenta', [139, 0, 139]],
|
|
2284
|
+
['darkolivegreen', [85, 107, 47]],
|
|
2285
|
+
['darkorange', [255, 140, 0]],
|
|
2286
|
+
['darkorchid', [153, 50, 204]],
|
|
2287
|
+
['darkred', [139, 0, 0]],
|
|
2288
|
+
['darksalmon', [233, 150, 122]],
|
|
2289
|
+
['darkseagreen', [143, 188, 143]],
|
|
2290
|
+
['darkslateblue', [72, 61, 139]],
|
|
2291
|
+
['darkslategray', [47, 79, 79]],
|
|
2292
|
+
['darkslategrey', [47, 79, 79]],
|
|
2293
|
+
['darkturquoise', [0, 206, 209]],
|
|
2294
|
+
['darkviolet', [148, 0, 211]],
|
|
2295
|
+
['deeppink', [255, 20, 147]],
|
|
2296
|
+
['deepskyblue', [0, 191, 255]],
|
|
2297
|
+
['dimgray', [105, 105, 105]],
|
|
2298
|
+
['dimgrey', [105, 105, 105]],
|
|
2299
|
+
['dodgerblue', [30, 144, 255]],
|
|
2300
|
+
['firebrick', [178, 34, 34]],
|
|
2301
|
+
['floralwhite', [255, 250, 240]],
|
|
2302
|
+
['forestgreen', [34, 139, 34]],
|
|
2303
|
+
['fuchsia', [255, 0, 255]],
|
|
2304
|
+
['gainsboro', [220, 220, 220]],
|
|
2305
|
+
['ghostwhite', [248, 248, 255]],
|
|
2306
|
+
['gold', [255, 215, 0]],
|
|
2307
|
+
['goldenrod', [218, 165, 32]],
|
|
2308
|
+
['gray', [128, 128, 128]],
|
|
2309
|
+
['grey', [128, 128, 128]],
|
|
2310
|
+
['green', [0, 128, 0]],
|
|
2311
|
+
['greenyellow', [173, 255, 47]],
|
|
2312
|
+
['honeydew', [240, 255, 240]],
|
|
2313
|
+
['hotpink', [255, 105, 180]],
|
|
2314
|
+
['indianred', [205, 92, 92]],
|
|
2315
|
+
['indigo', [75, 0, 130]],
|
|
2316
|
+
['ivory', [255, 255, 240]],
|
|
2317
|
+
['khaki', [240, 230, 140]],
|
|
2318
|
+
['lavender', [230, 230, 250]],
|
|
2319
|
+
['lavenderblush', [255, 240, 245]],
|
|
2320
|
+
['lawngreen', [124, 252, 0]],
|
|
2321
|
+
['lemonchiffon', [255, 250, 205]],
|
|
2322
|
+
['lightblue', [173, 216, 230]],
|
|
2323
|
+
['lightcoral', [240, 128, 128]],
|
|
2324
|
+
['lightcyan', [224, 255, 255]],
|
|
2325
|
+
['lightgoldenrodyellow', [250, 250, 210]],
|
|
2326
|
+
['lightgreen', [144, 238, 144]],
|
|
2327
|
+
['lightgray', [211, 211, 211]],
|
|
2328
|
+
['lightgrey', [211, 211, 211]],
|
|
2329
|
+
['lightpink', [255, 182, 193]],
|
|
2330
|
+
['lightsalmon', [255, 160, 122]],
|
|
2331
|
+
['lightseagreen', [32, 178, 170]],
|
|
2332
|
+
['lightskyblue', [135, 206, 250]],
|
|
2333
|
+
['lightslategray', [119, 136, 153]],
|
|
2334
|
+
['lightslategrey', [119, 136, 153]],
|
|
2335
|
+
['lightsteelblue', [176, 196, 222]],
|
|
2336
|
+
['lightyellow', [255, 255, 224]],
|
|
2337
|
+
['lime', [0, 255, 0]],
|
|
2338
|
+
['limegreen', [50, 205, 50]],
|
|
2339
|
+
['linen', [250, 240, 230]],
|
|
2340
|
+
['magenta', [255, 0, 255]],
|
|
2341
|
+
['maroon', [128, 0, 0]],
|
|
2342
|
+
['mediumaquamarine', [102, 205, 170]],
|
|
2343
|
+
['mediumblue', [0, 0, 205]],
|
|
2344
|
+
['mediumorchid', [186, 85, 211]],
|
|
2345
|
+
['mediumpurple', [147, 112, 219]],
|
|
2346
|
+
['mediumseagreen', [60, 179, 113]],
|
|
2347
|
+
['mediumslateblue', [123, 104, 238]],
|
|
2348
|
+
['mediumspringgreen', [0, 250, 154]],
|
|
2349
|
+
['mediumturquoise', [72, 209, 204]],
|
|
2350
|
+
['mediumvioletred', [199, 21, 133]],
|
|
2351
|
+
['midnightblue', [25, 25, 112]],
|
|
2352
|
+
['mintcream', [245, 255, 250]],
|
|
2353
|
+
['mistyrose', [255, 228, 225]],
|
|
2354
|
+
['moccasin', [255, 228, 181]],
|
|
2355
|
+
['navajowhite', [255, 222, 173]],
|
|
2356
|
+
['navy', [0, 0, 128]],
|
|
2357
|
+
['oldlace', [253, 245, 230]],
|
|
2358
|
+
['olive', [128, 128, 0]],
|
|
2359
|
+
['olivedrab', [107, 142, 35]],
|
|
2360
|
+
['orange', [255, 165, 0]],
|
|
2361
|
+
['orangered', [255, 69, 0]],
|
|
2362
|
+
['orchid', [218, 112, 214]],
|
|
2363
|
+
['palegoldenrod', [238, 232, 170]],
|
|
2364
|
+
['palegreen', [152, 251, 152]],
|
|
2365
|
+
['paleturquoise', [175, 238, 238]],
|
|
2366
|
+
['palevioletred', [219, 112, 147]],
|
|
2367
|
+
['papayawhip', [255, 239, 213]],
|
|
2368
|
+
['peachpuff', [255, 218, 185]],
|
|
2369
|
+
['peru', [205, 133, 63]],
|
|
2370
|
+
['pink', [255, 192, 203]],
|
|
2371
|
+
['plum', [221, 160, 221]],
|
|
2372
|
+
['powderblue', [176, 224, 230]],
|
|
2373
|
+
['purple', [128, 0, 128]],
|
|
2374
|
+
['rebeccapurple', [102, 51, 153]],
|
|
2375
|
+
['red', [255, 0, 0]],
|
|
2376
|
+
['rosybrown', [188, 143, 143]],
|
|
2377
|
+
['royalblue', [65, 105, 225]],
|
|
2378
|
+
['saddlebrown', [139, 69, 19]],
|
|
2379
|
+
['salmon', [250, 128, 114]],
|
|
2380
|
+
['sandybrown', [244, 164, 96]],
|
|
2381
|
+
['seagreen', [46, 139, 87]],
|
|
2382
|
+
['seashell', [255, 245, 238]],
|
|
2383
|
+
['sienna', [160, 82, 45]],
|
|
2384
|
+
['silver', [192, 192, 192]],
|
|
2385
|
+
['skyblue', [135, 206, 235]],
|
|
2386
|
+
['slateblue', [106, 90, 205]],
|
|
2387
|
+
['slategray', [112, 128, 144]],
|
|
2388
|
+
['slategrey', [112, 128, 144]],
|
|
2389
|
+
['snow', [255, 250, 250]],
|
|
2390
|
+
['springgreen', [0, 255, 127]],
|
|
2391
|
+
['steelblue', [70, 130, 180]],
|
|
2392
|
+
['tan', [210, 180, 140]],
|
|
2393
|
+
['teal', [0, 128, 128]],
|
|
2394
|
+
['thistle', [216, 191, 216]],
|
|
2395
|
+
['tomato', [255, 99, 71]],
|
|
2396
|
+
['turquoise', [64, 224, 208]],
|
|
2397
|
+
['violet', [238, 130, 238]],
|
|
2398
|
+
['wheat', [245, 222, 179]],
|
|
2399
|
+
['white', [255, 255, 255]],
|
|
2400
|
+
['whitesmoke', [245, 245, 245]],
|
|
2401
|
+
['yellow', [255, 255, 0]],
|
|
2402
|
+
['yellowgreen', [154, 205, 50]],
|
|
2403
|
+
['transparent', [0, 0, 0, 0]],
|
|
2404
|
+
];
|
|
2405
|
+
|
|
2406
|
+
console.assert(
|
|
2407
|
+
COLOR_TO_RGBA_ENTRIES.every(([nickname]) => nickname.toLowerCase() === nickname),
|
|
2408
|
+
'All color nicknames must be lowercase.');
|
|
2409
|
+
|
|
2410
|
+
export const Nicknames = new Map(COLOR_TO_RGBA_ENTRIES);
|
|
2411
|
+
|
|
2412
|
+
const RGBAToNickname = new Map(
|
|
2413
|
+
// Default opacity to 1 if the color only specified 3 channels
|
|
2414
|
+
COLOR_TO_RGBA_ENTRIES.map(([nickname, [r, g, b, a = 1]]) => {
|
|
2415
|
+
return [String([r, g, b, a]), nickname];
|
|
2416
|
+
}),
|
|
2417
|
+
);
|
|
2418
|
+
|
|
2419
|
+
const LAYOUT_LINES_HIGHLIGHT_COLOR = [127, 32, 210];
|
|
2420
|
+
|
|
2421
|
+
export const PageHighlight = {
|
|
2422
|
+
Content: Legacy.fromRGBA([111, 168, 220, .66]),
|
|
2423
|
+
ContentLight: Legacy.fromRGBA([111, 168, 220, .5]),
|
|
2424
|
+
ContentOutline: Legacy.fromRGBA([9, 83, 148]),
|
|
2425
|
+
Padding: Legacy.fromRGBA([147, 196, 125, .55]),
|
|
2426
|
+
PaddingLight: Legacy.fromRGBA([147, 196, 125, .4]),
|
|
2427
|
+
Border: Legacy.fromRGBA([255, 229, 153, .66]),
|
|
2428
|
+
BorderLight: Legacy.fromRGBA([255, 229, 153, .5]),
|
|
2429
|
+
Margin: Legacy.fromRGBA([246, 178, 107, .66]),
|
|
2430
|
+
MarginLight: Legacy.fromRGBA([246, 178, 107, .5]),
|
|
2431
|
+
EventTarget: Legacy.fromRGBA([255, 196, 196, .66]),
|
|
2432
|
+
Shape: Legacy.fromRGBA([96, 82, 177, 0.8]),
|
|
2433
|
+
ShapeMargin: Legacy.fromRGBA([96, 82, 127, .6]),
|
|
2434
|
+
CssGrid: Legacy.fromRGBA([0x4b, 0, 0x82, 1]),
|
|
2435
|
+
LayoutLine: Legacy.fromRGBA([...LAYOUT_LINES_HIGHLIGHT_COLOR, 1]),
|
|
2436
|
+
GridBorder: Legacy.fromRGBA([...LAYOUT_LINES_HIGHLIGHT_COLOR, 1]),
|
|
2437
|
+
GapBackground: Legacy.fromRGBA([...LAYOUT_LINES_HIGHLIGHT_COLOR, .3]),
|
|
2438
|
+
GapHatch: Legacy.fromRGBA([...LAYOUT_LINES_HIGHLIGHT_COLOR, .8]),
|
|
2439
|
+
GridAreaBorder: Legacy.fromRGBA([26, 115, 232, 1]),
|
|
2440
|
+
};
|
|
2441
|
+
|
|
2442
|
+
export const SourceOrderHighlight = {
|
|
2443
|
+
ParentOutline: Legacy.fromRGBA([224, 90, 183, 1]),
|
|
2444
|
+
ChildOutline: Legacy.fromRGBA([0, 120, 212, 1]),
|
|
2445
|
+
};
|
|
2446
|
+
|
|
2447
|
+
export const IsolationModeHighlight = {
|
|
2448
|
+
Resizer: Legacy.fromRGBA([222, 225, 230, 1]), // --color-background-elevation-2
|
|
2449
|
+
ResizerHandle: Legacy.fromRGBA([166, 166, 166, 1]),
|
|
2450
|
+
Mask: Legacy.fromRGBA([248, 249, 249, 1]),
|
|
2451
|
+
};
|
|
2452
|
+
|
|
2453
|
+
type Space = number|{
|
|
2454
|
+
min: number,
|
|
2455
|
+
max: number,
|
|
2456
|
+
count: (number|undefined),
|
|
2457
|
+
};
|
|
2458
|
+
|
|
2459
|
+
export class Generator {
|
|
2460
|
+
readonly #hueSpace: Space;
|
|
2461
|
+
readonly #satSpace: Space;
|
|
2462
|
+
readonly #lightnessSpace: Space;
|
|
2463
|
+
readonly #alphaSpace: Space;
|
|
2464
|
+
readonly #colors = new Map<string, string>();
|
|
2465
|
+
constructor(hueSpace?: Space, satSpace?: Space, lightnessSpace?: Space, alphaSpace?: Space) {
|
|
2466
|
+
this.#hueSpace = hueSpace || {min: 0, max: 360, count: undefined};
|
|
2467
|
+
this.#satSpace = satSpace || 67;
|
|
2468
|
+
this.#lightnessSpace = lightnessSpace || 80;
|
|
2469
|
+
this.#alphaSpace = alphaSpace || 1;
|
|
2470
|
+
}
|
|
2471
|
+
|
|
2472
|
+
setColorForID(id: string, color: string): void {
|
|
2473
|
+
this.#colors.set(id, color);
|
|
2474
|
+
}
|
|
2475
|
+
|
|
2476
|
+
colorForID(id: string): string {
|
|
2477
|
+
let color = this.#colors.get(id);
|
|
2478
|
+
if (!color) {
|
|
2479
|
+
color = this.generateColorForID(id);
|
|
2480
|
+
this.#colors.set(id, color);
|
|
2481
|
+
}
|
|
2482
|
+
return color;
|
|
2483
|
+
}
|
|
2484
|
+
|
|
2485
|
+
private generateColorForID(id: string): string {
|
|
2486
|
+
const hash = Platform.StringUtilities.hashCode(id);
|
|
2487
|
+
const h = this.indexToValueInSpace(hash, this.#hueSpace);
|
|
2488
|
+
const s = this.indexToValueInSpace(hash >> 8, this.#satSpace);
|
|
2489
|
+
const l = this.indexToValueInSpace(hash >> 16, this.#lightnessSpace);
|
|
2490
|
+
const a = this.indexToValueInSpace(hash >> 24, this.#alphaSpace);
|
|
2491
|
+
const start = `hsl(${h}deg ${s}% ${l}%`;
|
|
2492
|
+
if (a !== 1) {
|
|
2493
|
+
return `${start} / ${Math.floor(a * 100)}%)`;
|
|
2494
|
+
}
|
|
2495
|
+
return `${start})`;
|
|
2496
|
+
}
|
|
2497
|
+
|
|
2498
|
+
private indexToValueInSpace(index: number, space: Space): number {
|
|
2499
|
+
if (typeof space === 'number') {
|
|
2500
|
+
return space;
|
|
2501
|
+
}
|
|
2502
|
+
const count = space.count || space.max - space.min;
|
|
2503
|
+
index %= count;
|
|
2504
|
+
return space.min + Math.floor(index / (count - 1) * (space.max - space.min));
|
|
2505
|
+
}
|
|
2506
|
+
}
|