Skip to content

Commit 5681a0e

Browse files
committed
add database and models for Colour extraction
1 parent 0a526dc commit 5681a0e

5 files changed

Lines changed: 193 additions & 4 deletions

File tree

app/Models/Colour.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/**
4+
* SPDX-License-Identifier: MIT
5+
* Copyright (c) 2017-2018 Tobias Reich
6+
* Copyright (c) 2018-2025 LycheeOrg.
7+
*/
8+
9+
namespace App\Models;
10+
11+
use Illuminate\Database\Eloquent\Model;
12+
13+
/**
14+
* App\Models\Colour.
15+
*
16+
* @property int $id
17+
* @property int $R
18+
* @property int $G
19+
* @property int $B
20+
*/
21+
class Colour extends Model
22+
{
23+
public $timestamps = false;
24+
25+
protected $fillable = [
26+
'id',
27+
'R',
28+
'G',
29+
'B',
30+
];
31+
32+
/**
33+
* Convert the colour to a hexadecimal string.
34+
*
35+
* @return string
36+
*/
37+
public function toHex(): string
38+
{
39+
return sprintf('#%02x%02x%02x', $this->R, $this->G, $this->B);
40+
}
41+
}

app/Models/Palette.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/**
4+
* SPDX-License-Identifier: MIT
5+
* Copyright (c) 2017-2018 Tobias Reich
6+
* Copyright (c) 2018-2025 LycheeOrg.
7+
*/
8+
9+
namespace App\Models;
10+
11+
use Illuminate\Database\Eloquent\Model;
12+
13+
/**
14+
* App\Models\Palette.
15+
*
16+
* @property int $id
17+
* @property string $photo_id
18+
* @property int $colour_1
19+
* @property int $colour_2
20+
* @property int $colour_3
21+
* @property int $colour_4
22+
* @property int $colour_5
23+
*/
24+
class Palette extends Model
25+
{
26+
public $timestamps = false;
27+
28+
protected $fillable = [
29+
'photo_id',
30+
'colour_1',
31+
'colour_2',
32+
'colour_3',
33+
'colour_4',
34+
'colour_5',
35+
];
36+
37+
/**
38+
* @return array{colour_1:string,colour_2:string,colour_3:string,colour_4:string,colour_5:string}
39+
*/
40+
public function toHexColours(): array
41+
{
42+
return [
43+
'colour_1' => self::toHex($this->colour_1),
44+
'colour_2' => self::toHex($this->colour_2),
45+
'colour_3' => self::toHex($this->colour_3),
46+
'colour_4' => self::toHex($this->colour_4),
47+
'colour_5' => self::toHex($this->colour_5),
48+
];
49+
}
50+
51+
public static function toHex(int $colour)
52+
{
53+
$b = $colour & 0xFF; // Extract the blue component
54+
$g = ($colour >> 8) & 0xFF; // Extract the green component
55+
$r = ($colour >> 16) & 0xFF; // Extract the red component
56+
57+
return sprintf('#%02x%02x%02x', $r, $g, $b);
58+
}
59+
}

app/Models/Photo.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
* @property User $owner
7979
* @property SizeVariants $size_variants
8080
* @property int $filesize
81+
* @property Palette|null $palette
8182
*
8283
* @method static PhotoBuilder|Photo addSelect($column)
8384
* @method static PhotoBuilder|Photo join(string $table, string $first, string $operator = null, string $second = null, string $type = 'inner', string $where = false)
@@ -175,10 +176,6 @@ class Photo extends Model implements HasUTCBasedTimes
175176
'live_photo_short_path', // serialize live_photo_url instead
176177
];
177178

178-
// protected $with = [
179-
// 'statistics',
180-
// ];
181-
182179
public function newEloquentBuilder($query): PhotoBuilder
183180
{
184181
return new PhotoBuilder($query);
@@ -219,6 +216,20 @@ public function statistics(): HasOne
219216
return $this->hasOne(Statistics::class, 'photo_id', 'id');
220217
}
221218

219+
/**
220+
* Returns the relationship between a photo and its associated color palette.
221+
*
222+
* This is a one-to-one relationship where each photo can have one palette
223+
* associated with it, which contains color information derived from the
224+
* photo.
225+
*
226+
* @return HasOne<Palette,$this>
227+
*/
228+
public function palette(): HasOne
229+
{
230+
return $this->hasOne(Palette::class, 'photo_id', 'id');
231+
}
232+
222233
/**
223234
* Accessor for attribute {@link Photo::$shutter}.
224235
*
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/**
4+
* SPDX-License-Identifier: MIT
5+
* Copyright (c) 2017-2018 Tobias Reich
6+
* Copyright (c) 2018-2025 LycheeOrg.
7+
*/
8+
9+
use Illuminate\Database\Migrations\Migration;
10+
use Illuminate\Database\Schema\Blueprint;
11+
use Illuminate\Support\Facades\Schema;
12+
13+
return new class() extends Migration {
14+
/**
15+
* Run the migrations.
16+
*/
17+
public function up(): void
18+
{
19+
Schema::create('colours', function (Blueprint $table) {
20+
$table->unsignedMediumInteger('id')->primary();
21+
$table->unsignedTinyInteger('R')->default(0);
22+
$table->unsignedTinyInteger('G')->default(0);
23+
$table->unsignedTinyInteger('B')->default(0);
24+
$table->unique(['R', 'G', 'B']); // Ensure that the combination of R, G, and B is unique
25+
});
26+
}
27+
28+
/**
29+
* Reverse the migrations.
30+
*/
31+
public function down(): void
32+
{
33+
Schema::dropIfExists('colours');
34+
}
35+
};
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/**
4+
* SPDX-License-Identifier: MIT
5+
* Copyright (c) 2017-2018 Tobias Reich
6+
* Copyright (c) 2018-2025 LycheeOrg.
7+
*/
8+
9+
use Illuminate\Database\Migrations\Migration;
10+
use Illuminate\Database\Schema\Blueprint;
11+
use Illuminate\Support\Facades\Schema;
12+
13+
return new class() extends Migration {
14+
public const PHOTO_ID = 'photo_id';
15+
public const RANDOM_ID_LENGTH = 24; // Length of the random ID
16+
17+
/**
18+
* Run the migrations.
19+
*/
20+
public function up(): void
21+
{
22+
Schema::create('palettes', function (Blueprint $table) {
23+
$table->id();
24+
$table->char(self::PHOTO_ID, self::RANDOM_ID_LENGTH)->nullable(false);
25+
$table->unsignedMediumInteger('colour_1')->nullable(false);
26+
$table->unsignedMediumInteger('colour_2')->nullable(false);
27+
$table->unsignedMediumInteger('colour_3')->nullable(false);
28+
$table->unsignedMediumInteger('colour_4')->nullable(false);
29+
$table->unsignedMediumInteger('colour_5')->nullable(false);
30+
31+
$table->index('id');
32+
$table->index([self::PHOTO_ID]);
33+
});
34+
}
35+
36+
/**
37+
* Reverse the migrations.
38+
*/
39+
public function down(): void
40+
{
41+
Schema::dropIfExists('palettes');
42+
}
43+
};

0 commit comments

Comments
 (0)