1 /* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_COLOR_INVERTER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_COLOR_INVERTER_H 18 #include <functional> 19 #include <mutex> 20 #include <shared_mutex> 21 #include <unordered_map> 22 23 #include "base/memory/ace_type.h" 24 #include "base/memory/referenced.h" 25 #include "base/utils/singleton.h" 26 #include "base/utils/utils.h" 27 #include "core/common/container_consts.h" 28 #include "core/components/common/properties/color.h" 29 30 namespace OHOS::Ace { 31 namespace { 32 constexpr int32_t PROCESS_LEVEL_ID = INSTANCE_ID_UNDEFINED; 33 } // namespace 34 35 typedef std::function<uint32_t(uint32_t)> ColorInvertFunc; 36 37 class ColorInvertFuncManager final : public AceType { 38 public: 39 ColorInvertFuncManager() = default; 40 ~ColorInvertFuncManager() = default; 41 42 static RefPtr<ColorInvertFuncManager> Create(); 43 44 void SetInvertFunc(const std::string& nodeTag, ColorInvertFunc&& func); 45 46 void DeleteInvertFunc(const std::string& nodeTag); 47 48 ColorInvertFunc GetInvertFunc(const std::string& nodeTag) const; 49 50 private: 51 std::unordered_map<std::string, ColorInvertFunc> colorInvertFuncMap_; 52 }; 53 54 class ColorInverter : public Singleton<ColorInverter> { 55 DECLARE_SINGLETON(ColorInverter); 56 57 public: 58 static uint32_t DefaultInverter(uint32_t color); 59 60 void EnableColorInvert(int32_t instanceId, const std::string& nodeTag, ColorInvertFunc&& func); 61 void DisableColorInvert(int32_t instanceId, const std::string& nodeTag); 62 ColorInvertFunc GetInvertFunc(int32_t instanceId, const std::string& nodeTag); 63 static Color Invert(Color color, int32_t instanceId, const std::string& nodeTag); 64 65 private: 66 std::shared_mutex mutex_; 67 RefPtr<ColorInvertFuncManager> GetOrCreateManager(int32_t instanceId); // for setter 68 RefPtr<ColorInvertFuncManager> GetManager(int32_t instanceId) const; // for user 69 std::unordered_map<int32_t, RefPtr<ColorInvertFuncManager>> colorInvertFuncManagerMap_; 70 }; 71 72 } // namespace OHOS::Ace 73 74 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_COLOR_INVERTER_H 75