UNPKG

kinetic-slider

Version:

A WebGL-powered kinetic slider component using PIXI.js

1 lines 7.48 kB
{"version":3,"file":"adjustmentFilter.cjs","sources":["../../../src/filters/adjustmentFilter.ts"],"sourcesContent":["import { AdjustmentFilter } from 'pixi-filters';\nimport { type AdjustmentFilterConfig, type FilterResult } from './types';\nimport { ShaderResourceManager } from '../managers/ShaderResourceManager';\n\n/**\n * Creates an Adjustment filter that allows controlling gamma, contrast, saturation, brightness,\n * alpha and color-channel shifts without using a matrix\n *\n * This filter is faster and simpler than ColorMatrixFilter as it doesn't use a matrix.\n * It provides direct control over common image adjustments.\n * Uses shader pooling for better performance.\n *\n * @param config - Configuration for the Adjustment filter\n * @returns FilterResult with the filter instance and control functions\n */\nexport function createAdjustmentFilter(config: AdjustmentFilterConfig): FilterResult {\n // Get shader manager instance\n const shaderManager = ShaderResourceManager.getInstance();\n\n // Create the filter with options\n const options: any = {\n gamma: config.gamma ?? 1,\n contrast: config.contrast ?? 1,\n saturation: config.saturation ?? 1,\n brightness: config.brightness ?? 1,\n red: config.red ?? 1,\n green: config.green ?? 1,\n blue: config.blue ?? 1,\n alpha: config.alpha ?? 1\n };\n\n // Create a unique key for this filter configuration\n const shaderKey = `adjustment-filter-${config.primaryProperty || 'default'}`;\n\n // Create the filter with options\n const filter = new AdjustmentFilter(options);\n\n // Register filter with shader manager\n try {\n shaderManager.registerFilter(filter, shaderKey);\n } catch (error) {\n console.warn('Error registering adjustment filter with shader manager:', error);\n }\n\n /**\n * Update the filter's intensity based on the configuration\n * For AdjustmentFilter, intensity modifies the primary property specified in config\n *\n * @param intensity - New intensity value (0-10 scale)\n */\n const updateIntensity = (intensity: number): void => {\n // Normalize intensity to a 0-10 scale\n const normalizedIntensity = Math.max(0, Math.min(10, intensity));\n\n // Determine which property to adjust based on config\n // If primaryProperty is specified, use that, otherwise use a default behavior\n if (config.primaryProperty) {\n // Map intensity (0-10) to appropriate range for the specified property\n // Most properties work well around 0-2 range, with 1 being neutral\n let value: number;\n\n switch (config.primaryProperty) {\n case 'contrast':\n case 'saturation':\n // For contrast and saturation, 0 is no effect (grayscale for saturation),\n // 1 is normal, and values > 1 increase the effect\n value = normalizedIntensity / 5; // 0-10 -> 0-2\n break;\n case 'brightness':\n // Brightness: 0 is black, 1 is normal, >1 is brighter\n value = normalizedIntensity / 5; // 0-10 -> 0-2\n break;\n case 'gamma':\n // Gamma: 0-1 darkens, 1 is normal, >1 lightens\n value = normalizedIntensity / 5; // 0-10 -> 0-2\n break;\n case 'red':\n case 'green':\n case 'blue':\n // Color channels: 0 removes the channel, 1 is normal, >1 intensifies\n value = normalizedIntensity / 5; // 0-10 -> 0-2\n break;\n case 'alpha':\n // Alpha: 0 is transparent, 1 is fully opaque\n value = normalizedIntensity / 10; // 0-10 -> 0-1\n break;\n default:\n // Default fallback\n value = normalizedIntensity / 5; // 0-10 -> 0-2\n }\n\n // Apply the value to the specified property\n (filter as any)[config.primaryProperty] = value;\n } else {\n // Default behavior if no primary property is specified:\n // Increase contrast as intensity increases (common use case)\n filter.contrast = normalizedIntensity / 5; // 0-10 -> 0-2\n }\n };\n\n // Set initial intensity\n updateIntensity(config.intensity);\n\n /**\n * Reset the filter to default state (neutral values)\n */\n const reset = (): void => {\n filter.gamma = 1;\n filter.contrast = 1;\n filter.saturation = 1;\n filter.brightness = 1;\n filter.red = 1;\n filter.green = 1;\n filter.blue = 1;\n filter.alpha = 1;\n };\n\n /**\n * Release any WebGL resources used by this filter\n */\n const dispose = (): void => {\n try {\n shaderManager.releaseFilter(filter, shaderKey);\n } catch (error) {\n console.warn('Error releasing adjustment filter shader:', error);\n }\n filter.destroy();\n };\n\n return { filter, updateIntensity, reset, dispose };\n}"],"names":["ShaderResourceManager","AdjustmentFilter"],"mappings":";;;;;AAeO,SAAS,uBAAuB,MAA8C,EAAA;AAEjF,EAAM,MAAA,aAAA,GAAgBA,4CAAsB,WAAY,EAAA;AAGxD,EAAA,MAAM,OAAe,GAAA;AAAA,IACjB,KAAA,EAAO,OAAO,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