auto color detection for credits

This commit is contained in:
Yoshman29
2023-02-19 15:21:54 +01:00
parent 19fbdeb8de
commit 8db770bc28
7 changed files with 130 additions and 13 deletions
+1 -1
View File
@@ -1 +1 @@
2265
2286
+16 -8
View File
@@ -1,5 +1,6 @@
package funkin.menus.credits;
import flixel.util.FlxColor;
import funkin.shaders.CustomShader;
import flixel.math.FlxMath;
import funkin.options.type.TextOption;
@@ -13,21 +14,18 @@ import funkin.github.GitHub;
import funkin.github.GitHubContributor;
import funkin.menus.MainMenuState;
using funkin.utils.BitmapUtil;
class CreditsCodename extends OptionsScreen {
public var contributorsSprites:Array<FlxSprite> = [];
public var contributorsAvatars:Array<FlxGraphic> = [];
public var contributorsColors:Array<FlxColor> = [];
public var avatarLoadListId:Int = 0;
public var contributors:Array<GitHubContributor>;
public override function create() {
public var interpColor:FlxInterpolateColor;
var bg:FlxSprite = new FlxSprite(-80).loadAnimatedGraphic(Paths.image('menus/menuBGBlue'));
bg.scrollFactor.set();
bg.scale.set(1.15, 1.15);
bg.updateHitbox();
bg.screenCenter();
bg.antialiasing = true;
add(bg);
public override function create() {
contributors = GitHub.getContributors("YoshiCrafter29", "CodenameEngine", function(e) {
trace(e);
@@ -48,6 +46,7 @@ class CreditsCodename extends OptionsScreen {
super.create();
interpColor = new FlxInterpolateColor(bg.color);
}
public override function createAdditional() {
@@ -66,7 +65,9 @@ class CreditsCodename extends OptionsScreen {
for(k=>c in contributors) {
var bytes = GitHub.__requestBytesOnGitHubServers('${c.avatar_url}&size=96');
var bmap = BitmapData.fromBytes(bytes);
var color = bmap.getMostPresentSaturatedColor();
contributorsAvatars.push(FlxG.bitmap.add(bmap, false, 'GITHUB-USER:${c.login}'));
contributorsColors.push(color);
if (destroyed) return;
}
});
@@ -88,9 +89,16 @@ class CreditsCodename extends OptionsScreen {
var alpha = options[k];
spr.x = alpha.x - 10;
}
interpColor.fpsLerpTo(contributorsColors[curSelected], 0.0625);
bg.color = interpColor.color;
}
public override function destroy() {
super.destroy();
}
public override function exit() {
FlxG.switchState(new CreditsMain());
}
}
+1 -1
View File
@@ -86,7 +86,7 @@ class Options
public static function load() {
if (__save == null) __save = new FlxSave();
__save.bind("options");
__save.bind("options", "CodenameEngine");
for(field in Reflect.fields(__save.data)) {
var obj = Reflect.field(__save.data, field);
Reflect.setProperty(Options, field, obj);
+2 -2
View File
@@ -19,7 +19,6 @@ class OptionsScreen extends MusicBeatState {
public static var instance:OptionsScreen;
public static var optionHeight:Float = 120;
// public var bgColor:FlxColor = 0xFFFDE871;
public var bg:FlxSprite;
public var descDrop:FlxBackdrop;
@@ -43,11 +42,12 @@ class OptionsScreen extends MusicBeatState {
FlxTransitionableState.skipNextTransIn = true;
super.create();
bg = new FlxSprite(-80).loadAnimatedGraphic(Paths.image('menus/menuBG'));
bg = new FlxSprite(-80).loadAnimatedGraphic(Paths.image('menus/menuDesat'));
bg.scrollFactor.set();
bg.scale.set(1.15, 1.15);
bg.updateHitbox();
bg.screenCenter();
bg.color = defaultBGColor;
bg.antialiasing = true;
add(bg);
+1 -1
View File
@@ -195,7 +195,7 @@ class Main extends Sprite
Assets.registerLibrary('default', lib);
funkin.options.PlayerSettings.init();
FlxG.save.bind('Codename-Engine');
FlxG.save.bind('save', 'CodenameEngine');
Options.load();
Highscore.load();
+69
View File
@@ -0,0 +1,69 @@
package funkin.utils;
import openfl.display.BitmapData;
import flixel.util.FlxColor;
class BitmapUtil {
/**
* Returns the most present color in a Bitmap.
* @param bmap Bitmap
* @return FlxColor Color that is the most present.
*/
public static function getMostPresentColor(bmap:BitmapData):FlxColor {
// map containing all the colors and the number of times they've been assigned.
var colorMap:Map<FlxColor, Float> = [];
var color:FlxColor = 0;
var fixedColor:FlxColor = 0;
for(y in 0...bmap.height) {
for(x in 0...bmap.width) {
color = bmap.getPixel32(x, y);
fixedColor = 0xFF000000 + (color % 0x1000000);
if (colorMap[fixedColor] == null)
colorMap[fixedColor] = 0;
colorMap[fixedColor] += color.alphaFloat;
}
}
var mostPresentColor:FlxColor = 0;
var mostPresentColorCount:Float = -1;
for(c=>n in colorMap) {
if (n > mostPresentColorCount) {
mostPresentColorCount = n;
mostPresentColor = c;
}
}
return mostPresentColor;
}
/**
* Returns the most present saturated color in a Bitmap.
* @param bmap Bitmap
* @return FlxColor Color that is the most present.
*/
public static function getMostPresentSaturatedColor(bmap:BitmapData):FlxColor {
// map containing all the colors and the number of times they've been assigned.
var colorMap:Map<FlxColor, Float> = [];
var color:FlxColor = 0;
var fixedColor:FlxColor = 0;
for(y in 0...bmap.height) {
for(x in 0...bmap.width) {
color = bmap.getPixel32(x, y);
fixedColor = 0xFF000000 + (color % 0x1000000);
if (colorMap[fixedColor] == null)
colorMap[fixedColor] = 0;
colorMap[fixedColor] += color.alphaFloat * 0.33 + (0.67 * (color.saturation * (2 * (color.lightness > 0.5 ? 0.5 - (color.lightness) : color.lightness))));
}
}
var mostPresentColor:FlxColor = 0;
var mostPresentColorCount:Float = -1;
for(c=>n in colorMap) {
if (n > mostPresentColorCount) {
mostPresentColorCount = n;
mostPresentColor = c;
}
}
return mostPresentColor;
}
}
@@ -0,0 +1,40 @@
package funkin.utils;
import flixel.FlxG;
import flixel.util.FlxColor;
class FlxInterpolateColor {
public var red:Float = 0;
public var green:Float = 0;
public var blue:Float = 0;
public var alpha:Float = 0;
public var color(get, set):FlxColor;
private inline function get_color()
return FlxColor.fromRGBFloat(red, green, blue, alpha);
private inline function set_color(c:FlxColor):FlxColor {
red = c.redFloat;
green = c.greenFloat;
blue = c.blueFloat;
alpha = c.alphaFloat;
return c;
}
public inline function toString()
return '[FlxInterpolateColor - (R:${red} | G:${green} | B:${blue} | A:${alpha})]';
public function new(color:FlxColor) {
this.color = color;
}
public function lerpTo(color:FlxColor, ratio:Float) {
red = (color.redFloat - red) * ratio + red;
green = (color.greenFloat - green) * ratio + green;
blue = (color.blueFloat - blue) * ratio + blue;
alpha = (color.alphaFloat - alpha) * ratio + alpha;
}
public inline function fpsLerpTo(color:FlxColor, ratio:Float)
lerpTo(color, ratio * 60 * FlxG.elapsed);
}