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 #include "core/common/color_inverter.h"
17
18 #include "core/components/common/properties/color.h"
19 #include "core/components_v2/inspector/inspector_constants.h"
20
21 namespace OHOS::Ace {
22 ColorInverter::ColorInverter() = default;
23 ColorInverter::~ColorInverter() = default;
24
Create()25 RefPtr<ColorInvertFuncManager> ColorInvertFuncManager::Create()
26 {
27 return AceType::MakeRefPtr<ColorInvertFuncManager>();
28 }
29
SetInvertFunc(const std::string & nodeTag,ColorInvertFunc && func)30 void ColorInvertFuncManager::SetInvertFunc(const std::string& nodeTag, ColorInvertFunc&& func)
31 {
32 colorInvertFuncMap_[nodeTag] = std::move(func);
33 }
34
DeleteInvertFunc(const std::string & nodeTag)35 void ColorInvertFuncManager::DeleteInvertFunc(const std::string& nodeTag)
36 {
37 colorInvertFuncMap_.erase(nodeTag);
38 }
39
GetInvertFunc(const std::string & nodeTag) const40 ColorInvertFunc ColorInvertFuncManager::GetInvertFunc(const std::string& nodeTag) const
41 {
42 auto iter = colorInvertFuncMap_.find(nodeTag);
43 if (iter == colorInvertFuncMap_.end()) {
44 iter = colorInvertFuncMap_.find(V2::UNDEFINED_NODE_ETS_TAG);
45 }
46 CHECK_NULL_RETURN(iter != colorInvertFuncMap_.end(), nullptr);
47 return iter->second;
48 }
49
GetOrCreateManager(int32_t instanceId)50 RefPtr<ColorInvertFuncManager> ColorInverter::GetOrCreateManager(int32_t instanceId)
51 {
52 auto iter = colorInvertFuncManagerMap_.find(instanceId);
53 if (iter == colorInvertFuncManagerMap_.end()) {
54 auto manager = ColorInvertFuncManager::Create();
55 colorInvertFuncManagerMap_.emplace(instanceId, manager);
56 return manager;
57 }
58 return iter->second;
59 }
60
EnableColorInvert(int32_t instanceId,const std::string & nodeTag,ColorInvertFunc && func)61 void ColorInverter::EnableColorInvert(int32_t instanceId, const std::string& nodeTag, ColorInvertFunc&& func)
62 {
63 std::unique_lock<std::shared_mutex> lock(mutex_);
64 auto manager = GetOrCreateManager(instanceId);
65 if (manager) {
66 manager->SetInvertFunc(nodeTag, std::move(func));
67 }
68 }
69
DisableColorInvert(int32_t instanceId,const std::string & nodeTag)70 void ColorInverter::DisableColorInvert(int32_t instanceId, const std::string& nodeTag)
71 {
72 std::unique_lock<std::shared_mutex> lock(mutex_);
73 if (nodeTag == V2::UNDEFINED_NODE_ETS_TAG) {
74 colorInvertFuncManagerMap_.erase(instanceId);
75 } else {
76 auto manager = GetManager(instanceId);
77 CHECK_NULL_VOID(manager);
78 manager->DeleteInvertFunc(nodeTag);
79 }
80 }
81
GetInvertFunc(int32_t instanceId,const std::string & nodeTag)82 ColorInvertFunc ColorInverter::GetInvertFunc(int32_t instanceId, const std::string& nodeTag)
83 {
84 std::shared_lock<std::shared_mutex> lock(mutex_);
85 auto manager = GetManager(instanceId);
86 if (!manager) {
87 manager = GetManager(PROCESS_LEVEL_ID);
88 }
89 CHECK_NULL_RETURN(manager, nullptr);
90 return manager->GetInvertFunc(nodeTag);
91 }
92
GetManager(int32_t instanceId) const93 RefPtr<ColorInvertFuncManager> ColorInverter::GetManager(int32_t instanceId) const
94 {
95 auto iter = colorInvertFuncManagerMap_.find(instanceId);
96 if (iter == colorInvertFuncManagerMap_.end()) {
97 return nullptr;
98 }
99 return iter->second;
100 }
101
DefaultInverter(uint32_t color)102 uint32_t ColorInverter::DefaultInverter(uint32_t color)
103 {
104 Color curColor = Color(color);
105 uint8_t full = 255;
106 auto curAlpha = curColor.GetAlpha();
107 Color invertColor = Color::FromARGB(curAlpha,
108 full - curColor.GetRed(), full - curColor.GetGreen(), full - curColor.GetBlue());
109 return invertColor.GetValue();
110 }
111
Invert(Color color,int32_t instanceId,const std::string & nodeTag)112 Color ColorInverter::Invert(Color color, int32_t instanceId, const std::string& nodeTag)
113 {
114 ColorInvertFunc func = ColorInverter::GetInstance().GetInvertFunc(instanceId, nodeTag);
115 return func ? Color(func(color.GetValue())) : color;
116 }
117
118 } // namespace OHOS::Ace
119