2020-10-18 12:31:45 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
|
|
|
|
2022-07-10 09:47:27 +08:00
|
|
|
https://adxeproject.github.io/
|
2020-10-18 12:31:45 +08:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "CCControlUtils.h"
|
|
|
|
|
|
|
|
NS_CC_EXT_BEGIN
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Sprite* ControlUtils::addSpriteToTargetWithPosAndAnchor(const char* spriteName, Node* target, Vec2 pos, Vec2 anchor)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Sprite* sprite = Sprite::createWithSpriteFrameName(spriteName);
|
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
if (!sprite)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
sprite->setPosition(pos);
|
|
|
|
sprite->setAnchorPoint(anchor);
|
|
|
|
target->addChild(sprite);
|
|
|
|
|
|
|
|
return sprite;
|
|
|
|
}
|
|
|
|
|
|
|
|
HSV ControlUtils::HSVfromRGB(RGBA value)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
HSV out;
|
|
|
|
double min, max, delta;
|
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
min = value.r < value.g ? value.r : value.g;
|
2021-12-25 10:04:45 +08:00
|
|
|
min = min < value.b ? min : value.b;
|
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
max = value.r > value.g ? value.r : value.g;
|
2021-12-25 10:04:45 +08:00
|
|
|
max = max > value.b ? max : value.b;
|
|
|
|
|
|
|
|
out.v = max; // v
|
2020-10-18 12:31:45 +08:00
|
|
|
delta = max - min;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (max > 0.0)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
out.s = (delta / max); // s
|
|
|
|
}
|
|
|
|
else
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
// r = g = b = 0 // s = 0, v is undefined
|
|
|
|
out.s = 0.0;
|
2021-12-25 10:04:45 +08:00
|
|
|
out.h = -1; // its now undefined (don't know if setting to NAN is a good idea)
|
2020-10-18 12:31:45 +08:00
|
|
|
return out;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (value.r >= max) // > is bogus, just keeps compilor happy
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
out.h = (value.g - value.b) / delta; // between yellow & magenta
|
|
|
|
}
|
|
|
|
else
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (value.g >= max)
|
|
|
|
out.h = 2.0 + (value.b - value.r) / delta; // between cyan & yellow
|
2020-10-18 12:31:45 +08:00
|
|
|
else
|
2021-12-25 10:04:45 +08:00
|
|
|
out.h = 4.0 + (value.r - value.g) / delta; // between magenta & cyan
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
out.h *= 60.0; // degrees
|
|
|
|
|
|
|
|
if (out.h < 0.0)
|
2020-10-18 12:31:45 +08:00
|
|
|
out.h += 360.0;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
RGBA ControlUtils::RGBfromHSV(HSV value)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
double hh, p, q, t, ff;
|
|
|
|
long i;
|
|
|
|
RGBA out;
|
|
|
|
out.a = 1;
|
|
|
|
|
|
|
|
if (value.s <= 0.0) // < is bogus, just shuts up warnings
|
|
|
|
{
|
|
|
|
if (std::isnan(value.h)) // value.h == NAN
|
|
|
|
{
|
2020-10-18 12:31:45 +08:00
|
|
|
out.r = value.v;
|
|
|
|
out.g = value.v;
|
|
|
|
out.b = value.v;
|
|
|
|
return out;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
// error - should never happen
|
|
|
|
out.r = 0.0;
|
|
|
|
out.g = 0.0;
|
|
|
|
out.b = 0.0;
|
|
|
|
return out;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
hh = value.h;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (hh >= 360.0)
|
|
|
|
hh = 0.0;
|
2020-10-18 12:31:45 +08:00
|
|
|
hh /= 60.0;
|
2021-12-25 10:04:45 +08:00
|
|
|
i = (long)hh;
|
2020-10-18 12:31:45 +08:00
|
|
|
ff = hh - i;
|
2021-12-25 10:04:45 +08:00
|
|
|
p = value.v * (1.0 - value.s);
|
|
|
|
q = value.v * (1.0 - (value.s * ff));
|
|
|
|
t = value.v * (1.0 - (value.s * (1.0 - ff)));
|
|
|
|
|
|
|
|
switch (i)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
case 0:
|
|
|
|
out.r = value.v;
|
|
|
|
out.g = t;
|
|
|
|
out.b = p;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
out.r = q;
|
|
|
|
out.g = value.v;
|
|
|
|
out.b = p;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
out.r = p;
|
|
|
|
out.g = value.v;
|
|
|
|
out.b = t;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
out.r = p;
|
|
|
|
out.g = q;
|
|
|
|
out.b = value.v;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
out.r = t;
|
|
|
|
out.g = p;
|
|
|
|
out.b = value.v;
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
default:
|
|
|
|
out.r = value.v;
|
|
|
|
out.g = p;
|
|
|
|
out.b = q;
|
|
|
|
break;
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
return out;
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Rect ControlUtils::RectUnion(const Rect& src1, const Rect& src2)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
Rect result;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
float x1 = MIN(src1.getMinX(), src2.getMinX());
|
|
|
|
float y1 = MIN(src1.getMinY(), src2.getMinY());
|
|
|
|
float x2 = MAX(src1.getMaxX(), src2.getMaxX());
|
|
|
|
float y2 = MAX(src1.getMaxY(), src2.getMaxY());
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
result.origin = Vec2(x1, y1);
|
|
|
|
result.size = Size(x2 - x1, y2 - y1);
|
2020-10-18 12:31:45 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_EXT_END
|