summaryrefslogtreecommitdiff
path: root/svx/source/tbxctrls/Palette.cxx
diff options
context:
space:
mode:
authorKrisztian Pinter <pin.terminator@gmail.com>2014-07-03 18:47:10 +0200
committerKrisztian Pinter <pin.terminator@gmail.com>2014-07-25 17:14:12 +0200
commit0ac41beac1495e86b103889ffa8d2ba6bf64ddcd (patch)
tree8693979221a9343754b50d58a90666d53942faf3 /svx/source/tbxctrls/Palette.cxx
parent1cff66591f531f2acc40a4400c704f8708d8027d (diff)
Add PaletteManager, refactor palette code
Change-Id: I7e30fc895834318514b51bc648d32aa6d297bfae
Diffstat (limited to 'svx/source/tbxctrls/Palette.cxx')
-rw-r--r--svx/source/tbxctrls/Palette.cxx120
1 files changed, 120 insertions, 0 deletions
diff --git a/svx/source/tbxctrls/Palette.cxx b/svx/source/tbxctrls/Palette.cxx
new file mode 100644
index 000000000000..aebb7f0fa48d
--- /dev/null
+++ b/svx/source/tbxctrls/Palette.cxx
@@ -0,0 +1,120 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+#include <svx/Palette.hxx>
+#include <tools/stream.hxx>
+
+// finds first token in rStr from index, separated by whitespace
+// returns position of next token in index
+OString lcl_getToken(const OString& rStr, sal_Int32& index)
+{
+ sal_Int32 substart, toklen = 0;
+
+ while(index < rStr.getLength() &&
+ (rStr[index] == ' ' || rStr[index] == '\n' || rStr[index] == '\t'))
+ ++index;
+ if(index == rStr.getLength())
+ {
+ index = -1;
+ return OString();
+ }
+ substart = index;
+
+ while(index < rStr.getLength() &&
+ !(rStr[index] == ' ' || rStr[index] == '\n' || rStr[index] == '\t'))
+ {
+ ++index;
+ ++toklen;
+ }
+
+ while(index < rStr.getLength() &&
+ (rStr[index] == ' ' || rStr[index] == '\n' || rStr[index] == '\t'))
+ ++index;
+ if(index == rStr.getLength())
+ index = -1;
+
+ return rStr.copy(substart, toklen);
+}
+
+Palette::Palette(const OUString &rFname) :
+ mbLoaded( false ),
+ maFname( rFname ){}
+
+const OString& Palette::GetPaletteName()
+{
+ LoadPalette();
+ return maName;
+}
+
+const Palette::ColorList& Palette::GetPaletteColors()
+{
+ LoadPalette();
+ return maColors;
+}
+
+void Palette::LoadPalette()
+{
+ if( mbLoaded ) return;
+
+ mbLoaded = true;
+
+ // TODO add error handling!!!
+ SvFileStream aFile(maFname, STREAM_READ);
+
+ OString aLine;
+
+ aFile.ReadLine(aLine);
+ if( !aLine.startsWith("GIMP Palette") ) return;
+ aFile.ReadLine(aLine);
+ if( aLine.startsWith("Name: ", &maName) )
+ {
+ aFile.ReadLine(aLine);
+ if( aLine.startsWith("Columns: "))
+ aFile.ReadLine(aLine); // we can ignore this
+ }
+
+ do {
+ if (aLine[0] != '#' && aLine[0] != '\n')
+ {
+ // TODO check if r,g,b are 0<= x <=255, or just clamp?
+ sal_Int32 nIndex = 0;
+ OString token;
+
+ token = lcl_getToken(aLine, nIndex);
+ if(token == "" || nIndex == -1) continue;
+ sal_Int32 r = token.toInt32();
+
+ token = lcl_getToken(aLine, nIndex);
+ if(token == "" || nIndex == -1) continue;
+ sal_Int32 g = token.toInt32();
+
+ token = lcl_getToken(aLine, nIndex);
+ if(token == "") continue;
+ sal_Int32 b = token.toInt32();
+
+ OString name;
+ if(nIndex != -1)
+ name = aLine.copy(nIndex);
+
+ maColors.push_back(std::make_pair(Color(r, g, b), name));
+ }
+ } while (aFile.ReadLine(aLine));
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */